-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathace.go
90 lines (76 loc) · 1.77 KB
/
ace.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
package ace
import (
"github.com/julienschmidt/httprouter"
"github.com/plimble/utils/pool"
"net/http"
"sync"
)
var bufPool = pool.NewBufferPool(100)
type Ace struct {
*Router
httprouter *httprouter.Router
pool sync.Pool
render Renderer
panicFunc PanicHandler
notfoundFunc HandlerFunc
}
type PanicHandler func(c *C, rcv interface{})
type HandlerFunc func(c *C)
func GetPool() *pool.BufferPool {
return bufPool
}
//New server
func New() *Ace {
a := &Ace{}
a.Router = &Router{
handlers: nil,
prefix: "/",
ace: a,
}
a.panicFunc = defaultPanic
a.notfoundFunc = defaultNotfound
a.httprouter = httprouter.New()
a.pool.New = func() interface{} {
c := &C{}
c.index = -1
c.Writer = &c.writercache
return c
}
a.httprouter.PanicHandler = func(w http.ResponseWriter, req *http.Request, rcv interface{}) {
c := a.createContext(w, req)
a.panicFunc(c, rcv)
a.pool.Put(c)
}
a.httprouter.NotFound = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
c := a.createContext(w, req)
a.notfoundFunc(c)
a.pool.Put(c)
})
return a
}
//Default server white recovery and logger middleware
func Default() *Ace {
a := New()
a.Use(Logger())
return a
}
//SetPoolSize of buffer
func (a *Ace) SetPoolSize(poolSize int) {
bufPool = pool.NewBufferPool(poolSize)
}
//Run server with specific address and port
func (a *Ace) Run(addr string) {
if err := http.ListenAndServe(addr, a); err != nil {
panic(err)
}
}
//RunTLS server with specific address and port
func (a *Ace) RunTLS(addr string, cert string, key string) {
if err := http.ListenAndServeTLS(addr, cert, key, a); err != nil {
panic(err)
}
}
//ServeHTTP implement http.Handler
func (a *Ace) ServeHTTP(w http.ResponseWriter, req *http.Request) {
a.httprouter.ServeHTTP(w, req)
}