watcher/server/watcher_svc.go
timerzz 99ea10b74f
Some checks failed
Build image / build (push) Failing after 13s
feat 分离pusher
2024-05-21 16:42:45 +08:00

95 lines
2.0 KiB
Go

package server
import (
"github.com/gofiber/fiber/v3"
"github.com/pkg/errors"
"haitao_watcher/pkg/model"
"haitao_watcher/pkg/watcher"
"net/url"
)
type WatcherSvc struct {
ctl *watcher.Controller
}
func NewWatcherController(ctl *watcher.Controller) *WatcherSvc {
return &WatcherSvc{
ctl: ctl,
}
}
func (s *WatcherSvc) RegistryRouter(r fiber.Router) {
r.Get("watchers", s.ListWatcherInfo)
r.Post("watchers", s.CreateWatcher)
r.Delete("watchers/:uid", s.DeleteWatcher)
r.Delete("watchers/:uid/status", s.StopWatcher)
r.Post("watchers/:uid/status", s.StartWatcher)
}
func (s *WatcherSvc) ListWatcherInfo(ctx fiber.Ctx) error {
var req watcher.ListWatcherInfoRequest
if err := ctx.Bind().Query(&req); err != nil {
return err
}
resp, err := s.ctl.List(req)
if err != nil {
return err
}
return ctx.JSON(resp)
}
func (s *WatcherSvc) CreateWatcher(ctx fiber.Ctx) (err error) {
var cfg model.WatchInfo
if err = ctx.Bind().JSON(&cfg); err != nil {
return err
}
if cfg.Pid, err = url.QueryUnescape(cfg.Pid); err != nil {
return
}
return s.ctl.RunWatcher(cfg)
}
func (s *WatcherSvc) StopWatcher(ctx fiber.Ctx) (err error) {
uid, err := url.QueryUnescape(ctx.Params("uid"))
if err != nil {
return err
}
if uid == "" {
return errors.New("uid is empty")
}
return s.ctl.Stop(uid)
}
func (s *WatcherSvc) StartWatcher(ctx fiber.Ctx) (err error) {
uid, err := url.QueryUnescape(ctx.Params("uid"))
if err != nil {
return err
}
if uid == "" {
return errors.New("uid is empty")
}
return s.ctl.Start(uid)
}
func (s *WatcherSvc) DeleteWatcher(ctx fiber.Ctx) error {
uid, err := url.QueryUnescape(ctx.Params("uid"))
if err != nil {
return err
}
if uid == "" {
return errors.New("uid is empty")
}
return s.ctl.Delete(uid)
}
//func (s *WatcherSvc) SetWatcherPushers(ctx fiber.Ctx) (err error) {
// var pusherIds []uint
// if err = ctx.Bind().JSON(&pusherIds); err != nil {
// return
// }
// uid := ctx.Params("uid")
// if uid == "" {
// return errors.New("uid is empty")
// }
// return s.ctl.SetPushers(uid, pusherIds)
//}