-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
41 lines (33 loc) · 1.01 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package mix
import (
"fmt"
"github.com/gozelle/gin"
"github.com/gozelle/jsonrpc"
"github.com/gozelle/mix/middlewares/cors"
"strings"
)
func NewServer() *Server {
s := &Server{}
s.Engine = gin.New()
s.Engine.Use(Logger(), gin.Recovery(), cors.Cors())
return s
}
type Server struct {
*gin.Engine
}
func (s *Server) RegisterRPC(router gin.IRouter, namespace string, handler any, middlewares ...gin.HandlerFunc) {
rpcServer := jsonrpc.NewServer()
rpcServer.Register(namespace, handler)
router.POST("", append([]gin.HandlerFunc{gin.WrapH(rpcServer)}, middlewares...)...)
}
func (s *Server) RegisterAPI(router gin.IRouter, namespace string, handler any, middlewares ...gin.HandlerFunc) {
var path string
if strings.TrimSpace(namespace) == "" {
path = fmt.Sprintf("/:%s", method)
} else {
path = fmt.Sprintf("/:%s/:%s", module, method)
}
rpcServer := jsonrpc.NewServer()
rpcServer.Register(namespace, handler)
router.POST(path, append([]gin.HandlerFunc{wrapAPI(namespace, rpcServer)}, middlewares...)...)
}