1 Star 0 Fork 0

hwfo/hwf

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
router.go 4.11 KB
一键复制 编辑 原始数据 按行查看 历史
haitgo 提交于 2025-12-27 17:04 +08:00 . 完善token中间件功能
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hwfo/hwf.git
git@gitee.com:hwfo/hwf.git
hwfo
hwf
hwf
ba98abdfc0af

搜索帮助