-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
68 lines (54 loc) · 1.7 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
package router
import (
"github.com/photowey/fastrouter/api/apiconstant"
"github.com/photowey/fastrouter/api/dispatcher"
"github.com/photowey/fastrouter/api/filter"
"github.com/photowey/fastrouter/api/interceptor"
"github.com/photowey/fastrouter/api/request"
"github.com/valyala/fasthttp"
)
var (
_ Router = (*router)(nil)
_router *router
)
func init() {
_router = newRouter()
}
type Router interface {
route(rctx *request.Context, ctx *fasthttp.RequestCtx) error
}
type router struct {
handler dispatcher.Handler
}
func (r *router) route(rctx *request.Context, ctx *fasthttp.RequestCtx) error {
return _router.handler.Route(rctx, ctx)
}
func Register(method, path string, handler request.Handler) {
_router.handler.Register(method, path, handler)
}
func UnRegister(method, path string, handler request.Handler) error {
return _router.handler.UnRegister(method, path, handler)
}
func RegisterFilter(name string, filterx filter.Filter) {
_router.handler.RegisterFilter(name, filterx)
}
func UnRegisterFilter(name string, filterx filter.Filter) error {
return _router.handler.UnRegisterFilter(name, filterx)
}
func RegisterInterceptor(name string, interceptorx interceptor.Interceptor) {
_router.handler.RegisterInterceptor(name, interceptorx)
}
func UnRegisterInterceptor(name string, interceptorx interceptor.Interceptor) error {
return _router.handler.UnRegisterInterceptor(name, interceptorx)
}
func Route(ctx *fasthttp.RequestCtx) error {
ctx.SetContentType(apiconstant.ContentTypeApplicationJSON)
ctx.SetStatusCode(fasthttp.StatusOK)
rctx := request.NewRequestContext(ctx)
return _router.route(rctx, ctx)
}
func newRouter() *router {
return &router{
handler: dispatcher.NewHandler(),
}
}