From 64555b3678261cd3859b4b606a9833dcc1992823 Mon Sep 17 00:00:00 2001 From: what Date: Thu, 1 Feb 2024 15:54:40 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E6=96=B0=E5=A2=9E=20RouteParam=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- errno.go | 5 +++++ routing.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/errno.go b/errno.go index 7a3fd52..11cbcaa 100644 --- a/errno.go +++ b/errno.go @@ -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 创建新的 错误 diff --git a/routing.go b/routing.go index 31fd316..bc83715 100644 --- a/routing.go +++ b/routing.go @@ -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