Skip to content

Commit 25ad9d5

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

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

components/public-api-server/go.mod

Lines changed: 5 additions & 0 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 5 additions & 3 deletions
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/server/server.go

Lines changed: 15 additions & 2 deletions
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",
@@ -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)