Files
req/routing.go
T
what 6d0c01c21f feat: Router.Call 拆分出 CallWithUser,支持指定内部调用身份
新增 CallWithUser(r, u, code, params, opts...),内部转发请求时可以冒充成指定用户身份,而不是沿用外部传入 request 自带的身份,用于类似"查自己的信息不该受字段级角色权限限制"这种需要以系统用户身份发起内部调用的场景。Call 本身签名不变,不影响现有调用方。
2026-07-21 18:01:28 +08:00

142 lines
3.9 KiB
Go
Executable File

package req
import (
"net/http"
"github.com/go-chi/chi/v5"
)
type RouteMethod string
type RouteService string
type RouteParamType string
type RouteParamCategory string
type RouteCtx struct {
Name string
}
const (
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 (
// 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"
IMPORT RouteService = "import"
PRINT RouteService = "print"
LAYOUT RouteService = "layout"
GRID_LAYOUT RouteService = "grid-layout"
GRID_LAYOUT_FORM RouteService = "grid-layout-form"
ECHART RouteService = "echart"
APPROVAL RouteService = "approval"
SELECTOR RouteService = "selector"
LIST_DETAIL RouteService = "list-detail"
LIST_DATA_STORE RouteService = "list-data-store"
LIST_GRID_LAYOUT RouteService = "list-grid-layout"
LIST_CONDITION_LAYOUT RouteService = "list-condition-layout"
LIST_OPERATIONS_ACCESS RouteService = "list-operations-access"
)
const (
ReqString RouteParamType = "string"
ReqBool RouteParamType = "bool"
ReqNumber RouteParamType = "number"
ReqInteger RouteParamType = "integer"
ReqFloat RouteParamType = "float"
ReqJson RouteParamType = "json"
ReqArray RouteParamType = "array"
ReqAny RouteParamType = "any"
)
const (
ROUTER RouteParamCategory = "router"
HEADER RouteParamCategory = "header"
PARAM RouteParamCategory = "param"
)
func (k RouteCtx) String() string {
return k.Name
}
type RouteMiddleware interface {
HttpMiddleware(r Route) func(next http.Handler) http.Handler
}
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
GetUri() string
GetUris() []string
GetParamValues(*http.Request, ...RouteParam) (GlobalParams, error)
GetResource() Resource
GetService() RouteService
GetRoles() []string
MakeRequest(r *http.Request, params map[string]any) (*http.Request, error)
Version() string
}
type RouteParam interface {
GetCode() string
GetDataType() RouteParamType
GetCategory() RouteParamCategory
IsRequired() bool
InjectRequestToGlobalParams(*http.Request, GlobalParams) error
}
type RouteMatchOption func(opts *RouteMatches)
type RouteMatches struct {
methods []RouteMethod
services []RouteService
}
func (this RouteMatches) RouteMethods() []RouteMethod {
return this.methods
}
func (this RouteMatches) RouteServices() []RouteService {
return this.services
}
func RouteMatch[T RouteMethod | RouteService](v T) RouteMatchOption {
return func(opt *RouteMatches) {
switch value := any(v).(type) {
case RouteMethod:
opt.methods = append(opt.methods, value)
case RouteService:
opt.services = append(opt.services, value)
}
}
}