-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkelly.go
59 lines (47 loc) · 1.06 KB
/
kelly.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
// Copyright 2017 King Qiu. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
// https://github.com/qjw/kelly
package kelly
import (
"github.com/urfave/negroni"
"net/http"
)
type HandlerFunc func(c *Context)
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f(newContext(w, r, nil))
}
type Kelly interface {
Router
Run(addr ...string)
}
type kellyImp struct {
*router
n *negroni.Negroni
}
func (k *kellyImp) Run(addr ...string) {
if k.n == nil {
panic("invalid kelly")
}
k.router.doBeforeRun()
k.n.Run(addr...)
}
func newImp(n *negroni.Negroni, handlers ...HandlerFunc) Kelly {
rt := newRouterImp(handlers...)
ky := &kellyImp{
router: rt,
n: n,
}
ky.n = n
n.UseHandler(rt)
return ky
}
func EnableDebug(debug bool) {
DebugFlag = debug
}
func NewClassic(handlers ...HandlerFunc) Kelly {
return newImp(negroni.Classic(), handlers...)
}
func New(handlers ...HandlerFunc) Kelly {
return newImp(negroni.New(negroni.NewRecovery()), handlers...)
}