Skip to content

Commit d2384ea

Browse files
csweichelroboquat
authored andcommitted
[baseserver] Make consuing ServerConfiguration easier
1 parent 5dd9465 commit d2384ea

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

components/common-go/baseserver/options.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,17 @@ func WithUnderTest() Option {
7777
}
7878

7979
// WithHTTP configures and enables the HTTP server.
80-
func WithHTTP(addr string, tls *TLSConfiguration) Option {
80+
func WithHTTP(cfg *ServerConfiguration) Option {
8181
return func(opts *options) error {
82-
opts.config.Services.HTTP = &ServerConfiguration{
83-
Address: addr,
84-
TLS: tls,
85-
}
82+
opts.config.Services.HTTP = cfg
8683
return nil
8784
}
8885
}
8986

9087
// WithGRPC configures and enables the GRPC server.
91-
func WithGRPC(addr string, tls *TLSConfiguration) Option {
88+
func WithGRPC(cfg *ServerConfiguration) Option {
9289
return func(opts *options) error {
93-
opts.config.Services.GRPC = &ServerConfiguration{
94-
Address: addr,
95-
TLS: tls,
96-
}
90+
opts.config.Services.GRPC = cfg
9791
return nil
9892
}
9993
}

components/common-go/baseserver/options_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func TestOptions(t *testing.T) {
2525
grpcCfg := ServerConfiguration{Address: "localhost:8081"}
2626

2727
var opts = []Option{
28-
WithHTTP(httpCfg.Address, httpCfg.TLS),
29-
WithGRPC(grpcCfg.Address, grpcCfg.TLS),
28+
WithHTTP(&httpCfg),
29+
WithGRPC(&grpcCfg),
3030
WithLogger(logger),
3131
WithCloseTimeout(timeout),
3232
WithMetricsRegistry(registry),

components/common-go/baseserver/server_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func TestServer_StartStop(t *testing.T) {
2121
// We don't use the helper NewForTests, because we want to control stopping ourselves.
2222
srv, err := baseserver.New("server_test",
2323
baseserver.WithUnderTest(),
24-
baseserver.WithHTTP("localhost:8765", nil),
25-
baseserver.WithGRPC("localhost:8766", nil),
24+
baseserver.WithHTTP(&baseserver.ServerConfiguration{Address: "localhost:8765"}),
25+
baseserver.WithGRPC(&baseserver.ServerConfiguration{Address: "localhost:8766"}),
2626
)
2727
require.NoError(t, err)
2828
baseserver.StartServerForTests(t, srv)
@@ -48,11 +48,15 @@ func TestServer_ServerCombinations_StartsAndStops(t *testing.T) {
4848
var opts []baseserver.Option
4949
opts = append(opts, baseserver.WithUnderTest())
5050
if test.StartHTTP {
51-
opts = append(opts, baseserver.WithHTTP(fmt.Sprintf("localhost:%d", baseserver.MustFindFreePort(t)), nil))
51+
opts = append(opts, baseserver.WithHTTP(&baseserver.ServerConfiguration{
52+
Address: fmt.Sprintf("localhost:%d", baseserver.MustFindFreePort(t)),
53+
}))
5254
}
5355

5456
if test.StartGRPC {
55-
opts = append(opts, baseserver.WithGRPC(fmt.Sprintf("localhost:%d", baseserver.MustFindFreePort(t)), nil))
57+
opts = append(opts, baseserver.WithGRPC(&baseserver.ServerConfiguration{
58+
Address: fmt.Sprintf("localhost:%d", baseserver.MustFindFreePort(t)),
59+
}))
5660
}
5761

5862
srv, err := baseserver.New("test_server", opts...)
@@ -79,7 +83,9 @@ func TestServer_ServerCombinations_StartsAndStops(t *testing.T) {
7983

8084
func TestServer_Metrics_gRPC(t *testing.T) {
8185
ctx := context.Background()
82-
srv := baseserver.NewForTests(t)
86+
srv := baseserver.NewForTests(t, baseserver.WithGRPC(&baseserver.ServerConfiguration{
87+
Address: fmt.Sprintf("localhost:%d", baseserver.MustFindFreePort(t)),
88+
}))
8389

8490
// At this point, there must be metrics registry available for use
8591
require.NotNil(t, srv.MetricsRegistry())

components/common-go/baseserver/testing.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ func NewForTests(t *testing.T, opts ...Option) *Server {
2121

2222
defaultTestOpts := []Option{
2323
WithUnderTest(),
24-
WithGRPC(fmt.Sprintf("localhost:%d", MustFindFreePort(t)), nil),
25-
WithHTTP(fmt.Sprintf("localhost:%d", MustFindFreePort(t)), nil),
2624
WithCloseTimeout(1 * time.Second),
2725
}
2826

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Start(logger *logrus.Entry, cfg Config) error {
2020

2121
srv, err := baseserver.New("public_api_server",
2222
baseserver.WithLogger(logger),
23-
baseserver.WithGRPC(fmt.Sprintf("localhost:%d", cfg.GRPCPort), nil),
23+
baseserver.WithGRPC(&baseserver.ServerConfiguration{Address: fmt.Sprintf("localhost:%d", cfg.GRPCPort)}),
2424
baseserver.WithMetricsRegistry(registry),
2525
)
2626
if err != nil {

0 commit comments

Comments
 (0)