package contracts import ( "net/http" "git.fsdpf.net/go/req" "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() req.User // 获取请求信息 Request() *http.Request // 请求处理 Execute(req.GlobalParams) any // 路由信息 Route() req.Route // 内部调用 Call(code string, params map[string]any, category ...req.RouteCategory) (req.HttpResponse, error) // 内部调用 // Invoke() // 数据库权限 AuthDB() req.ResAuthDB } type WsController interface { HttpController // 请求处理 ExecuteWS(req.GlobalParams) error // 获取 Ws 客户端 WsClient() WsClient // 获取 Ws 标识 WsClientId(req.GlobalParams) WsClientID // 获取 Ws 分组 WsClientGroup(req.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() req.Route { return this.Request().Context().Value(req.RouteCtx{Name: "Route"}).(req.Route) } func (this HttpHandleController) User() req.User { return this.Request().Context().Value(req.RouteCtx{Name: "User"}).(req.User) } func (this HttpHandleController) Call(code string, params map[string]any, category ...req.RouteCategory) (req.HttpResponse, error) { return do.MustInvoke[req.Router](this.Container()).Call(this.Request(), code, params, category...) } func (HttpHandleController) Execute(params req.GlobalParams) any { return nil } func (HttpHandleController) AuthDB() req.ResAuthDB { return req.ResAuthOn } func (WsHandleController) ExecuteWS(params req.GlobalParams) error { return nil } func (this WsHandleController) WsClient() WsClient { return this.ws } func (WsHandleController) WsClientId(req.GlobalParams) WsClientID { wsClientID++ return wsClientID } func (WsHandleController) WsClientGroup(req.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, } }