2023-04-12 16:56:55 +08:00
|
|
|
package contracts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/samber/do"
|
2023-05-22 13:26:49 +08:00
|
|
|
"google.golang.org/grpc"
|
2023-04-12 16:56:55 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Controller interface {
|
2023-06-05 13:37:32 +08:00
|
|
|
Container() *do.Injector
|
|
|
|
// 派遣一个任务
|
|
|
|
Dispatch(job string, payload any, u User) error
|
2023-04-12 16:56:55 +08:00
|
|
|
}
|
|
|
|
|
2023-05-22 13:26:49 +08:00
|
|
|
type GRpController interface {
|
2023-06-05 13:37:32 +08:00
|
|
|
Controller
|
|
|
|
// 获取 gRPC 服务的描述信息
|
2023-05-23 17:14:02 +08:00
|
|
|
GetGRpcServiceDesc() *grpc.ServiceDesc
|
2023-05-22 13:26:49 +08:00
|
|
|
}
|
|
|
|
|
2023-06-05 13:37:32 +08:00
|
|
|
type JobController interface {
|
|
|
|
Controller
|
|
|
|
// 任务处理
|
|
|
|
Handle(any) 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
type WsController interface {
|
|
|
|
HttpController
|
|
|
|
// 获取 Ws 客户端
|
|
|
|
WsClient() WsClient
|
|
|
|
// 获取 Ws 标识
|
|
|
|
WsClientId(GlobalParams) WsClientID
|
|
|
|
// 获取 Ws 分组
|
|
|
|
WsClientGroup(GlobalParams) WsClientGroup
|
|
|
|
}
|
|
|
|
|
2023-04-12 16:56:55 +08:00
|
|
|
type BaseController struct {
|
2023-06-05 13:37:32 +08:00
|
|
|
container *do.Injector
|
|
|
|
}
|
|
|
|
|
|
|
|
type GRpcBaseController struct {
|
|
|
|
Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
type JobBaseController struct {
|
|
|
|
Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
type HttpBaseController struct {
|
|
|
|
Controller
|
|
|
|
request *http.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
type WsBaseController struct {
|
|
|
|
HttpController
|
|
|
|
ws WsClient
|
2023-04-12 16:56:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var defaultWsClientGroup WsClientGroup = "__DEFAULT__"
|
|
|
|
var wsClientID WsClientID = 0
|
|
|
|
|
2023-06-05 13:37:32 +08:00
|
|
|
func (this BaseController) Container() *do.Injector {
|
|
|
|
return this.container
|
2023-04-12 16:56:55 +08:00
|
|
|
}
|
|
|
|
|
2023-06-05 13:37:32 +08:00
|
|
|
func (this BaseController) Dispatch(job string, payload any, u User) error {
|
|
|
|
return do.MustInvoke[Job](this.Container()).Dispatch(job, payload, u)
|
2023-04-12 16:56:55 +08:00
|
|
|
}
|
|
|
|
|
2023-06-05 13:37:32 +08:00
|
|
|
func NewHttpBaseController(container *do.Injector, request *http.Request) HttpController {
|
|
|
|
return &HttpBaseController{
|
|
|
|
Controller: &BaseController{container},
|
|
|
|
request: request,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-04-12 16:56:55 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:37:32 +08:00
|
|
|
func NewWsBaseController(ws WsClient, container *do.Injector, request *http.Request) WsController {
|
|
|
|
return &WsBaseController{
|
|
|
|
HttpController: NewHttpBaseController(container, request),
|
|
|
|
ws: ws,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this WsBaseController) WsClient() WsClient {
|
|
|
|
return this.ws
|
|
|
|
}
|
|
|
|
|
|
|
|
func (WsBaseController) WsClientId(GlobalParams) WsClientID {
|
2023-04-12 16:56:55 +08:00
|
|
|
wsClientID++
|
|
|
|
return wsClientID
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:37:32 +08:00
|
|
|
func (WsBaseController) WsClientGroup(GlobalParams) WsClientGroup {
|
2023-04-12 16:56:55 +08:00
|
|
|
return defaultWsClientGroup
|
|
|
|
}
|
|
|
|
|
2023-06-05 13:37:32 +08:00
|
|
|
func NewGRpBaseController(container *do.Injector) JobController {
|
|
|
|
return &JobBaseController{
|
|
|
|
Controller: &BaseController{container},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (JobBaseController) Handle(any) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewJobBaseController(container *do.Injector) GRpController {
|
|
|
|
return &GRpcBaseController{
|
|
|
|
Controller: &BaseController{container},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (GRpcBaseController) GetGRpcServiceDesc() *grpc.ServiceDesc {
|
|
|
|
return nil
|
2023-04-12 16:56:55 +08:00
|
|
|
}
|