contracts/http.go

78 lines
1.6 KiB
Go

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.RouteMatchOption) (req.HttpResponse, error)
// 内部调用
// Invoke()
// 数据库权限
AuthDB() req.ResAuthDB
}
type HttpHandleController struct {
Controller
request *http.Request
}
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, opts ...req.RouteMatchOption) (req.HttpResponse, error) {
return do.MustInvoke[req.Router](this.Container()).Call(this.Request(), code, params, opts...)
}
func (HttpHandleController) Execute(params req.GlobalParams) any {
return nil
}
func (HttpHandleController) AuthDB() req.ResAuthDB {
return req.ResAuthOn
}
func NewHttpController(container *do.Injector, request *http.Request) HttpController {
return &HttpHandleController{
Controller: &BaseController{container},
request: request,
}
}