This commit is contained in:
loveuer
2024-07-26 17:59:02 +08:00
parent d55c6b1932
commit eea192e385
14 changed files with 250 additions and 22 deletions

56
internal/unix/handler.go Normal file
View File

@ -0,0 +1,56 @@
package unix
import (
"context"
"fmt"
"time"
"ultone/internal/log"
"ultone/internal/opt"
)
type Handler struct {
Ctx context.Context
}
type AvailableReq struct{}
type 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.Debug = true
log.Info(h.Ctx, "set global debug[true]")
} else {
opt.Debug = false
log.Info(h.Ctx, "set global debug[false]")
}
out.Status = 200
out.Msg = "操作成功"
return nil
}

51
internal/unix/start.go Normal file
View File

@ -0,0 +1,51 @@
package unix
import (
"context"
"net"
"net/rpc"
"net/url"
"ultone/internal/log"
"ultone/internal/opt"
)
func Start(ctx context.Context) error {
ready := make(chan bool)
defer close(ready)
uri, err := url.Parse(opt.Cfg.Listen.Unix)
if err != nil {
return err
}
address := uri.Host + uri.Path
log.Debug(ctx, "[rpc-svc] listen at [%s] [%s]", uri.Scheme, address)
ln, err := net.Listen(uri.Scheme, address)
if err != nil {
return err
}
go func() {
ready <- true
<-ctx.Done()
_ = ln.Close()
}()
<-ready
svc := rpc.NewServer()
if err = svc.RegisterName("svc", &Handler{Ctx: ctx}); err != nil {
return err
}
go func() {
log.Info(ctx, "[rpc-svc] start at: [%s] [%s]", uri.Scheme, address)
ready <- true
svc.Accept(ln)
}()
<-ready
return nil
}