-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
123 lines (104 loc) · 3.71 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package web
import (
"github.com/procyon-projects/goo"
context "github.com/procyon-projects/procyon-context"
"github.com/valyala/fasthttp"
"sync"
)
type Router interface {
Route(requestCtx *fasthttp.RequestCtx)
}
type ProcyonRouter struct {
ctx context.ConfigurableApplicationContext
handlerMapping HandlerMapping
requestContextPool *sync.Pool
generateContextId bool
recoveryActive bool
errorHandlerManager *errorHandlerManager
validator Validator
requestBinder RequestBinder
responseBodyWriter ResponseBodyWriter
}
func newProcyonRouterForBenchmark(context context.ConfigurableApplicationContext, handlerRegistry SimpleHandlerRegistry) *ProcyonRouter {
router := &ProcyonRouter{
ctx: context,
}
router.requestContextPool = &sync.Pool{
New: router.newWebRequestContext,
}
router.handlerMapping = NewRequestHandlerMapping(NewRequestMappingRegistry(), nil)
registryMap := handlerRegistry.getRegistryMap()
for _, handlers := range registryMap {
for _, handler := range handlers {
router.handlerMapping.RegisterHandlerMethod(handler.Path, handler.Method, handler.HandlerFunc, nil)
}
}
return router
}
func NewProcyonRouter(context context.ConfigurableApplicationContext) *ProcyonRouter {
router := &ProcyonRouter{
ctx: context,
generateContextId: true,
recoveryActive: true,
validator: newDefaultValidator(),
requestBinder: newDefaultRequestBinder(),
responseBodyWriter: newDefaultResponseBodyWriter(),
}
router.requestContextPool = &sync.Pool{
New: router.newWebRequestContext,
}
router.configure()
return router
}
func (router *ProcyonRouter) newWebRequestContext() interface{} {
requestContext := &WebRequestContext{
router: router,
handlerIndex: 0,
valueMap: make(map[string]interface{}),
}
return requestContext
}
func (router *ProcyonRouter) configure() {
peaFactory := router.ctx.GetPeaFactory()
handlerAdapter := peaFactory.GetSharedPeaType(goo.GetType((*HandlerMapping)(nil)))
router.handlerMapping = handlerAdapter.(HandlerMapping)
// custom logger
router.errorHandlerManager = newErrorHandlerManager(router.ctx.GetLogger())
errorHandler, _ := peaFactory.GetPeaByType(goo.GetType((*ErrorHandler)(nil)))
if errorHandler != nil {
router.errorHandlerManager.customErrorHandler = errorHandler.(ErrorHandler)
}
// custom validator
customValidator, _ := peaFactory.GetPeaByType(goo.GetType((*Validator)(nil)))
if customValidator != nil {
router.validator = customValidator.(Validator)
}
// custom request binder
customRequestBinder, _ := peaFactory.GetPeaByType(goo.GetType((*RequestBinder)(nil)))
if customRequestBinder != nil {
router.requestBinder = customRequestBinder.(RequestBinder)
}
// custom response body writer
customResponseBodyWriter, _ := peaFactory.GetPeaByType(goo.GetType((*ResponseBodyWriter)(nil)))
if customResponseBodyWriter != nil {
router.responseBodyWriter = customResponseBodyWriter.(ResponseBodyWriter)
}
}
func (router *ProcyonRouter) Route(requestCtx *fasthttp.RequestCtx) {
requestContext := router.requestContextPool.Get().(*WebRequestContext)
requestContext.fastHttpRequestContext = requestCtx
// prepare the context
requestContext.prepare(router.generateContextId)
// get handler chain and call all handlers
router.handlerMapping.GetHandlerChain(requestContext)
if requestContext.handlerChain == nil {
router.ctx.GetLogger().Warning(requestContext, "Handler not found : "+string(requestCtx.Path()))
router.errorHandlerManager.HandleError(HttpErrorNotFound, requestContext)
requestContext.reset()
router.requestContextPool.Put(requestContext)
return
}
requestContext.invoke()
requestContext.reset()
router.requestContextPool.Put(requestContext)
}