/
restgo.go
96 lines (77 loc) · 2.47 KB
/
restgo.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
package restgo
import (
"github.com/fasthttp-contrib/render"
"github.com/valyala/fasthttp"
)
type Restgo struct {
router *Router // root router for restgo
server *fasthttp.Server
}
// create app instance, give render config or use default(pass nothing)
// lean how to config render here : https://github.com/unrolled/render
func App(renderConfig ...*render.Config) *Restgo {
app := &Restgo{
router: NewRouter(renderConfig...),
server: &fasthttp.Server{
Name: "Restgo",
},
}
app.server.Handler = app.router.FastHttpHandler
return app
}
// create a new router
func (this *Restgo) Router(renderConfig ...*render.Config) *Router {
return NewRouter(renderConfig...)
}
// run app on address `addr` or default `:8080`. only first addr will be used in the parameters.
func (this *Restgo) Run(addr ...string) {
var address = ":8080" // default address
if len(addr) > 0 {
address = addr[0]
}
err := this.server.ListenAndServe(address)
panic(err)
}
/*
Shorthands methods
*/
// set handlers for `path`, default is `/`. you can use it as filters
func (this *Restgo) Use(handlers ...interface{}) *Router {
return this.router.Use(handlers...)
}
// create a sub-route
func (this *Restgo) Route(path string) *Route {
return this.router.Route(path)
}
// set handlers for all types requests
func (this *Restgo) All(path string, handlers ...HTTPHandler) *Router {
return this.router.All(path, handlers...)
}
// set handlers for `GET` request
func (this *Restgo) GET(path string, handlers ...HTTPHandler) *Router {
return this.router.GET(path, handlers...)
}
// set handlers for `POST` request
func (this *Restgo) POST(path string, handlers ...HTTPHandler) *Router {
return this.router.POST(path, handlers...)
}
// set handlers for `PUT` request
func (this *Restgo) PUT(path string, handlers ...HTTPHandler) *Router {
return this.router.PUT(path, handlers...)
}
// set handlers for `DELETE` request
func (this *Restgo) DELETE(path string, handlers ...HTTPHandler) *Router {
return this.router.DELETE(path, handlers...)
}
// set handlers for `HEAD` request
func (this *Restgo) HEAD(path string, handlers ...HTTPHandler) *Router {
return this.router.HEAD(path, handlers...)
}
// set handlers for `OPTIONS` request
func (this *Restgo) OPTIONS(path string, handlers ...HTTPHandler) *Router {
return this.router.OPTIONS(path, handlers...)
}
// set handlers for `PATCH` request
func (this *Restgo) PATCH(path string, handlers ...HTTPHandler) *Router {
return this.router.PATCH(path, handlers...)
}