[feat] 新增 RouteParam 接口

This commit is contained in:
what 2024-02-01 15:54:40 +08:00
parent a3eaa24141
commit 64555b3678
2 changed files with 45 additions and 3 deletions

View File

@ -44,6 +44,9 @@ var (
ErrFileUpload = &Errno{Code: 10009, Msg: "文件上传错误"}
ErrFileNotFound = &Errno{Code: 10010, Msg: "文件不存在"}
ErrConfigureNotFound = &Errno{Code: 10011, Msg: "配置不存在"}
ErrConfigureInvalid = &Errno{Code: 10012, Msg: "配置参数无效"}
ErrParamRequired = &Errno{Code: 10013, Msg: "参数不存在"}
ErrParamInvalid = &Errno{Code: 10014, Msg: "参数无效"}
// service 错误
ErrServiceDecode = &Errno{Code: 10101, Msg: "解析 base62param 错误"}
@ -97,6 +100,8 @@ var (
ErrCaptchaExpired = &Errno{Code: 20604, Msg: "验证码已过期"}
ErrCaptchaSend = &Errno{Code: 20605, Msg: "验证码发送错误"}
ErrCaptchaInterval = &Errno{Code: 20606, Msg: "验证码获取失败, 请稍后再试"}
//
)
// 使用 错误码 和 error 创建新的 错误

View File

@ -7,13 +7,23 @@ import (
"github.com/gorilla/websocket"
)
type RouteMethod string
type RouteCategory string
type RouteService string
type RouteParamType string
type RouteParamCategory string
type RouteCtx struct {
Name string
}
const (
RouteMethod_GET RouteMethod = "GET"
RouteMethod_POST RouteMethod = "POST"
RouteMethod_PUT RouteMethod = "PUT"
RouteMethod_DELETE RouteMethod = "DELETE"
)
const (
RouteCategory_WS RouteCategory = "ws"
RouteCategory_GRPC RouteCategory = "grpc"
@ -45,10 +55,31 @@ const (
RouteService_LIST_OPERATIONS_ACCESS RouteService = "list-operations-access"
)
const (
RouteParam_STRING RouteParamType = "string"
RouteParam_BOOL RouteParamType = "bool"
RouteParam_NUMBER RouteParamType = "number"
RouteParam_INTEGER RouteParamType = "integer"
RouteParam_FLOAT RouteParamType = "float"
RouteParam_JSON RouteParamType = "json"
RouteParam_ARRAY RouteParamType = "array"
RouteParam_ANY RouteParamType = "any"
)
const (
RouteParamCategory_ROUTER RouteParamCategory = "router"
RouteParamCategory_HEADER RouteParamCategory = "header"
RouteParamCategory_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, category ...RouteCategory) (HttpResponse, error)
Get(uuid string, category ...RouteCategory) (Route, bool)
@ -57,12 +88,14 @@ type Router interface {
}
type Route interface {
RouteMiddleware
GetUuid() string
GetCode() string
GetPrimaryKey() string
GetUri() string
GetUris() []string
GetParamValues(*http.Request) GlobalParams
GetParamValues(*http.Request, ...RouteParam) (GlobalParams, error)
GetResource() Resource
GetCategory() RouteCategory
GetService() RouteService
@ -70,8 +103,12 @@ type Route interface {
MakeRequest(r *http.Request, params map[string]any) (*http.Request, error)
}
type RouteMiddleware interface {
HttpMiddleware(r Route) func(next http.Handler) http.Handler
type RouteParam interface {
GetCode() string
GetType() RouteParamType
GetCategory() RouteParamCategory
IsRequired() bool
InjectValueToGlobalParams(*http.Request, GlobalParams) error
}
type WsClientGroup string