1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

remove enableCors from the config and move it as a job arg

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)
This commit is contained in:
Victor Vieux 2014-01-29 00:20:51 +00:00
parent fc697e3fd7
commit d41844ed2d
4 changed files with 9 additions and 11 deletions

14
api.go
View file

@ -931,7 +931,7 @@ func writeCorsHeaders(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS") 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) { return func(w http.ResponseWriter, r *http.Request) {
// log the request // log the request
utils.Debugf("Calling %s %s", localMethod, localRoute) utils.Debugf("Calling %s %s", localMethod, localRoute)
@ -950,7 +950,7 @@ func makeHttpHandler(srv *Server, logging bool, localMethod string, localRoute s
if err != nil { if err != nil {
version = APIVERSION version = APIVERSION
} }
if srv.runtime.config.EnableCors { if enableCors {
writeCorsHeaders(w, r) writeCorsHeaders(w, r)
} }
@ -992,7 +992,7 @@ func AttachProfiler(router *mux.Router) {
router.HandleFunc("/debug/pprof/threadcreate", pprof.Handler("threadcreate").ServeHTTP) 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() r := mux.NewRouter()
if os.Getenv("DEBUG") != "" { if os.Getenv("DEBUG") != "" {
AttachProfiler(r) AttachProfiler(r)
@ -1053,7 +1053,7 @@ func createRouter(srv *Server, logging bool) (*mux.Router, error) {
localMethod := method localMethod := method
// build the handler function // build the handler function
f := makeHttpHandler(srv, logging, localMethod, localRoute, localFct) f := makeHttpHandler(srv, logging, localMethod, localRoute, localFct, enableCors)
// add the new route // add the new route
if localRoute == "" { 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 // 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. // router each time. This requires first moving ListenAndServe into Server.
func ServeRequest(srv *Server, apiversion float64, w http.ResponseWriter, req *http.Request) error { 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 { if err != nil {
return err 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 // ListenAndServe sets up the required http.Server and gets it listening for
// each addr passed in and does protocol specific checking. // each addr passed in and does protocol specific checking.
func ListenAndServe(proto, addr string, srv *Server, logging bool) error { func ListenAndServe(proto, addr string, srv *Server, logging, enableCors bool) error {
r, err := createRouter(srv, logging) r, err := createRouter(srv, logging, enableCors)
if err != nil { if err != nil {
return err return err
} }

View file

@ -10,7 +10,6 @@ type DaemonConfig struct {
Pidfile string Pidfile string
Root string Root string
AutoRestart bool AutoRestart bool
EnableCors bool
Dns []string Dns []string
EnableIptables bool EnableIptables bool
EnableIpForward bool EnableIpForward bool
@ -29,7 +28,6 @@ func ConfigFromJob(job *engine.Job) *DaemonConfig {
config.Pidfile = job.Getenv("Pidfile") config.Pidfile = job.Getenv("Pidfile")
config.Root = job.Getenv("Root") config.Root = job.Getenv("Root")
config.AutoRestart = job.GetenvBool("AutoRestart") config.AutoRestart = job.GetenvBool("AutoRestart")
config.EnableCors = job.GetenvBool("EnableCors")
if dns := job.GetenvList("Dns"); dns != nil { if dns := job.GetenvList("Dns"); dns != nil {
config.Dns = dns config.Dns = dns
} }

View file

@ -86,7 +86,6 @@ func main() {
job.Setenv("Pidfile", *pidfile) job.Setenv("Pidfile", *pidfile)
job.Setenv("Root", *flRoot) job.Setenv("Root", *flRoot)
job.SetenvBool("AutoRestart", *flAutoRestart) job.SetenvBool("AutoRestart", *flAutoRestart)
job.SetenvBool("EnableCors", *flEnableCors)
job.SetenvList("Dns", flDns.GetAll()) job.SetenvList("Dns", flDns.GetAll())
job.SetenvBool("EnableIptables", *flEnableIptables) job.SetenvBool("EnableIptables", *flEnableIptables)
job.SetenvBool("EnableIpForward", *flEnableIpForward) job.SetenvBool("EnableIpForward", *flEnableIpForward)
@ -102,6 +101,7 @@ func main() {
// Serve api // Serve api
job = eng.Job("serveapi", flHosts.GetAll()...) job = eng.Job("serveapi", flHosts.GetAll()...)
job.SetenvBool("Logging", true) job.SetenvBool("Logging", true)
job.SetenvBool("EnableCors", *flEnableCors)
if err := job.Run(); err != nil { if err := job.Run(); err != nil {
log.Fatal(err) log.Fatal(err)
} }

View file

@ -125,7 +125,7 @@ func (srv *Server) ListenAndServe(job *engine.Job) engine.Status {
protoAddrParts := strings.SplitN(protoAddr, "://", 2) protoAddrParts := strings.SplitN(protoAddr, "://", 2)
go func() { go func() {
log.Printf("Listening for HTTP on %s (%s)\n", protoAddrParts[0], protoAddrParts[1]) 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"))
}() }()
} }