代码拉取完成,页面将自动刷新
package httpserver
import (
"fmt"
"log/slog"
"net/http"
)
// router 定义路由器的数据类型
type router map[string]HandleFunc
// addRoute 添加路由处理器到路由器中
func (own router) addRoute(method, path string, handler HandleFunc) {
key := method + "-" + path
if _, ok := own[key]; ok {
slog.Error(fmt.Sprintf("Add route failed: %s - %s already exist", method, path))
return
}
slog.Info(fmt.Sprintf("Add route success: %s - %s", method, path))
own[key] = handler
}
// handleContext 处理HTTP请求
func (own router) handleContext(ctx *Context) {
key := ctx.Method + "-" + ctx.Path
handler, ok := own[key]
if !ok {
if ctx.staticMiddleware != nil {
handler = ctx.staticMiddleware
} else {
handler = func(ctx *Context) {
ctx.ResponseString(http.StatusNotFound, ERR_NOT_FOUND)
}
}
}
// 将请求路由对应的处理器添加到上下文的中间件中,并最后一个执行
ctx.middlewares = append(ctx.middlewares, handler)
ctx.RunNext()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。