7.09 Middleware总结和面试
PART1. Filter要不要考虑时机的问题?
// serve 查找路由树并执行命中的业务逻辑
func (s *HTTPServer) serve(ctx *Context) {
// 检测并执行 beforeRoute 的filter
method := ctx.Req.Method
path := ctx.Req.URL.Path
targetNode, ok := s.findRoute(method, path)
// 检测并执行 afterRoute 的filter
// 没有在路由树中找到对应的路由节点 或 找到了路由节点的处理函数为空(即NPE:none pointer exception 的问题)
// 则返回404
if !ok || targetNode.node.HandleFunc == nil {
ctx.RespStatusCode = http.StatusNotFound
ctx.RespData = []byte("Not Found")
return
}
// 命中节点则将路径参数名值对设置到上下文中
ctx.PathParams = targetNode.pathParams
// 命中节点则将节点的路由设置到上下文中
ctx.MatchRoute = targetNode.node.route
// 检测并执行 beforeExecute 的filter
// 执行路由节点的处理函数
targetNode.node.HandleFunc(ctx)
// 检测并执行 afterExecute 的filter
}
PART2. Middleware要不要考虑顺序问题?





PART3. Middleware要不要考虑分路由问题?
PART4. 面试要点
Last updated