Skip to content

Commit

Permalink
fix variable names, format
Browse files Browse the repository at this point in the history
  • Loading branch information
fetsorn committed Jul 9, 2024
1 parent e9f14ae commit 5907bfc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 41 deletions.
24 changes: 3 additions & 21 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ func (c *Config) Environ() []string {
fmt.Sprintf("SOFT_SERVE_HTTP_TLS_KEY_PATH=%s", c.HTTP.TLSKeyPath),
fmt.Sprintf("SOFT_SERVE_HTTP_TLS_CERT_PATH=%s", c.HTTP.TLSCertPath),
fmt.Sprintf("SOFT_SERVE_HTTP_PUBLIC_URL=%s", c.HTTP.PublicURL),
fmt.Sprintf("SOFT_SERVE_HTTP_ALLOWED_HEADERS=%s", strings.Join(c.HTTP.CORS.AllowedHeaders, "\n")),
fmt.Sprintf("SOFT_SERVE_HTTP_ALLOWED_ORIGINS=%s", strings.Join(c.HTTP.CORS.AllowedOrigins, "\n")),
fmt.Sprintf("SOFT_SERVE_HTTP_ALLOWED_METHODS=%s", strings.Join(c.HTTP.CORS.AllowedMethods, "\n")),
fmt.Sprintf("SOFT_SERVE_HTTP_CORS_ALLOWED_HEADERS=%s", strings.Join(c.HTTP.CORS.AllowedHeaders, "\n")),
fmt.Sprintf("SOFT_SERVE_HTTP_CORS_ALLOWED_ORIGINS=%s", strings.Join(c.HTTP.CORS.AllowedOrigins, "\n")),
fmt.Sprintf("SOFT_SERVE_HTTP_CORS_ALLOWED_METHODS=%s", strings.Join(c.HTTP.CORS.AllowedMethods, "\n")),
fmt.Sprintf("SOFT_SERVE_STATS_LISTEN_ADDR=%s", c.Stats.ListenAddr),
fmt.Sprintf("SOFT_SERVE_LOG_FORMAT=%s", c.Log.Format),
fmt.Sprintf("SOFT_SERVE_LOG_TIME_FORMAT=%s", c.Log.TimeFormat),
Expand Down Expand Up @@ -260,24 +260,6 @@ func parseEnv(cfg *Config) error {
cfg.InitialAdminKeys = append(cfg.InitialAdminKeys, initialAdminKeys...)
}

// split allowed headers and append to cfg
if allowedHeadersEnv := os.Getenv("SOFT_SERVE_HTTP_CORS_ALLOWED_HEADERS"); allowedHeadersEnv != "" {
allowedHeaders := strings.Split(allowedHeadersEnv, " ")
cfg.HTTP.CORS.AllowedHeaders = append(cfg.HTTP.CORS.AllowedHeaders, allowedHeaders...)
}

// split allowed origins and append to cfg
if allowedOriginsEnv := os.Getenv("SOFT_SERVE_HTTP_CORS_ALLOWED_ORIGINS"); allowedOriginsEnv != "" {
allowedOrigins := strings.Split(allowedOriginsEnv, " ")
cfg.HTTP.CORS.AllowedOrigins = append(cfg.HTTP.CORS.AllowedOrigins, allowedOrigins...)
}

// split allowed methods and append to cfg
if allowedMethodsEnv := os.Getenv("SOFT_SERVE_HTTP_CORS_ALLOWED_METHODS"); allowedMethodsEnv != "" {
allowedMethods := strings.Split(allowedMethodsEnv, " ")
cfg.HTTP.CORS.AllowedMethods = append(cfg.HTTP.CORS.AllowedMethods, allowedMethods...)
}

return cfg.Validate()
}

Expand Down
25 changes: 5 additions & 20 deletions pkg/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"net/http"

"github.com/charmbracelet/log"
"github.com/charmbracelet/soft-serve/pkg/config"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/charmbracelet/soft-serve/pkg/config"
)

// NewRouter returns a new HTTP router.
Expand All @@ -29,25 +29,10 @@ func NewRouter(ctx context.Context) http.Handler {

cfg := config.FromContext(ctx)

CORSHeaders := []string{"Accept", "Accept-Language", "Content-Language", "Origin"}

if len(cfg.HTTP.CORS.AllowedHeaders) != 0 {
CORSHeaders = cfg.HTTP.CORS.AllowedHeaders
}

CORSOrigins := []string{}

if len(cfg.HTTP.CORS.AllowedOrigins) != 0 {
CORSOrigins = cfg.HTTP.CORS.AllowedOrigins
}

CORSMethods := []string{http.MethodGet, http.MethodHead, http.MethodPost}

if len(cfg.HTTP.CORS.AllowedMethods) != 0 {
CORSMethods = cfg.HTTP.CORS.AllowedMethods
}

h = handlers.CORS(handlers.AllowedHeaders(CORSHeaders),handlers.AllowedOrigins(CORSOrigins),handlers.AllowedMethods(CORSMethods))(h)
h = handlers.CORS(handlers.AllowedHeaders(cfg.HTTP.CORS.AllowedHeaders),
handlers.AllowedOrigins(cfg.HTTP.CORS.AllowedOrigins),
handlers.AllowedMethods(cfg.HTTP.CORS.AllowedMethods),
)(h)

return h
}

0 comments on commit 5907bfc

Please sign in to comment.