Skip to content

Commit

Permalink
chore: refactor service configuration objects (open-feature#545)
Browse files Browse the repository at this point in the history
<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

### Related Issues
<!-- add here the GitHub issue that this PR resolves if applicable -->

Fixes open-feature#524

Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
  • Loading branch information
odubajDT authored Mar 23, 2023
1 parent 30c8022 commit c7b29ed
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 deletions.
6 changes: 0 additions & 6 deletions core/pkg/runtime/from_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ func FromConfig(logger *logger.Logger, config Config) (*Runtime, error) {

func (r *Runtime) setService(logger *logger.Logger) {
r.Service = &service.ConnectService{
ConnectServiceConfiguration: &service.ConnectServiceConfiguration{
ServerKeyPath: r.config.ServiceKeyPath,
ServerCertPath: r.config.ServiceCertPath,
ServerSocketPath: r.config.ServiceSocketPath,
CORS: r.config.CORS,
},
Logger: logger.WithFields(
zap.String("component", "service"),
),
Expand Down
4 changes: 4 additions & 0 deletions core/pkg/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (r *Runtime) Start() error {
Port: r.config.ServicePort,
MetricsPort: r.config.MetricsPort,
ServiceName: r.serviceName,
KeyPath: r.config.ServiceKeyPath,
CertPath: r.config.ServiceCertPath,
SocketPath: r.config.ServiceSocketPath,
CORS: r.config.CORS,
})
})
<-gCtx.Done()
Expand Down
37 changes: 15 additions & 22 deletions core/pkg/service/flag-evaluation/connect_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,11 @@ import (
const ErrorPrefix = "FlagdError:"

type ConnectService struct {
ConnectServiceConfiguration *ConnectServiceConfiguration
Eval eval.IEvaluator
Logger *logger.Logger
Metrics *otel.MetricsRecorder
eventingConfiguration *eventingConfiguration
server http.Server
}
type ConnectServiceConfiguration struct {
ServerCertPath string
ServerKeyPath string
ServerSocketPath string
CORS []string
Logger *logger.Logger
Eval eval.IEvaluator
Metrics *otel.MetricsRecorder
eventingConfiguration *eventingConfiguration
server http.Server
}

func (s *ConnectService) Serve(ctx context.Context, eval eval.IEvaluator, svcConf service.Configuration) error {
Expand All @@ -54,11 +47,11 @@ func (s *ConnectService) Serve(ctx context.Context, eval eval.IEvaluator, svcCon
errChan := make(chan error, 1)
go func() {
s.Logger.Info(fmt.Sprintf("Flag Evaluation listening at %s", lis.Addr()))
if s.ConnectServiceConfiguration.ServerCertPath != "" && s.ConnectServiceConfiguration.ServerKeyPath != "" {
if svcConf.CertPath != "" && svcConf.KeyPath != "" {
if err := s.server.ServeTLS(
lis,
s.ConnectServiceConfiguration.ServerCertPath,
s.ConnectServiceConfiguration.ServerKeyPath,
svcConf.CertPath,
svcConf.KeyPath,
); err != nil && !errors.Is(err, http.ErrServerClosed) {
errChan <- err
}
Expand All @@ -84,8 +77,8 @@ func (s *ConnectService) setupServer(svcConf service.Configuration) (net.Listene
var lis net.Listener
var err error
mux := http.NewServeMux()
if s.ConnectServiceConfiguration.ServerSocketPath != "" {
lis, err = net.Listen("unix", s.ConnectServiceConfiguration.ServerSocketPath)
if svcConf.SocketPath != "" {
lis, err = net.Listen("unix", svcConf.SocketPath)
} else {
address := fmt.Sprintf(":%d", svcConf.Port)
lis, err = net.Listen("tcp", address)
Expand All @@ -110,11 +103,11 @@ func (s *ConnectService) setupServer(svcConf service.Configuration) (net.Listene

go bindMetrics(s, svcConf)

if s.ConnectServiceConfiguration.ServerCertPath != "" && s.ConnectServiceConfiguration.ServerKeyPath != "" {
handler = s.newCORS().Handler(h)
if svcConf.CertPath != "" && svcConf.KeyPath != "" {
handler = s.newCORS(svcConf).Handler(h)
} else {
handler = h2c.NewHandler(
s.newCORS().Handler(h),
s.newCORS(svcConf).Handler(h),
&http2.Server{},
)
}
Expand All @@ -133,7 +126,7 @@ func (s *ConnectService) Notify(n service.Notification) {
}
}

func (s *ConnectService) newCORS() *cors.Cors {
func (s *ConnectService) newCORS(svcConf service.Configuration) *cors.Cors {
return cors.New(cors.Options{
AllowedMethods: []string{
http.MethodHead,
Expand All @@ -143,7 +136,7 @@ func (s *ConnectService) newCORS() *cors.Cors {
http.MethodPatch,
http.MethodDelete,
},
AllowedOrigins: s.ConnectServiceConfiguration.CORS,
AllowedOrigins: svcConf.CORS,
AllowedHeaders: []string{"*"},
ExposedHeaders: []string{
// Content-Type is in the default safelist.
Expand Down
4 changes: 1 addition & 3 deletions core/pkg/service/flag-evaluation/connect_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ func TestConnectService_UnixConnection(t *testing.T) {
exp := metric.NewManualReader()
metricRecorder := otel.NewOTelRecorder(exp, tt.name)
svc := ConnectService{
ConnectServiceConfiguration: &ConnectServiceConfiguration{
ServerSocketPath: tt.socketPath,
},
Logger: logger.NewLogger(nil, false),
Metrics: metricRecorder,
}
serveConf := iservice.Configuration{
ReadinessProbe: func() bool {
return true
},
SocketPath: tt.socketPath,
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
Expand Down
4 changes: 4 additions & 0 deletions core/pkg/service/iservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type Configuration struct {
Port uint16
MetricsPort uint16
ServiceName string
CertPath string
KeyPath string
SocketPath string
CORS []string
}

/*
Expand Down

0 comments on commit c7b29ed

Please sign in to comment.