API Server
需要设置HTTP
处理函数:
......
server.SetHandler(api.NewPrimary(cl, tlsConfig, &statusHandler{cl, nil, nil}, c.GlobalBool("debug"), c.Bool("cors")))
......
api.Server
结构体定义如下:
// Server is a Docker API server.
type Server struct {
hosts []string
tlsConfig *tls.Config
dispatcher *dispatcher
}
其中处理HTTP
请求的相关方法如下:
// Dispatcher is a meta http.Handler. It acts as an http.Handler and forwards
// requests to another http.Handler that can be changed at runtime.
type dispatcher struct {
handler http.Handler
}
// SetHandler changes the underlying handler.
func (d *dispatcher) SetHandler(handler http.Handler) {
d.handler = handler
}
// ServeHTTP forwards requests to the underlying handler.
func (d *dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if d.handler == nil {
httpError(w, "No dispatcher defined", http.StatusInternalServerError)
return
}
d.handler.ServeHTTP(w, r)
}
// SetHandler is used to overwrite the HTTP handler for the API.
// This can be the api router or a reverse proxy.
func (s *Server) SetHandler(handler http.Handler) {
s.dispatcher.SetHandler(handler)
}
Server.SetHandler
所做的就是把handler
赋给Server.dispatcher.handler
。handler
类型是http.Handler:
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
api.NewPrimary
利用了mux这个project
:
// NewPrimary creates a new API router.
func NewPrimary(cluster cluster.Cluster, tlsConfig *tls.Config, status StatusHandler, debug, enableCors bool) *mux.Router {
// Register the API events handler in the cluster.
eventsHandler := newEventsHandler()
cluster.RegisterEventHandler(eventsHandler)
context := &context{
cluster: cluster,
eventsHandler: eventsHandler,
statusHandler: status,
tlsConfig: tlsConfig,
}
r := mux.NewRouter()
setupPrimaryRouter(r, context, enableCors)
if debug {
profilerSetup(r, "/debug/")
}
return r
}
而*mux.Router
实现了ServeHTTP
这个方法,所以符合http.Handler
这个interface
:
// ServeHTTP dispatches the handler registered in the matched route.
//
// When there is a match, the route variables can be retrieved calling
// mux.Vars(request).
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
......
}
api.Server
的ListenAndServe()
函数定义了新的http.Server
变量,利用上面实现的api.Server.dispatcher
的ServeHTTP
方法来响应HTTP
请求:
server = &http.Server{
Addr: protoAddrParts[1],
Handler: s.dispatcher,
}
再看一下setupPrimaryRouter
这个函数:
func setupPrimaryRouter(r *mux.Router, context *context, enableCors bool) {
for method, mappings := range routes {
for route, fct := range mappings {
log.WithFields(log.Fields{"method": method, "route": route}).Debug("Registering HTTP route")
localRoute := route
localFct := fct
wrap := func(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{"method": r.Method, "uri": r.RequestURI}).Debug("HTTP request received")
if enableCors {
writeCorsHeaders(w, r)
}
context.apiVersion = mux.Vars(r)["version"]
localFct(context, w, r)
}
localMethod := method
r.Path("/v{version:[0-9]+.[0-9]+}" + localRoute).Methods(localMethod).HandlerFunc(wrap)
r.Path(localRoute).Methods(localMethod).HandlerFunc(wrap)
if enableCors {
optionsMethod := "OPTIONS"
optionsFct := optionsHandler
wrap := func(w http.ResponseWriter, r *http.Request) {
log.WithFields(log.Fields{"method": optionsMethod, "uri": r.RequestURI}).
Debug("HTTP request received")
if enableCors {
writeCorsHeaders(w, r)
}
context.apiVersion = mux.Vars(r)["version"]
optionsFct(context, w, r)
}
r.Path("/v{version:[0-9]+.[0-9]+}" + localRoute).
Methods(optionsMethod).HandlerFunc(wrap)
r.Path(localRoute).Methods(optionsMethod).
HandlerFunc(wrap)
}
}
}
}
其中routes
定义如下:
var routes = map[string]map[string]handler{
"HEAD": {
"/containers/{name:.*}/archive": proxyContainer,
},
"GET": {
"/_ping": ping,
"/events": getEvents,
"/info": getInfo,
......
}
......
}
以
"HEAD": {
"/containers/{name:.*}/archive": proxyContainer,
}
为例:method
和localMethod
是"HEAD"
;mappings
是:
{
"/containers/{name:.*}/archive": proxyContainer,
}
route
和localRoute
是"/containers/{name:.*}/archive"
;fct
和localFct
是proxyContainer
。
所以
r.Path("/v{version:[0-9]+.[0-9]+}" + localRoute).Methods(localMethod).HandlerFunc(wrap)
r.Path(localRoute).Methods(localMethod).HandlerFunc(wrap)
就是为"/v*/containers/{name:.*}/archive"
和"/containers/{name:.*}/archive"
这个PATH
的"HEAD"
操作注册了wrap
函数,而wrap
函数则封装了localFct
,也就是proxyContainer
函数。 另外,if enableCors
部分与上述类似。