esway/internal/unix/handler.go

60 lines
932 B
Go
Raw Normal View History

2024-12-19 15:03:36 +08:00
package unix
import (
"context"
"fmt"
"time"
"esway/internal/log"
"esway/internal/opt"
)
type Handler struct {
Ctx context.Context
}
type (
AvailableReq struct{}
AvailableResp struct {
OK bool
Now time.Time
Start time.Time
Duration string
}
)
func (*Handler) Available(_ *AvailableReq, out *AvailableResp) error {
now := time.Now()
out.OK, out.Now = opt.OK, now
out.Start = opt.Start
out.Duration = fmt.Sprint(now.Sub(opt.Start))
return nil
}
type SettingReq struct {
Debug bool
}
type Resp[T any] struct {
Status uint32
Msg string
Data T
}
func (h *Handler) Setting(in *SettingReq, out *Resp[bool]) error {
opt.Locker.Lock()
defer opt.Locker.Unlock()
if in.Debug {
opt.Cfg.Debug = true
log.Info(h.Ctx, "set global debug[true]")
} else {
opt.Cfg.Debug = false
log.Info(h.Ctx, "set global debug[false]")
}
out.Status = 200
out.Msg = "操作成功"
return nil
}