mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-04-27 14:07:29 +08:00
refactor rpc skills and clean api tests
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user