-
Notifications
You must be signed in to change notification settings - Fork 0
/
chocokacang.go
73 lines (55 loc) · 1.17 KB
/
chocokacang.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
package chocokacang
import (
"net/http"
"sync"
"github.com/chocokacang/chocokacang/config"
"github.com/chocokacang/chocokacang/log"
)
type CK struct {
Router *Router
trees trees
pool sync.Pool
}
func New() *CK {
// Create environment instance
env := &CK{}
// Create router instance
env.Router = &Router{
env: env,
}
// Create pool for context
env.pool.New = func() any {
return &Context{env: env}
}
return env
}
func (env *CK) AddRouter() {
}
func (env *CK) AddGroupRouter(load func(r *Router)) {
load(env.Router)
}
func (env *CK) AddModule(load func(m Module)) {
load(env)
}
func (env *CK) ServeHTTP(rsw http.ResponseWriter, rq *http.Request) {
// Get context instance from the pool
c := env.pool.Get().(*Context)
rsw.WriteHeader(http.StatusNotFound)
rsw.WriteHeader(http.StatusOK)
rsw.Write([]byte("X"))
// Put context to the pool
env.pool.Put(c)
}
func (env *CK) Run() {
port := config.Get.AppPort
log.Info("Run the server in localhost:%s", port)
server := &http.Server{
Addr: ":" + port,
Handler: env,
ErrorLog: log.Logger(log.WARNING),
}
err := server.ListenAndServe()
if err != nil {
log.Error("Stop the server, %v", err)
}
}