-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
57 lines (44 loc) · 931 Bytes
/
router.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package jrpc
import (
"context"
"log/slog"
)
type Option func(*handler)
func DontRender(h *handler) {
h.dontRender = true
}
type Router struct {
path string
engine *engine
}
func NewRouter(logger ...*slog.Logger) *Router {
return &Router{
engine: newEngine(logger...),
}
}
func (r *Router) Group(method string) *Router {
if r.path == "" {
return &Router{
path: method,
engine: r.engine,
}
}
return &Router{
path: r.path + "." + method,
engine: r.engine,
}
}
func (r *Router) Method(method string, handlerFunc func(ctx context.Context) (any, error), opts ...Option) {
h := &handler{handlerFunc: handlerFunc}
for _, opt := range opts {
opt(h)
}
if r.path == "" {
r.engine.handleMethod(method, h)
return
}
r.engine.handleMethod(r.path+"."+method, h)
}
func (r *Router) Handle(ctx context.Context, jsonRPCRequest []byte) []byte {
return r.engine.handle(ctx, jsonRPCRequest)
}