mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-07-14 10:17:26 +08:00
Add cors only to routes that need it
This commit is contained in:
parent
0df09bebdb
commit
d1772aebe9
16
api.go
16
api.go
@ -47,6 +47,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
|
corsRouter := mux.NewRouter()
|
||||||
|
|
||||||
// Reverse proxy (needs to be the first route, to make sure it is the first thing we check)
|
// Reverse proxy (needs to be the first route, to make sure it is the first thing we check)
|
||||||
proxyHandler := handlers.NewMultipleHostReverseProxy()
|
proxyHandler := handlers.NewMultipleHostReverseProxy()
|
||||||
@ -60,13 +61,15 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
corsHandler := gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), gh.AllowedOrigins([]string{"*"}))
|
||||||
|
|
||||||
// Specific routes
|
// Specific routes
|
||||||
r.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-{port:[0-9]*}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
|
r.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}-{port:[0-9]*}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
|
||||||
r.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
|
r.Host(`{subdomain:.*}{node:pwd[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}_[0-9]{1,3}}.{tld:.*}`).HandlerFunc(proxyMultiplexer)
|
||||||
r.HandleFunc("/ping", handlers.Ping).Methods("GET")
|
r.HandleFunc("/ping", handlers.Ping).Methods("GET")
|
||||||
r.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET")
|
corsRouter.HandleFunc("/sessions/{sessionId}", handlers.GetSession).Methods("GET")
|
||||||
r.Handle("/sessions/{sessionId}/instances", http.HandlerFunc(handlers.NewInstance)).Methods("POST")
|
corsRouter.HandleFunc("/sessions/{sessionId}/instances", handlers.NewInstance).Methods("POST")
|
||||||
r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", handlers.DeleteInstance).Methods("DELETE")
|
corsRouter.HandleFunc("/sessions/{sessionId}/instances/{instanceName}", handlers.DeleteInstance).Methods("DELETE")
|
||||||
r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/keys", handlers.SetKeys).Methods("POST")
|
r.HandleFunc("/sessions/{sessionId}/instances/{instanceName}/keys", handlers.SetKeys).Methods("POST")
|
||||||
|
|
||||||
h := func(w http.ResponseWriter, r *http.Request) {
|
h := func(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -82,7 +85,7 @@ func main() {
|
|||||||
http.ServeFile(rw, r, "www/sdk.js")
|
http.ServeFile(rw, r, "www/sdk.js")
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Handle("/sessions/{sessionId}/ws/", server)
|
corsRouter.Handle("/sessions/{sessionId}/ws/", server)
|
||||||
r.Handle("/metrics", promhttp.Handler())
|
r.Handle("/metrics", promhttp.Handler())
|
||||||
|
|
||||||
// Generic routes
|
// Generic routes
|
||||||
@ -98,14 +101,15 @@ func main() {
|
|||||||
}
|
}
|
||||||
}).Methods("GET")
|
}).Methods("GET")
|
||||||
|
|
||||||
r.HandleFunc("/", handlers.NewSession).Methods("POST")
|
corsRouter.HandleFunc("/", handlers.NewSession).Methods("POST")
|
||||||
|
|
||||||
n := negroni.Classic()
|
n := negroni.Classic()
|
||||||
|
r.PathPrefix("/").Handler(negroni.New(negroni.Wrap(corsHandler(corsRouter))))
|
||||||
n.UseHandler(r)
|
n.UseHandler(r)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
log.Println("Listening on port " + config.PortNumber)
|
log.Println("Listening on port " + config.PortNumber)
|
||||||
log.Fatal(http.ListenAndServe("0.0.0.0:"+config.PortNumber, gh.CORS(gh.AllowCredentials(), gh.AllowedHeaders([]string{"x-requested-with", "content-type"}), gh.AllowedOrigins([]string{"*"}))(n)))
|
log.Fatal(http.ListenAndServe("0.0.0.0:"+config.PortNumber, n))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ssl := mux.NewRouter()
|
ssl := mux.NewRouter()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user