分享
  1. 首页
  2. 文章

Docker Swarm代码分析笔记(8)——创建Docker API router

肖楠 · · 1303 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

上一篇提到的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.handlerhandler类型是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.ServerListenAndServe()函数定义了新的http.Server变量,利用上面实现的api.Server.dispatcherServeHTTP方法来响应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,
}

为例:
methodlocalMethod"HEAD";
mappings是:

{
 "/containers/{name:.*}/archive": proxyContainer,
}

routelocalRoute"/containers/{name:.*}/archive";
fctlocalFctproxyContainer
所以

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部分与上述类似。


有疑问加站长微信联系(非本文作者)

本文来自:nanxiao

感谢作者:肖楠

查看原文:Docker Swarm代码分析笔记(8)——创建Docker API router

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
1303 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏