feat: Router.Call 拆分出 CallWithUser,支持指定内部调用身份
新增 CallWithUser(r, u, code, params, opts...),内部转发请求时可以冒充成指定用户身份,而不是沿用外部传入 request 自带的身份,用于类似"查自己的信息不该受字段级角色权限限制"这种需要以系统用户身份发起内部调用的场景。Call 本身签名不变,不影响现有调用方。
This commit is contained in:
+32
-25
@@ -7,7 +7,6 @@ import (
|
||||
)
|
||||
|
||||
type RouteMethod string
|
||||
type RouteCategory string
|
||||
type RouteService string
|
||||
type RouteParamType string
|
||||
type RouteParamCategory string
|
||||
@@ -17,25 +16,25 @@ type RouteCtx struct {
|
||||
}
|
||||
|
||||
const (
|
||||
GET RouteMethod = "GET"
|
||||
POST RouteMethod = "POST"
|
||||
PUT RouteMethod = "PUT"
|
||||
DELETE RouteMethod = "DELETE"
|
||||
GET RouteMethod = "GET"
|
||||
POST RouteMethod = "POST"
|
||||
PUT RouteMethod = "PUT"
|
||||
DELETE RouteMethod = "DELETE"
|
||||
WS RouteMethod = "WS"
|
||||
SSE RouteMethod = "SSE"
|
||||
GRPC RouteMethod = "GRPC"
|
||||
MCP RouteMethod = "MCP"
|
||||
STRUCTURE RouteMethod = "STRUCTURE"
|
||||
)
|
||||
|
||||
const (
|
||||
WS RouteCategory = "ws"
|
||||
GRPC RouteCategory = "grpc"
|
||||
FUNC RouteCategory = "func"
|
||||
SHOW RouteCategory = "show"
|
||||
QUERY RouteCategory = "query"
|
||||
STORE RouteCategory = "store"
|
||||
DESTROY RouteCategory = "destroy"
|
||||
STRUCTURE RouteCategory = "structure"
|
||||
)
|
||||
|
||||
const (
|
||||
API RouteService = "api"
|
||||
// resource_apis
|
||||
FUNC RouteService = "func"
|
||||
SHOW RouteService = "show"
|
||||
QUERY RouteService = "query"
|
||||
STORE RouteService = "store"
|
||||
DESTROY RouteService = "destroy"
|
||||
// system
|
||||
FSM RouteService = "fsm"
|
||||
LIST RouteService = "list"
|
||||
XLSX RouteService = "xlsx"
|
||||
@@ -81,12 +80,19 @@ type RouteMiddleware interface {
|
||||
|
||||
type Router interface {
|
||||
Call(r *http.Request, code string, params map[string]any, opts ...RouteMatchOption) (HttpResponse, error)
|
||||
// CallWithUser 与 Call 相同,但内部转发请求时冒充成 u 的身份,而不是沿用 r 自带的身份
|
||||
// (比如需要以系统用户身份发起内部调用)。
|
||||
CallWithUser(r *http.Request, u User, code string, params map[string]any, opts ...RouteMatchOption) (HttpResponse, error)
|
||||
Get(uuid string, opts ...RouteMatchOption) (Route, bool)
|
||||
Register(cr chi.Router)
|
||||
RefreshRoutes() error
|
||||
// Shutdown 关闭路由层管理的所有长连接(如 MCP SSE),
|
||||
// 应通过 http.Server.RegisterOnShutdown 注册,在 HTTP 服务关闭时调用。
|
||||
Shutdown()
|
||||
}
|
||||
|
||||
type Route interface {
|
||||
Method() RouteMethod
|
||||
GetUuid() string
|
||||
GetCode() string
|
||||
GetPrimaryKey() string
|
||||
@@ -94,10 +100,10 @@ type Route interface {
|
||||
GetUris() []string
|
||||
GetParamValues(*http.Request, ...RouteParam) (GlobalParams, error)
|
||||
GetResource() Resource
|
||||
GetCategory() RouteCategory
|
||||
GetService() RouteService
|
||||
GetRoles() []string
|
||||
MakeRequest(r *http.Request, params map[string]any) (*http.Request, error)
|
||||
Version() string
|
||||
}
|
||||
|
||||
type RouteParam interface {
|
||||
@@ -110,25 +116,26 @@ type RouteParam interface {
|
||||
|
||||
type RouteMatchOption func(opts *RouteMatches)
|
||||
type RouteMatches struct {
|
||||
categories []RouteCategory
|
||||
services []RouteService
|
||||
methods []RouteMethod
|
||||
services []RouteService
|
||||
}
|
||||
|
||||
func (this RouteMatches) RouteCategories() []RouteCategory {
|
||||
return this.categories
|
||||
func (this RouteMatches) RouteMethods() []RouteMethod {
|
||||
return this.methods
|
||||
}
|
||||
|
||||
func (this RouteMatches) RouteServices() []RouteService {
|
||||
return this.services
|
||||
}
|
||||
|
||||
func RouteMatch[T RouteCategory | RouteService](v T) RouteMatchOption {
|
||||
func RouteMatch[T RouteMethod | RouteService](v T) RouteMatchOption {
|
||||
return func(opt *RouteMatches) {
|
||||
switch value := any(v).(type) {
|
||||
case RouteCategory:
|
||||
opt.categories = append(opt.categories, value)
|
||||
case RouteMethod:
|
||||
opt.methods = append(opt.methods, value)
|
||||
case RouteService:
|
||||
opt.services = append(opt.services, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user