contracts/controller.go

36 lines
708 B
Go
Raw Permalink Normal View History

2023-04-12 16:56:55 +08:00
package contracts
import (
2024-05-09 13:15:24 +08:00
"git.fsdpf.net/go/req"
2023-04-12 16:56:55 +08:00
"github.com/samber/do"
)
type Controller interface {
// 初始化
Init() error
// 获取 Container
2023-06-05 13:37:32 +08:00
Container() *do.Injector
// 派遣一个任务
2024-05-09 13:15:24 +08:00
Dispatch(job string, payload any, u req.User) error
2023-04-12 16:56:55 +08:00
}
type BaseController struct {
2023-06-05 13:37:32 +08:00
container *do.Injector
}
func (this BaseController) Container() *do.Injector {
return this.container
2023-04-12 16:56:55 +08:00
}
2023-07-13 09:35:04 +08:00
func (BaseController) Init() error {
return nil
}
2024-05-09 13:15:24 +08:00
func (this BaseController) Dispatch(job string, payload any, u req.User) error {
2023-06-05 16:52:03 +08:00
return do.MustInvoke[JobService](this.Container()).Dispatch(job, payload, u)
2023-04-12 16:56:55 +08:00
}
2023-06-05 14:46:25 +08:00
func NewBaseController(container *do.Injector) Controller {
return &BaseController{container}
}