11package config
22
3- import "go.uber.org/zap"
3+ import (
4+ "errors"
5+
6+ "go.uber.org/zap"
7+ )
8+
9+ type ServerMode = string
10+
11+ const (
12+ // ServerModeHTTP is the HTTP server mode.
13+ ServerModeHTTP ServerMode = "http"
14+ // ServerModeLambda is the AWS Lambda server mode.
15+ ServerModeLambda ServerMode = "lambda"
16+ // ServerModeUnixSocket is the Unix Socket server mode.
17+ ServerModeUnixSocket ServerMode = "unixsocket"
18+ )
19+
20+ type LambdaAdapter = string
21+
22+ const (
23+ LambdaAdapterAPIGatewayV1 LambdaAdapter = "APIGatewayV1"
24+ LambdaAdapterAPIGatewayV2 LambdaAdapter = "APIGatewayV2"
25+ LambdaAdapterALB LambdaAdapter = "ALB"
26+ )
427
528type Server struct {
29+ // Mode is the server mode.
30+ // default: http
31+ Mode ServerMode `mapstructure:"mode" koanf:"mode"`
32+
633 // Host is the server host.
734 // default: 0.0.0.0
835 Host string `mapstructure:"host" koanf:"host"`
@@ -11,12 +38,38 @@ type Server struct {
1138 // default: 1031
1239 Port int `mapstructure:"port" koanf:"port"`
1340
14- // MonitoringPort is the monitoring port.
41+ // MonitoringPort is the monitoring port. It can be specified only if the server mode is HTTP.
1542 // default: none, it will use the same as server port.
1643 MonitoringPort int `mapstructure:"monitoringPort" koanf:"monitoringport"`
1744
1845 // UnixSocket is the server unix socket path.
1946 UnixSocket string `mapstructure:"unixSocket" koanf:"unixsocket"`
47+
48+ // AWS Lambda configuration
49+ // LambdaAdapter is the adapter to use when the relay proxy is started as an AWS Lambda.
50+ // default: APIGatewayV2
51+ LambdaAdapter LambdaAdapter `mapstructure:"awsLambdaAdapter" koanf:"awsLambdaAdapter"`
52+
53+ // AwsApiGatewayBasePath (optional) is the base path prefix for AWS API Gateway deployments.
54+ // This is useful when deploying behind a non-root path like "/api" or "/dev/feature-flags".
55+ // The relay proxy will strip this base path from incoming requests before processing.
56+ // Example: if set to "/api/feature-flags", requests to "/api/feature-flags/health" will be processed as "/health"
57+ // Default: ""
58+ AwsApiGatewayBasePath string `mapstructure:"awsApiGatewayBasePath" koanf:"awsapigatewaybasepath"`
59+ }
60+
61+ func (s * Server ) Validate () error {
62+ switch s .Mode {
63+ case ServerModeUnixSocket :
64+ if s .UnixSocket == "" {
65+ return errors .New ("unixSocket must be set when server mode is unixsocket" )
66+ }
67+ return nil
68+ case ServerModeLambda , ServerModeHTTP :
69+ return nil
70+ default :
71+ return errors .New ("invalid server mode: " + s .Mode )
72+ }
2073}
2174
2275// GetMonitoringPort returns the monitoring port, checking first the top-level config
@@ -54,3 +107,54 @@ func (c *Config) GetServerPort(logger *zap.Logger) int {
54107 }
55108 return 1031
56109}
110+
111+ // GetServerMode returns the server mode, checking first the server config
112+ // and then the top-level config, defaulting to HTTP if not set.
113+ func (c * Config ) GetServerMode (logger * zap.Logger ) ServerMode {
114+ if c .Server .Mode != "" {
115+ return c .Server .Mode
116+ }
117+
118+ if c .StartAsAwsLambda {
119+ if logger != nil {
120+ zap .L ().Warn ("The server mode is set using `startAsAwsLambda`, this option is deprecated, please migrate to `server.mode`" )
121+ }
122+ return ServerModeLambda
123+ }
124+
125+ return ServerModeHTTP
126+ }
127+
128+ // GetLambdaAdapter returns the lambda adapter, checking first the server config
129+ // and then the top-level config, defaulting to APIGatewayV2 if not set.
130+ func (c * Config ) GetLambdaAdapter (logger * zap.Logger ) LambdaAdapter {
131+ if c .Server .LambdaAdapter != "" {
132+ return c .Server .LambdaAdapter
133+ }
134+
135+ if c .AwsLambdaAdapter != "" {
136+ if logger != nil {
137+ zap .L ().Warn ("The lambda adapter is set using `awsLambdaAdapter`, this option is deprecated, please migrate to `server.awsLambdaAdapter`" )
138+ }
139+ return LambdaAdapter (c .AwsLambdaAdapter )
140+ }
141+
142+ return LambdaAdapterAPIGatewayV2
143+ }
144+
145+ // GetAwsApiGatewayBasePath returns the AWS API Gateway base path, checking first the server config
146+ // and then the top-level config, defaulting to empty string if not set.
147+ func (c * Config ) GetAwsApiGatewayBasePath (logger * zap.Logger ) string {
148+ if c .Server .AwsApiGatewayBasePath != "" {
149+ return c .Server .AwsApiGatewayBasePath
150+ }
151+
152+ if c .AwsApiGatewayBasePath != "" {
153+ if logger != nil {
154+ zap .L ().Warn ("The AWS API Gateway base path is set using `awsApiGatewayBasePath`, this option is deprecated, please migrate to `server.awsApiGatewayBasePath`" )
155+ }
156+ return c .AwsApiGatewayBasePath
157+ }
158+
159+ return ""
160+ }
0 commit comments