Skip to content

Commit fdeacc9

Browse files
committed
Fix cors headers for non-api endpoints
Previously, the API cors were applied to all endpoints. Now, each part of the application handles its own cors.
1 parent 2199de8 commit fdeacc9

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

internal/dev_server/dev_server.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,17 @@ func (c LDClient) RunServer(ctx context.Context, serverParams ServerParams) {
6666
r.Handle("/ui", http.RedirectHandler("/ui/", http.StatusMovedPermanently))
6767
r.PathPrefix("/ui/").Handler(http.StripPrefix("/ui/", ui.AssetHandler))
6868
sdk.BindRoutes(r)
69-
handler := api.HandlerFromMux(apiServer, r)
70-
handler = api.CorsHeadersWithConfig(serverParams.CorsEnabled, serverParams.CorsOrigin)(handler)
69+
70+
// Create API handler with CORS middleware applied only to API routes
71+
var apiMiddlewares []api.MiddlewareFunc
72+
if serverParams.CorsEnabled {
73+
apiMiddlewares = append(apiMiddlewares, api.CorsHeadersWithConfig(true, serverParams.CorsOrigin))
74+
}
75+
handler := api.HandlerWithOptions(apiServer, api.GorillaServerOptions{
76+
BaseRouter: r,
77+
Middlewares: apiMiddlewares,
78+
})
79+
7180
handler = handlers.CombinedLoggingHandler(os.Stdout, handler)
7281
handler = handlers.RecoveryHandler(handlers.PrintRecoveryStack(true))(handler)
7382

0 commit comments

Comments
 (0)