manually add preflight middleware
This should have already been solved by the rs/cors middleware, but
somehow it's not.
@@ -965,7 +965,9 @@ func setupMiddlewares(handler http.Handler) http.Handler {
// The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
// So this is a good place to plug in a panic handling middleware, logging and metrics
func setupGlobalMiddleware(handler http.Handler) http.Handler {
- handleCORS := cors.Default().Handler
+ handleCORS := cors.New(cors.Options{
+ OptionsPassthrough: true,
+ }).Handler
handler = handleCORS(handler)
if feature_flags.EnableDevUI {
@@ -973,7 +975,10 @@ func setupGlobalMiddleware(handler http.Handler) http.Handler {
handler = swagger_middleware.AddMiddleware([]byte(SwaggerJSON), handler)
}
- return addLogging(handler)
+ handler = addLogging(handler)
+ handler = addPreflight(handler)
+
+ return handler
}
func addLogging(next http.Handler) http.Handler {
@@ -985,6 +990,19 @@ func addLogging(next http.Handler) http.Handler {
})
}
+func addPreflight(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+
+ if r.Method == "OPTIONS" {
+ w.Header().Set("Access-Control-Allow-Origin", "*")
+ w.Header().Set("Access-Control-Allow-Methods", "*")
+ return
+ }
+
+ next.ServeHTTP(w, r)
+ })
+}
+
// This function loads the Contextionary database, and creates
// an in-memory database for the centroids of the classes / properties in the Schema.
func loadContextionary() {
