-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorineeweb.go
140 lines (120 loc) · 3.47 KB
/
gorineeweb.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package GorineeWeb
import (
"github.com/fasthttp/router"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
"log"
"time"
)
// Exported constants
const (
Version = "0.0.1 alpha" // Version of GorineeWeb
Name = "GorineeWeb" // Name of GorineeWeb
banner2 = `
____ _ __ __ _
/ ___| ___ ___ _ __(_)_ __ ___ __\ \ / /__| |__
| | _ / _ \ / _ \| '__| | '_ \ / _ \/ _ \ \ /\ / / _ \ '_ \
| |_| | (_) | (_) | | | | | | | __/ __/\ V V / __/ |_) |
\____|\___/ \___/|_| |_|_| |_|\___|\___| \_/\_/ \___|_.__/ v%s
Listening on %s`
)
//router 생성하기
type gorineeWeb struct {
httpServer *fasthttp.Server
router *router.Router
registeredRoutes []*Route
address string // server address
middlewares handlersChain
settings *Settings
}
type Route struct {
Method METHOD
Path string
Handler Handler
}
type GorineeWeb interface {
Get(path string, handler Handler)
Post(path string, handler Handler)
Put(path string, handler Handler)
Delete(path string, handler Handler)
Head(path string, handler Handler)
New() *gorineeWeb
Custom(server *fasthttp.Server) *gorineeWeb
Run(addr string)
HealthCheck()
handle(method METHOD, path string, handler Handler)
//WebSocket(path string, handler Handler)
}
func New() *gorineeWeb {
return &gorineeWeb{
httpServer: &fasthttp.Server{
Logger: &customLogger{},
Concurrency: 1000,
MaxRequestBodySize: 2 << 20,
ReadTimeout: time.Duration(10 * 1000),
WriteTimeout: time.Duration(10 * 1000),
},
router: router.New(),
}
}
//custom
func Custom(server *fasthttp.Server) *gorineeWeb {
return &gorineeWeb{
httpServer: server,
router: router.New(),
}
}
//서버 실행하기
func (g *gorineeWeb) Run(addr string) {
g.httpServer.Handler = g.router.Handler
log.Printf(banner2, Version, addr)
g.httpServer.ListenAndServe(addr)
}
func (g *gorineeWeb) HealthCheck() {
fasthandler := fasthttpadaptor.NewFastHTTPHandler(promhttp.Handler())
g.router.Handle(Get.convert(), "metrics", fasthandler)
}
func (g *gorineeWeb) handle(method METHOD, path string, handler Handler) {
g.router.Handle(method.convert(), path, func(ctx *fasthttp.RequestCtx) {
gorineeWebContext := &gorineeWebContext{ctx: ctx}
handler(gorineeWebContext)
})
}
//func (g *gorineeWeb) WebSocket(path string, handler Handler) {
// g.router.Handle(method.convert(), path, func(ctx *fasthttp.RequestCtx) {
// gorineeWebContext := &gorineeWebContext{ctx: ctx}
// handler(gorineeWebContext)
// })
//}
//func (g *gorineeWeb) registeredRoute(method METHOD, path string, handler Handler) *Route {
// route := &Route{
// Path: path,
// Method: method,
// Handler: handler,
// }
// g.registeredRoutes = append(g.registeredRoutes, route)
// return route
//}
func (g *gorineeWeb) Get(path string, handler Handler) {
//TODO implement me
g.handle(Get, path, handler)
}
func (g *gorineeWeb) Post(path string, handler Handler) {
//TODO implement me
g.handle(Post, path, handler)
}
func (g *gorineeWeb) Put(path string, handler Handler) {
//TODO implement me
g.handle(Put, path, handler)
}
func (g *gorineeWeb) Delete(path string, handler Handler) {
//TODO implement me
g.handle(Delete, path, handler)
}
func (g *gorineeWeb) Head(path string, handler Handler) {
//TODO implement me
g.handle(Head, path, handler)
}
func registerMiddleware(handler Handler) {
}