@@ -61,6 +61,15 @@ type GitConfig struct {
6161 MaxConnections int `env:"MAX_CONNECTIONS" yaml:"max_connections"`
6262}
6363
64+ // CORSConfig is the CORS configuration for the server.
65+ type CORSConfig struct {
66+ AllowedHeaders []string `env:"ALLOWED_HEADERS" yaml:"allowed_headers"`
67+
68+ AllowedOrigins []string `env:"ALLOWED_ORIGINS" yaml:"allowed_origins"`
69+
70+ AllowedMethods []string `env:"ALLOWED_METHODS" yaml:"allowed_methods"`
71+ }
72+
6473// HTTPConfig is the HTTP configuration for the server.
6574type HTTPConfig struct {
6675 // Enabled toggles the HTTP server on/off
@@ -77,6 +86,9 @@ type HTTPConfig struct {
7786
7887 // PublicURL is the public URL of the HTTP server.
7988 PublicURL string `env:"PUBLIC_URL" yaml:"public_url"`
89+
90+ // CORS is the cross-origin configuration for the HTTP server.
91+ CORS CORSConfig `envPrefix:"CORS_" yaml:"cors"`
8092}
8193
8294// StatsConfig is the configuration for the stats server.
@@ -196,6 +208,9 @@ func (c *Config) Environ() []string {
196208 fmt .Sprintf ("SOFT_SERVE_HTTP_TLS_KEY_PATH=%s" , c .HTTP .TLSKeyPath ),
197209 fmt .Sprintf ("SOFT_SERVE_HTTP_TLS_CERT_PATH=%s" , c .HTTP .TLSCertPath ),
198210 fmt .Sprintf ("SOFT_SERVE_HTTP_PUBLIC_URL=%s" , c .HTTP .PublicURL ),
211+ fmt .Sprintf ("SOFT_SERVE_HTTP_CORS_ALLOWED_HEADERS=%s" , strings .Join (c .HTTP .CORS .AllowedHeaders , "," )),
212+ fmt .Sprintf ("SOFT_SERVE_HTTP_CORS_ALLOWED_ORIGINS=%s" , strings .Join (c .HTTP .CORS .AllowedOrigins , "," )),
213+ fmt .Sprintf ("SOFT_SERVE_HTTP_CORS_ALLOWED_METHODS=%s" , strings .Join (c .HTTP .CORS .AllowedMethods , "," )),
199214 fmt .Sprintf ("SOFT_SERVE_STATS_ENABLED=%t" , c .Stats .Enabled ),
200215 fmt .Sprintf ("SOFT_SERVE_STATS_LISTEN_ADDR=%s" , c .Stats .ListenAddr ),
201216 fmt .Sprintf ("SOFT_SERVE_LOG_FORMAT=%s" , c .Log .Format ),
@@ -355,6 +370,11 @@ func DefaultConfig() *Config {
355370 Enabled : true ,
356371 ListenAddr : ":23232" ,
357372 PublicURL : "http://localhost:23232" ,
373+ CORS : CORSConfig {
374+ AllowedHeaders : []string {"Accept" , "Accept-Language" , "Content-Language" , "Content-Type" , "Origin" , "X-Requested-With" , "User-Agent" , "Authorization" , "Access-Control-Request-Method" , "Access-Control-Allow-Origin" },
375+ AllowedMethods : []string {"GET" , "HEAD" , "POST" , "PUT" , "OPTIONS" },
376+ AllowedOrigins : []string {"http://localhost:23232" },
377+ },
358378 },
359379 Stats : StatsConfig {
360380 Enabled : true ,
@@ -423,6 +443,8 @@ func (c *Config) Validate() error {
423443
424444 c .InitialAdminKeys = pks
425445
446+ c .HTTP .CORS .AllowedOrigins = append ([]string {c .HTTP .PublicURL }, c .HTTP .CORS .AllowedOrigins ... )
447+
426448 return nil
427449}
428450
0 commit comments