/
handle.go
50 lines (40 loc) · 1.13 KB
/
handle.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
// Copyright 2021 eatmoreapple. All rights reserved.
// Use of this source code is governed by a GPL style
// license that can be found in the LICENSE file.
package regia
import (
"net/http"
)
type HandleFunc func(context *Context)
type HandleFuncGroup []HandleFunc
type handleFuncNode struct {
HandleFunc HandleFunc
BluePrint *BluePrint
}
type handleFuncNodeGroup []*handleFuncNode
func HandleNotFound(context *Context) { http.NotFound(context.ResponseWriter, context.Request) }
func RawHandlerFunc(handler http.HandlerFunc) HandleFunc {
return func(c *Context) {
SetContextIntoRequest(c)
handler(c.ResponseWriter, c.Request)
}
}
func RawHandlerFuncGroup(handlers ...http.HandlerFunc) HandleFuncGroup {
group := make(HandleFuncGroup, len(handlers))
for index, handler := range handlers {
h := RawHandlerFunc(handler)
group[index] = h
}
return group
}
// RecoverHandler recovers from panics and call given handler
func RecoverHandler(h func(ctx *Context, rec interface{})) HandleFunc {
return func(context *Context) {
defer func() {
if rec := recover(); rec != nil {
h(context, rec)
}
}()
context.Next()
}
}