|
1 | 1 | package godzilla |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "log" |
| 5 | + "strings" |
4 | 6 | "sync" |
5 | 7 |
|
6 | 8 | "github.com/valyala/fasthttp" |
@@ -106,3 +108,94 @@ func (r *router) allowed(reqMethod, path string, ctx *context) string { |
106 | 108 | } |
107 | 109 | return allow |
108 | 110 | } |
| 111 | + |
| 112 | +func (r *router) Handler(fctx *fasthttp.RequestCtx) { |
| 113 | + context := r.acquireCtx(fctx) |
| 114 | + defer r.releaseCtx(context) |
| 115 | + |
| 116 | + if r.settings.AutoRecover { |
| 117 | + defer func(fctx *fasthttp.RequestCtx) { |
| 118 | + if rcv := recover(); rcv != nil { |
| 119 | + log.Printf("recovered from error: %v", rcv) |
| 120 | + fctx.Error(fasthttp.StatusMessage(fasthttp.StatusInternalServerError), |
| 121 | + fasthttp.StatusInternalServerError) |
| 122 | + } |
| 123 | + }(fctx) |
| 124 | + } |
| 125 | + |
| 126 | + path := GetString(fctx.URI().PathOriginal()) |
| 127 | + |
| 128 | + if r.settings.CaseInSensitive { |
| 129 | + path = strings.ToLower(path) |
| 130 | + } |
| 131 | + |
| 132 | + method := GetString(fctx.Method()) |
| 133 | + |
| 134 | + var cacheKey string |
| 135 | + useCache := !r.settings.DisableCaching && |
| 136 | + (method == MethodGet || method == MethodPost) |
| 137 | + if useCache { |
| 138 | + cacheKey = path + method |
| 139 | + r.mutex.RLock() |
| 140 | + cacheResult, ok := r.cache[cacheKey] |
| 141 | + |
| 142 | + if ok { |
| 143 | + context.handlers = cacheResult.handlers |
| 144 | + context.paramValues = cacheResult.params |
| 145 | + r.mutex.RUnlock() |
| 146 | + context.handlers[0](context) |
| 147 | + return |
| 148 | + } |
| 149 | + r.mutex.RUnlock() |
| 150 | + } |
| 151 | + |
| 152 | + if root := r.trees[method]; root != nil { |
| 153 | + if handlers := root.matchRoute(path, context); handlers != nil { |
| 154 | + context.handlers = handlers |
| 155 | + context.handlers[0](context) |
| 156 | + |
| 157 | + if useCache { |
| 158 | + r.mutex.Lock() |
| 159 | + |
| 160 | + if r.cacheLen == r.settings.CacheSize { |
| 161 | + r.cache = make(map[string]*matchResult) |
| 162 | + r.cacheLen = 0 |
| 163 | + } |
| 164 | + r.cache[cacheKey] = &matchResult{ |
| 165 | + handlers: handlers, |
| 166 | + params: context.paramValues, |
| 167 | + } |
| 168 | + r.cacheLen++ |
| 169 | + r.mutex.Unlock() |
| 170 | + } |
| 171 | + return |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if method == MethodOptions && r.settings.HandleOPTIONS { |
| 176 | + if allow := r.allowed(method, path, context); len(allow) > 0 { |
| 177 | + fctx.Response.Header.Set("Allow", allow) |
| 178 | + return |
| 179 | + } |
| 180 | + } else if r.settings.HandleMethodNotAllowed { |
| 181 | + if allow := r.allowed(method, path, context); len(allow) > 0 { |
| 182 | + fctx.Response.Header.Set("Allow", allow) |
| 183 | + fctx.SetStatusCode(fasthttp.StatusMethodNotAllowed) |
| 184 | + fctx.SetContentTypeBytes(defaultContentType) |
| 185 | + fctx.SetBodyString(fasthttp.StatusMessage(fasthttp.StatusMethodNotAllowed)) |
| 186 | + return |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if r.notFound != nil { |
| 191 | + r.notFound[0](context) |
| 192 | + return |
| 193 | + } |
| 194 | + |
| 195 | + fctx.Error(fasthttp.StatusMessage(fasthttp.StatusNotFound), |
| 196 | + fasthttp.StatusNotFound) |
| 197 | +} |
| 198 | + |
| 199 | +func (r *router) SetNotFound(handlers handlersChain) { |
| 200 | + r.notFound = append(r.notFound, handlers...) |
| 201 | +} |
0 commit comments