Skip to content

[public-api] Wire up DB connection #14741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/public-api-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/stripe/stripe-go/v72 v72.122.0
google.golang.org/grpc v1.49.0
google.golang.org/protobuf v1.28.1
gorm.io/gorm v1.24.1
)

require (
Expand All @@ -35,11 +36,14 @@ require (
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -56,6 +60,7 @@ require (
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gorm.io/driver/mysql v1.4.4 // indirect
)

replace github.com/gitpod-io/gitpod/common-go => ../common-go // leeway
Expand Down
12 changes: 12 additions & 0 deletions components/public-api-server/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions components/public-api-server/pkg/apiv1/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ import (
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
"github.com/google/uuid"
"gorm.io/gorm"
)

func NewTokensService(connPool proxy.ServerConnectionPool, expClient experiments.Client) *TokensService {
func NewTokensService(connPool proxy.ServerConnectionPool, expClient experiments.Client, dbConn *gorm.DB) *TokensService {
return &TokensService{
connectionPool: connPool,
expClient: expClient,
dbConn: dbConn,
}
}

type TokensService struct {
connectionPool proxy.ServerConnectionPool

expClient experiments.Client
expClient experiments.Client
dbConn *gorm.DB

v1connect.UnimplementedTokensServiceHandler
}
Expand Down
2 changes: 1 addition & 1 deletion components/public-api-server/pkg/apiv1/tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func setupTokensService(t *testing.T, expClient experiments.Client) (*protocol.M

serverMock := protocol.NewMockAPIInterface(ctrl)

svc := NewTokensService(&FakeServerConnPool{api: serverMock}, expClient)
svc := NewTokensService(&FakeServerConnPool{api: serverMock}, expClient, nil)

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

Expand Down
4 changes: 2 additions & 2 deletions components/public-api-server/pkg/server/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestPublicAPIServer_v1_WorkspaceService(t *testing.T) {

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

require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient()))
require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient(), nil))
baseserver.StartServerForTests(t, srv)

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

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

require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient()))
require.NoError(t, register(srv, connPool, experiments.NewAlwaysReturningDefaultValueClient(), nil))

baseserver.StartServerForTests(t, srv)

Expand Down
19 changes: 16 additions & 3 deletions components/public-api-server/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ package server

import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"strings"

"github.com/bufbuild/connect-go"
common_db "github.com/gitpod-io/gitpod/common-go/db"
"github.com/gitpod-io/gitpod/common-go/experiments"
"github.com/gitpod-io/gitpod/common-go/log"
"gorm.io/gorm"

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

dbConn, err := common_db.Connect(common_db.ConnectionParams{
User: os.Getenv("DB_USERNAME"),
Password: os.Getenv("DB_PASSWORD"),
Host: net.JoinHostPort(os.Getenv("DB_HOST"), os.Getenv("DB_PORT")),
Database: "gitpod",
})
if err != nil {
return fmt.Errorf("failed to establish database connection: %w", err)
}

expClient := experiments.NewClient()

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

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

if registerErr := register(srv, connPool, expClient); registerErr != nil {
if registerErr := register(srv, connPool, expClient, dbConn); registerErr != nil {
return fmt.Errorf("failed to register services: %w", registerErr)
}

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

func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expClient experiments.Client) error {
func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expClient experiments.Client, dbConn *gorm.DB) error {
proxy.RegisterMetrics(srv.MetricsRegistry())

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

tokensRoute, tokensServiceHandler := v1connect.NewTokensServiceHandler(apiv1.NewTokensService(connPool, expClient), handlerOptions...)
tokensRoute, tokensServiceHandler := v1connect.NewTokensServiceHandler(apiv1.NewTokensService(connPool, expClient, dbConn), handlerOptions...)
srv.HTTPMux().Handle(tokensRoute, tokensServiceHandler)

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