refactor rpc skills and clean api tests

This commit is contained in:
lpf
2026-03-15 14:23:01 +08:00
parent 2f124ec5b3
commit 617f7cc0f1
12 changed files with 1909 additions and 1661 deletions

View File

@@ -34,6 +34,10 @@ func (s *Server) handleCronRPC(w http.ResponseWriter, r *http.Request) {
s.handleRPC(w, r, s.cronRPCRegistry())
}
func (s *Server) handleSkillsRPC(w http.ResponseWriter, r *http.Request) {
s.handleRPC(w, r, s.skillsRPCRegistry())
}
func (s *Server) handleRPC(w http.ResponseWriter, r *http.Request, registry *rpcpkg.Registry) {
if !s.checkAuth(r) {
http.Error(w, "unauthorized", http.StatusUnauthorized)
@@ -258,6 +262,31 @@ func (s *Server) cronRPCRegistry() *rpcpkg.Registry {
return s.cronRPCReg
}
func (s *Server) buildSkillsRegistry() *rpcpkg.Registry {
svc := s.skillsRPCService()
reg := rpcpkg.NewRegistry()
rpcpkg.RegisterJSON(reg, "skills.view", func(ctx context.Context, req rpcpkg.SkillsViewRequest) (interface{}, *rpcpkg.Error) {
return svc.View(ctx, req)
})
rpcpkg.RegisterJSON(reg, "skills.mutate", func(ctx context.Context, req rpcpkg.SkillsMutateRequest) (interface{}, *rpcpkg.Error) {
return svc.Mutate(ctx, req)
})
return reg
}
func (s *Server) skillsRPCRegistry() *rpcpkg.Registry {
if s == nil {
return rpcpkg.NewRegistry()
}
s.skillsRPCOnce.Do(func() {
s.skillsRPCReg = s.buildSkillsRegistry()
})
if s.skillsRPCReg == nil {
return rpcpkg.NewRegistry()
}
return s.skillsRPCReg
}
func writeRPCError(w http.ResponseWriter, status int, requestID string, rpcErr *rpcpkg.Error) {
if rpcErr == nil {
rpcErr = rpcError("internal", "rpc error", nil, false)