From 47a74d113e622671682c473964b51383dc40f9d8 Mon Sep 17 00:00:00 2001 From: what Date: Tue, 30 Apr 2024 13:48:11 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=20=E5=9F=BA=E7=A1=80=20controller=20?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller.go | 106 ------------------------------------------------ http.go | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 106 deletions(-) diff --git a/controller.go b/controller.go index 9c58afb..a1a401f 100644 --- a/controller.go +++ b/controller.go @@ -1,8 +1,6 @@ package contracts import ( - "net/http" - "github.com/samber/do" ) @@ -15,59 +13,10 @@ type Controller interface { Dispatch(job string, payload any, u User) error } -type HttpController interface { - Controller - - // 获取用户信息 - User() User - - // 获取请求信息 - Request() *http.Request - - // 请求处理 - Execute(GlobalParams) any - - // 路由信息 - Route() Route - - // 内部调用 - Call(code string, params map[string]any, category ...RouteCategory) (HttpResponse, error) - // 内部调用 - // Invoke() - - // 数据库权限 - AuthDB() ResAuthDB -} - -type WsController interface { - HttpController - // 请求处理 - ExecuteWS(GlobalParams) error - // 获取 Ws 客户端 - WsClient() WsClient - // 获取 Ws 标识 - WsClientId(GlobalParams) WsClientID - // 获取 Ws 分组 - WsClientGroup(GlobalParams) WsClientGroup -} - type BaseController struct { container *do.Injector } -type HttpBaseController struct { - Controller - request *http.Request -} - -type WsBaseController struct { - HttpController - ws WsClient -} - -var defaultWsClientGroup WsClientGroup = "__DEFAULT__" -var wsClientID WsClientID = 0 - func (this BaseController) Container() *do.Injector { return this.container } @@ -80,61 +29,6 @@ func (this BaseController) Dispatch(job string, payload any, u User) error { return do.MustInvoke[JobService](this.Container()).Dispatch(job, payload, u) } -func (this HttpBaseController) Request() *http.Request { - return this.request -} - -func (this HttpBaseController) Route() Route { - return this.Request().Context().Value(RouteCtx{Name: "Route"}).(Route) -} - -func (this HttpBaseController) User() User { - return this.Request().Context().Value(RouteCtx{Name: "User"}).(User) -} - -func (this HttpBaseController) Call(code string, params map[string]any, category ...RouteCategory) (HttpResponse, error) { - return do.MustInvoke[Router](this.Container()).Call(this.Request(), code, params, category...) -} - -func (HttpBaseController) Execute(params GlobalParams) any { - return nil -} - -func (HttpBaseController) AuthDB() ResAuthDB { - return ResAuthOn -} - -func (WsBaseController) ExecuteWS(params GlobalParams) error { - return nil -} - -func (this WsBaseController) WsClient() WsClient { - return this.ws -} - -func (WsBaseController) WsClientId(GlobalParams) WsClientID { - wsClientID++ - return wsClientID -} - -func (WsBaseController) WsClientGroup(GlobalParams) WsClientGroup { - return defaultWsClientGroup -} - func NewBaseController(container *do.Injector) Controller { return &BaseController{container} } - -func NewHttpBaseController(container *do.Injector, request *http.Request) HttpController { - return &HttpBaseController{ - Controller: &BaseController{container}, - request: request, - } -} - -func NewWsBaseController(ws WsClient, ctr HttpController) WsController { - return &WsBaseController{ - HttpController: ctr, - ws: ws, - } -} diff --git a/http.go b/http.go index 9a09af8..05401a0 100644 --- a/http.go +++ b/http.go @@ -1,7 +1,117 @@ package contracts +import ( + "net/http" + + "github.com/samber/do" +) + +var defaultWsClientGroup WsClientGroup = "__DEFAULT__" +var wsClientID WsClientID = 0 + type Http interface { Start() error Stop() error Restart() error } + +type HttpController interface { + Controller + + // 获取用户信息 + User() User + + // 获取请求信息 + Request() *http.Request + + // 请求处理 + Execute(GlobalParams) any + + // 路由信息 + Route() Route + + // 内部调用 + Call(code string, params map[string]any, category ...RouteCategory) (HttpResponse, error) + // 内部调用 + // Invoke() + + // 数据库权限 + AuthDB() ResAuthDB +} + +type WsController interface { + HttpController + // 请求处理 + ExecuteWS(GlobalParams) error + // 获取 Ws 客户端 + WsClient() WsClient + // 获取 Ws 标识 + WsClientId(GlobalParams) WsClientID + // 获取 Ws 分组 + WsClientGroup(GlobalParams) WsClientGroup +} + +type HttpHandleController struct { + Controller + request *http.Request +} + +type WsHandleController struct { + HttpController + ws WsClient +} + +func (this HttpHandleController) Request() *http.Request { + return this.request +} + +func (this HttpHandleController) Route() Route { + return this.Request().Context().Value(RouteCtx{Name: "Route"}).(Route) +} + +func (this HttpHandleController) User() User { + return this.Request().Context().Value(RouteCtx{Name: "User"}).(User) +} + +func (this HttpHandleController) Call(code string, params map[string]any, category ...RouteCategory) (HttpResponse, error) { + return do.MustInvoke[Router](this.Container()).Call(this.Request(), code, params, category...) +} + +func (HttpHandleController) Execute(params GlobalParams) any { + return nil +} + +func (HttpHandleController) AuthDB() ResAuthDB { + return ResAuthOn +} + +func (WsHandleController) ExecuteWS(params GlobalParams) error { + return nil +} + +func (this WsHandleController) WsClient() WsClient { + return this.ws +} + +func (WsHandleController) WsClientId(GlobalParams) WsClientID { + wsClientID++ + return wsClientID +} + +func (WsHandleController) WsClientGroup(GlobalParams) WsClientGroup { + return defaultWsClientGroup +} + +func NewHttpController(container *do.Injector, request *http.Request) HttpController { + return &HttpHandleController{ + Controller: &BaseController{container}, + request: request, + } +} + +func NewWsController(ws WsClient, ctr HttpController) WsController { + return &WsHandleController{ + HttpController: ctr, + ws: ws, + } +}