代码拉取完成,页面将自动刷新
package httpserver
import (
"context"
"fmt"
"gitee.com/scottq/go-framework/src/utils"
"github.com/julienschmidt/httprouter"
"net/http"
"strings"
)
type Router struct {
Path string
Method string
RouteHandler HttpRouteFunc
Middleware []Middleware
HttpHandler http.Handler
}
func (r *Router) hash() string {
return r.Method + "_" + r.Path
}
func (r *Router) invokeRegister(router *httprouter.Router, c *ServerConfig) {
handler := r.handler(c)
if handler != nil {
method := strings.ToUpper(r.Method)
switch method {
case "GET":
router.GET(r.Path, handler)
case "POST":
router.POST(r.Path, handler)
case "OPTIONS":
router.OPTIONS(r.Path, handler)
case "DELETE":
router.DELETE(r.Path, handler)
case "PUT":
router.PUT(r.Path, handler)
default:
fmt.Printf("not support router method:%s\n", method)
}
}
}
func (r *Router) handler(c *ServerConfig) func(http.ResponseWriter, *http.Request, httprouter.Params) {
genCtx := func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) *Ctx {
ctx := NewCtx(writer, request)
ctx._setRouterParams(params)
if c.requestId {
ctx.httpContext = context.WithValue(ctx.httpContext, c.requestIdField, utils.NewRequestId())
}
return ctx
}
if r.HttpHandler != nil {
handler := handlerMiddleware(func(ctx *Ctx) {
r.HttpHandler.ServeHTTP(ctx.Writer(), ctx.Request())
}, r.Middleware)
return func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
ctx := genCtx(writer, request, params)
handler(ctx)
}
}
if r.RouteHandler != nil {
handler := handlerMiddleware(r.RouteHandler, r.Middleware)
return func(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
ctx := genCtx(writer, request, params)
handler(ctx)
}
}
return nil
}
type HttpRouteFunc = func(ctx *Ctx)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。