[feat] 基础 controller 目录结构调整

This commit is contained in:
what 2024-04-30 13:48:11 +08:00
parent a4f032b496
commit 47a74d113e
2 changed files with 110 additions and 106 deletions

View File

@ -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,
}
}

110
http.go
View File

@ -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,
}
}