Skip to content

Commit a451b0e

Browse files
easyCZroboquat
authored andcommitted
[public-api] Convert to a cobra command and add config
1 parent 2093016 commit a451b0e

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

components/public-api-server/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/gitpod-io/gitpod/common-go v0.0.0-00010101000000-000000000000
77
github.com/gitpod-io/gitpod/public-api v0.0.0-00010101000000-000000000000
88
github.com/sirupsen/logrus v1.8.1
9+
github.com/spf13/cobra v1.4.0
910
github.com/stretchr/testify v1.7.0
1011
google.golang.org/grpc v1.45.0
1112
google.golang.org/protobuf v1.28.0
@@ -20,13 +21,15 @@ require (
2021
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
2122
github.com/hashicorp/golang-lru v0.5.1 // indirect
2223
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb // indirect
24+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
2325
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
2426
github.com/opentracing/opentracing-go v1.2.0 // indirect
2527
github.com/pmezard/go-difflib v1.0.0 // indirect
2628
github.com/prometheus/client_golang v1.12.1 // indirect
2729
github.com/prometheus/client_model v0.2.0 // indirect
2830
github.com/prometheus/common v0.32.1 // indirect
2931
github.com/prometheus/procfs v0.7.3 // indirect
32+
github.com/spf13/pflag v1.0.5 // indirect
3033
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
3134
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
3235
golang.org/x/text v0.3.7 // indirect

components/public-api-server/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XP
5757
github.com/cncf/xds/go v0.0.0-20210805033703-aa0b78936158/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
5858
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
5959
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
60+
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
6061
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6162
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
6263
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -145,6 +146,8 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
145146
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb h1:tsEKRC3PU9rMw18w/uAptoijhgG4EvlA5kfJPtwrMDk=
146147
github.com/heptiolabs/healthcheck v0.0.0-20211123025425-613501dd5deb/go.mod h1:NtmN9h8vrTveVQRLHcX2HQ5wIPBDCsZ351TGbZWgg38=
147148
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
149+
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
150+
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
148151
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
149152
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
150153
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
@@ -205,11 +208,16 @@ github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0
205208
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
206209
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
207210
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
211+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
208212
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
209213
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
210214
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
211215
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
212216
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
217+
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
218+
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
219+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
220+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
213221
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
214222
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
215223
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=

components/public-api-server/main.go

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,76 @@
55
package main
66

77
import (
8+
"context"
89
"github.com/gitpod-io/gitpod/common-go/log"
910
"github.com/gitpod-io/gitpod/public-api-server/pkg/server"
11+
"github.com/sirupsen/logrus"
12+
"github.com/spf13/cobra"
13+
"github.com/spf13/pflag"
14+
"net/url"
15+
)
16+
17+
const (
18+
// Version is the current component version. Set during build time.
19+
Version = ""
20+
Service = "public-api-server"
1021
)
1122

1223
func main() {
13-
logger := log.New()
24+
ctx := context.Background()
25+
cmd := command()
1426

15-
if err := server.Start(logger); err != nil {
16-
logger.WithError(err).Fatal("Server errored.")
27+
if err := cmd.ExecuteContext(ctx); err != nil {
28+
log.Log.WithError(err).Fatal("Failed to execute command.")
1729
}
1830
}
31+
32+
func command() *cobra.Command {
33+
var (
34+
gitpodAPIURL string
35+
httpPort, grpcPort int
36+
verbose bool
37+
)
38+
39+
cmd := &cobra.Command{
40+
Use: "public-api-server",
41+
Short: "Serves public API services",
42+
Version: Version,
43+
Run: func(cmd *cobra.Command, args []string) {
44+
log.Init(Service, Version, true, verbose)
45+
logger := log.Log
46+
47+
logger.WithField("config", flagsToLogFields(cmd.Flags())).Info("Starting with config.")
48+
49+
gitpodAPI, urlErr := url.Parse(gitpodAPIURL)
50+
if urlErr != nil {
51+
logger.WithError(urlErr).Fatal("Failed to parse Gitpod API URL.")
52+
}
53+
54+
if err := server.Start(logger, server.Config{
55+
GitpodAPI: gitpodAPI,
56+
HTTPPort: httpPort,
57+
GRPCPort: grpcPort,
58+
}); err != nil {
59+
logger.WithError(err).Fatal("Server errored.")
60+
}
61+
},
62+
}
63+
64+
cmd.Flags().StringVar(&gitpodAPIURL, "gitpod-api-url", "wss://main.preview.gitpod-dev.com/api/v1", "URL for existing Gitpod Websocket API")
65+
cmd.Flags().IntVar(&httpPort, "http-port", 9500, "Port for serving HTTP traffic")
66+
cmd.Flags().IntVar(&grpcPort, "grpc-port", 9501, "Port for serving gRPC traffic")
67+
cmd.Flags().BoolVar(&verbose, "verbose", false, "Toggle verbose logging (debug level)")
68+
69+
return cmd
70+
}
71+
72+
func flagsToLogFields(fs *pflag.FlagSet) logrus.Fields {
73+
fields := logrus.Fields{}
74+
75+
fs.VisitAll(func(f *pflag.Flag) {
76+
fields[f.Name] = f.Value
77+
})
78+
79+
return fields
80+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License-AGPL.txt in the project root for license information.
4+
5+
package server
6+
7+
import "net/url"
8+
9+
type Config struct {
10+
GitpodAPI *url.URL
11+
12+
HTTPPort int
13+
GRPCPort int
14+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
"net/http"
1616
)
1717

18-
func Start(logger *logrus.Entry) error {
18+
func Start(logger *logrus.Entry, cfg Config) error {
1919
srv, err := baseserver.New("public_api_server",
2020
baseserver.WithLogger(logger),
21-
baseserver.WithHTTPPort(9000),
22-
baseserver.WithGRPCPort(9001),
21+
baseserver.WithHTTPPort(cfg.HTTPPort),
22+
baseserver.WithGRPCPort(cfg.GRPCPort),
2323
)
2424
if err != nil {
2525
return fmt.Errorf("failed to initialize public api server: %w", err)

0 commit comments

Comments
 (0)