Skip to content

Commit 9c8050a

Browse files
committed
[public-api] Wire up DB connection
1 parent 6d6b667 commit 9c8050a

File tree

6 files changed

+41
-9
lines changed

6 files changed

+41
-9
lines changed

components/public-api-server/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/stripe/stripe-go/v72 v72.122.0
2525
google.golang.org/grpc v1.49.0
2626
google.golang.org/protobuf v1.28.1
27+
gorm.io/gorm v1.24.1
2728
)
2829

2930
require (
@@ -35,11 +36,14 @@ require (
3536
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
3637
github.com/davecgh/go-spew v1.1.1 // indirect
3738
github.com/felixge/httpsnoop v1.0.1 // indirect
39+
github.com/go-sql-driver/mysql v1.6.0 // indirect
3840
github.com/golang/protobuf v1.5.2 // indirect
3941
github.com/gorilla/websocket v1.5.0 // indirect
4042
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
4143
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect
4244
github.com/inconshreveable/mousetrap v1.0.0 // indirect
45+
github.com/jinzhu/inflection v1.0.0 // indirect
46+
github.com/jinzhu/now v1.1.5 // indirect
4347
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
4448
github.com/opentracing/opentracing-go v1.2.0 // indirect
4549
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -56,6 +60,7 @@ require (
5660
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
5761
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect
5862
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
63+
gorm.io/driver/mysql v1.4.4 // indirect
5964
)
6065

6166
replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway

components/public-api-server/go.sum

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/public-api-server/pkg/apiv1/tokens.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ import (
1919
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
2020
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
2121
"github.com/google/uuid"
22+
"gorm.io/gorm"
2223
)
2324

24-
func NewTokensService(connPool proxy.ServerConnectionPool, expClient experiments.Client) *TokensService {
25+
func NewTokensService(connPool proxy.ServerConnectionPool, expClient experiments.Client, dbConn *gorm.DB) *TokensService {
2526
return &TokensService{
2627
connectionPool: connPool,
2728
expClient: expClient,
29+
dbConn: dbConn,
2830
}
2931
}
3032

3133
type TokensService struct {
3234
connectionPool proxy.ServerConnectionPool
33-
34-
expClient experiments.Client
35+
expClient experiments.Client
36+
dbConn *gorm.DB
3537

3638
v1connect.UnimplementedTokensServiceHandler
3739
}

components/public-api-server/pkg/apiv1/tokens_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func setupTokensService(t *testing.T, expClient experiments.Client) (*protocol.M
286286

287287
serverMock := protocol.NewMockAPIInterface(ctrl)
288288

289-
svc := NewTokensService(&FakeServerConnPool{api: serverMock}, expClient)
289+
svc := NewTokensService(&FakeServerConnPool{api: serverMock}, expClient, nil)
290290

291291
_, handler := v1connect.NewTokensServiceHandler(svc, connect.WithInterceptors(auth.NewServerInterceptor()))
292292

components/public-api-server/pkg/server/integration_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestPublicAPIServer_v1_WorkspaceService(t *testing.T) {
3131

3232
connPool := proxy.ServerConnectionPool(&proxy.NoConnectionPool{ServerAPI: gitpodAPI})
3333

34-
require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient()))
34+
require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient(), nil))
3535
baseserver.StartServerForTests(t, srv)
3636

3737
workspaceClient := v1connect.NewWorkspacesServiceClient(http.DefaultClient, srv.HTTPAddress(), connect.WithInterceptors(auth.NewClientInterceptor("some-token")))
@@ -60,7 +60,7 @@ func TestConnectWorkspaceService_RequiresAuth(t *testing.T) {
6060

6161
connPool := proxy.ServerConnectionPool(&proxy.NoConnectionPool{ServerAPI: gitpodAPI})
6262

63-
require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient()))
63+
require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient(), nil))
6464

6565
baseserver.StartServerForTests(t, srv)
6666

components/public-api-server/pkg/server/server.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ package server
66

77
import (
88
"fmt"
9+
"net"
910
"net/http"
1011
"net/url"
1112
"os"
1213
"strings"
1314

1415
"github.com/bufbuild/connect-go"
16+
common_db "github.com/gitpod-io/gitpod/common-go/db"
1517
"github.com/gitpod-io/gitpod/common-go/experiments"
1618
"github.com/gitpod-io/gitpod/common-go/log"
19+
"gorm.io/gorm"
1720

1821
"github.com/gitpod-io/gitpod/components/public-api/go/config"
1922
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
@@ -41,6 +44,16 @@ func Start(logger *logrus.Entry, version string, cfg *config.Configuration) erro
4144
return fmt.Errorf("failed to setup connection pool: %w", err)
4245
}
4346

47+
dbConn, err := common_db.Connect(common_db.ConnectionParams{
48+
User: os.Getenv("DB_USERNAME"),
49+
Password: os.Getenv("DB_PASSWORD"),
50+
Host: net.JoinHostPort(os.Getenv("DB_HOST"), os.Getenv("DB_PORT")),
51+
Database: "gitpod",
52+
})
53+
if err != nil {
54+
return fmt.Errorf("failed to establish database connection: %w", err)
55+
}
56+
4457
expClient := experiments.NewClient()
4558

4659
srv, err := baseserver.New("public_api_server",
@@ -73,7 +86,7 @@ func Start(logger *logrus.Entry, version string, cfg *config.Configuration) erro
7386

7487
srv.HTTPMux().Handle("/stripe/invoices/webhook", handlers.ContentTypeHandler(stripeWebhookHandler, "application/json"))
7588

76-
if registerErr := register(srv, connPool, expClient); registerErr != nil {
89+
if registerErr := register(srv, connPool, expClient, dbConn); registerErr != nil {
7790
return fmt.Errorf("failed to register services: %w", registerErr)
7891
}
7992

@@ -84,7 +97,7 @@ func Start(logger *logrus.Entry, version string, cfg *config.Configuration) erro
8497
return nil
8598
}
8699

87-
func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expClient experiments.Client) error {
100+
func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expClient experiments.Client, dbConn *gorm.DB) error {
88101
proxy.RegisterMetrics(srv.MetricsRegistry())
89102

90103
connectMetrics := NewConnectMetrics()
@@ -107,7 +120,7 @@ func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expCl
107120
teamsRoute, teamsServiceHandler := v1connect.NewTeamsServiceHandler(apiv1.NewTeamsService(connPool), handlerOptions...)
108121
srv.HTTPMux().Handle(teamsRoute, teamsServiceHandler)
109122

110-
tokensRoute, tokensServiceHandler := v1connect.NewTokensServiceHandler(apiv1.NewTokensService(connPool, expClient), handlerOptions...)
123+
tokensRoute, tokensServiceHandler := v1connect.NewTokensServiceHandler(apiv1.NewTokensService(connPool, expClient, dbConn), handlerOptions...)
111124
srv.HTTPMux().Handle(tokensRoute, tokensServiceHandler)
112125

113126
userRoute, userServiceHandler := v1connect.NewUserServiceHandler(apiv1.NewUserService(connPool), handlerOptions...)

0 commit comments

Comments
 (0)