diff --git a/api.go b/api.go index 85c06bb26b..24b6b7e4bf 100644 --- a/api.go +++ b/api.go @@ -931,7 +931,7 @@ func writeCorsHeaders(w http.ResponseWriter, r *http.Request) { w.Header().Add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS") } -func makeHttpHandler(srv *Server, logging bool, localMethod string, localRoute string, handlerFunc HttpApiFunc) http.HandlerFunc { +func makeHttpHandler(srv *Server, logging bool, localMethod string, localRoute string, handlerFunc HttpApiFunc, enableCors bool) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // log the request utils.Debugf("Calling %s %s", localMethod, localRoute) @@ -950,7 +950,7 @@ func makeHttpHandler(srv *Server, logging bool, localMethod string, localRoute s if err != nil { version = APIVERSION } - if srv.runtime.config.EnableCors { + if enableCors { writeCorsHeaders(w, r) } @@ -992,7 +992,7 @@ func AttachProfiler(router *mux.Router) { router.HandleFunc("/debug/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP) } -func createRouter(srv *Server, logging bool) (*mux.Router, error) { +func createRouter(srv *Server, logging, enableCors bool) (*mux.Router, error) { r := mux.NewRouter() if os.Getenv("DEBUG") != "" { AttachProfiler(r) @@ -1053,7 +1053,7 @@ func createRouter(srv *Server, logging bool) (*mux.Router, error) { localMethod := method // build the handler function - f := makeHttpHandler(srv, logging, localMethod, localRoute, localFct) + f := makeHttpHandler(srv, logging, localMethod, localRoute, localFct, enableCors) // add the new route if localRoute == "" { @@ -1072,7 +1072,7 @@ func createRouter(srv *Server, logging bool) (*mux.Router, error) { // FIXME: refactor this to be part of Server and not require re-creating a new // router each time. This requires first moving ListenAndServe into Server. func ServeRequest(srv *Server, apiversion float64, w http.ResponseWriter, req *http.Request) error { - router, err := createRouter(srv, false) + router, err := createRouter(srv, false, true) if err != nil { return err } @@ -1114,8 +1114,8 @@ func ServeFd(addr string, handle http.Handler) error { // ListenAndServe sets up the required http.Server and gets it listening for // each addr passed in and does protocol specific checking. -func ListenAndServe(proto, addr string, srv *Server, logging bool) error { - r, err := createRouter(srv, logging) +func ListenAndServe(proto, addr string, srv *Server, logging, enableCors bool) error { + r, err := createRouter(srv, logging, enableCors) if err != nil { return err } diff --git a/config.go b/config.go index a948744f0e..aad5d50fc0 100644 --- a/config.go +++ b/config.go @@ -10,7 +10,6 @@ type DaemonConfig struct { Pidfile string Root string AutoRestart bool - EnableCors bool Dns []string EnableIptables bool EnableIpForward bool @@ -29,7 +28,6 @@ func ConfigFromJob(job *engine.Job) *DaemonConfig { config.Pidfile = job.Getenv("Pidfile") config.Root = job.Getenv("Root") config.AutoRestart = job.GetenvBool("AutoRestart") - config.EnableCors = job.GetenvBool("EnableCors") if dns := job.GetenvList("Dns"); dns != nil { config.Dns = dns } diff --git a/docker/docker.go b/docker/docker.go index 6fb88fc7c6..e6c8076f36 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -86,7 +86,6 @@ func main() { job.Setenv("Pidfile", *pidfile) job.Setenv("Root", *flRoot) job.SetenvBool("AutoRestart", *flAutoRestart) - job.SetenvBool("EnableCors", *flEnableCors) job.SetenvList("Dns", flDns.GetAll()) job.SetenvBool("EnableIptables", *flEnableIptables) job.SetenvBool("EnableIpForward", *flEnableIpForward) @@ -102,6 +101,7 @@ func main() { // Serve api job = eng.Job("serveapi", flHosts.GetAll()...) job.SetenvBool("Logging", true) + job.SetenvBool("EnableCors", *flEnableCors) if err := job.Run(); err != nil { log.Fatal(err) } diff --git a/server.go b/server.go index 383284f259..87bab9ec5c 100644 --- a/server.go +++ b/server.go @@ -125,7 +125,7 @@ func (srv *Server) ListenAndServe(job *engine.Job) engine.Status { protoAddrParts := strings.SplitN(protoAddr, "://", 2) go func() { log.Printf("Listening for HTTP on %s (%s)\n", protoAddrParts[0], protoAddrParts[1]) - chErrors <- ListenAndServe(protoAddrParts[0], protoAddrParts[1], srv, job.GetenvBool("Logging")) + chErrors <- ListenAndServe(protoAddrParts[0], protoAddrParts[1], srv, job.GetenvBool("Logging"), job.GetenvBool("EnableCors")) }() }