wip: 完成 client api 分析

This commit is contained in:
loveuer
2024-12-19 15:03:36 +08:00
commit 64cdd0cb0e
76 changed files with 5146 additions and 0 deletions

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

@ -0,0 +1,59 @@
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
}

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

@ -0,0 +1,52 @@
package unix
import (
"context"
"net"
"net/rpc"
"net/url"
"esway/internal/log"
"esway/internal/opt"
)
func Start(ctx context.Context) error {
ready := make(chan bool)
defer close(ready)
uri, err := url.Parse(opt.RpcAddress)
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
}