package meta import ( "sync" ) // ============================== 路由元数据 ============================== // RouteMeta 路由元数据 // // 由请求结构体内嵌 gmeta.Meta 的扩展标签在注册时反射解析得出, // 权限校验、操作日志等横切能力按此元数据统一处理。 type RouteMeta struct { Method string `json:"method"` // 请求方法:GET/POST/PUT/DELETE Path string `json:"path"` // 路由模板,如 /api/user/detail/:id Summary string `json:"summary"` // 接口摘要(OpenAPI) Permission string `json:"permission"` // 权限节点 sys:user:page;空表示登录即可 Log string `json:"log"` // 操作日志标题;空表示不记录 LogType string `json:"logType"` // 日志类型:query/add/update/delete/import/export/reset Public bool `json:"public"` // 是否免登录(登录、验证码等) } // CtxKey 路由元数据写入请求上下文的键 // // 由绑定层写入,操作日志中间件读取 var CtxKey = struct{ name string }{"routeMeta"} // store 路由元数据存储 // 使用读写锁保护并发访问 type store struct { mu sync.RWMutex meta map[string]RouteMeta // key: "GET /api/user/page" } // s 全局路由元数据存储实例 var s = &store{meta: make(map[string]RouteMeta)} // key 生成存储键 func key(method, path string) string { return method + " " + path } // Register 注册路由元数据 // // 由绑定层反射时调用 func Register(m RouteMeta) { s.mu.Lock() defer s.mu.Unlock() s.meta[key(m.Method, m.Path)] = m } // Lookup 查询路由元数据 func Lookup(method, path string) (RouteMeta, bool) { s.mu.RLock() defer s.mu.RUnlock() m, ok := s.meta[key(method, path)] return m, ok } // All 返回全部路由元数据 // // 供调试与文档生成使用 func All() map[string]RouteMeta { s.mu.RLock() defer s.mu.RUnlock() out := make(map[string]RouteMeta, len(s.meta)) for k, v := range s.meta { out[k] = v } return out }