代码拉取完成,页面将自动刷新
package hwf
import (
"strings"
"sync"
)
const (
MethodGet = "GET"
MethodPost = "POST"
MethodPut = "PUT"
MethodDelete = "DELETE"
MethodAny = "ANY"
)
type HandlerFunc func(*ReqCtx)
type Router interface {
GET(path string, h HandlerFunc)
POST(path string, h HandlerFunc)
PUT(path string, h HandlerFunc)
DELETE(path string, h HandlerFunc)
Any(path string, h HandlerFunc)
Use(mw ...HandlerFunc)
Group(prefix string) Router
Static(prefix, dir string)
File(path, file string)
}
type RouterEngine struct {
prefix string
middlewares []HandlerFunc
routeMap map[string]map[string]HandlerFunc // path -> method -> handler
pathMW map[string][]HandlerFunc // path -> middleware chain snapshot
static map[string]string // prefix -> dir
files map[string]string // path -> file
prefixMW map[string][]HandlerFunc // prefix -> middleware snapshot
mutex sync.RWMutex // protects routeMap for concurrent access
}
// setupRouter initializes the global router engine.
func NewRouter() *RouterEngine {
return &RouterEngine{
routeMap: map[string]map[string]HandlerFunc{},
pathMW: map[string][]HandlerFunc{},
static: map[string]string{},
files: map[string]string{},
prefixMW: map[string][]HandlerFunc{},
mutex: sync.RWMutex{},
}
}
// Use adds middleware functions to the router. Middleware will be executed in the order they are added.
func (e *RouterEngine) Use(mw ...HandlerFunc) { e.middlewares = append(e.middlewares, mw...) }
// Group creates a new router group with the given prefix. All routes registered in the group
// will have the prefix prepended to their paths. The group inherits middleware from the parent router.
func (e *RouterEngine) Group(prefix string) Router {
return &RouterEngine{
prefix: e.prefix + prefix,
middlewares: e.middlewares,
routeMap: e.routeMap,
pathMW: e.pathMW,
static: e.static,
files: e.files,
prefixMW: e.prefixMW,
}
}
// GET registers a GET route handler.
func (e *RouterEngine) GET(path string, h HandlerFunc) {
e.add(MethodGet, path, h)
}
// POST registers a POST route handler.
func (e *RouterEngine) POST(path string, h HandlerFunc) {
e.add(MethodPost, path, h)
}
// PUT registers a PUT route handler.
func (e *RouterEngine) PUT(path string, h HandlerFunc) {
e.add(MethodPut, path, h)
}
// DELETE registers a DELETE route handler.
func (e *RouterEngine) DELETE(path string, h HandlerFunc) {
e.add(MethodDelete, path, h)
}
// Any registers a route handler that matches all HTTP methods.
func (e *RouterEngine) Any(path string, h HandlerFunc) {
e.add(MethodAny, path, h)
}
func (e *RouterEngine) add(method, path string, h HandlerFunc) {
full := e.prefix + path
e.mutex.Lock()
defer e.mutex.Unlock()
if _, ok := e.routeMap[full]; !ok {
e.routeMap[full] = map[string]HandlerFunc{}
}
e.routeMap[full][method] = h
// snapshot middleware chain for this path (only set once)
if _, ok := e.pathMW[full]; !ok {
chain := make([]HandlerFunc, len(e.middlewares))
copy(chain, e.middlewares)
e.pathMW[full] = chain
}
}
// Static serves static files from the given directory at the specified prefix.
func (e *RouterEngine) Static(prefix, dir string) {
if !strings.HasPrefix(prefix, "/") {
prefix = "/" + prefix
}
// ensure trailing slash for mux prefix matching
if !strings.HasSuffix(prefix, "/") {
prefix += "/"
}
e.static[prefix] = dir
chain := make([]HandlerFunc, len(e.middlewares))
copy(chain, e.middlewares)
e.prefixMW[prefix] = chain
}
// File serves a single file at the given path.
func (e *RouterEngine) File(path, file string) {
full := e.prefix + path
e.files[full] = file
chain := make([]HandlerFunc, len(e.middlewares))
copy(chain, e.middlewares)
e.pathMW[full] = chain
}
// ==================== Test Helpers ====================
// GetRouteMap returns the route map for testing purposes
func (e *RouterEngine) GetRouteMap() map[string]map[string]HandlerFunc {
return e.routeMap
}
// GetPathMW returns the path middleware map for testing purposes
func (e *RouterEngine) GetPathMW() map[string][]HandlerFunc {
return e.pathMW
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。