-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
70 lines (61 loc) · 1.57 KB
/
handler.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
package main
import (
"log"
"net/http"
_ "net/http/pprof"
"github.com/valyala/fasthttp"
router "github.com/vinhjaxt/fasthttp-staticrouter"
)
func buildHTTPHandler(staticDir string) func(*fasthttp.RequestCtx) {
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()
r := router.New()
api := r.Group("/api")
api.Use(func(ctx *fasthttp.RequestCtx) (b bool) {
b = true
ctx.Response.Header.SetContentType("application/json;charset=utf-8")
// Do authorization
auth := true // Do something
if !auth {
// Abort request
return
}
// Next router
b = false
return
})
apiv1 := api.Group("/v1.0")
apiv1.Use(func(ctx *fasthttp.RequestCtx) (b bool) {
ctx.Response.Header.Set("X-API-Version", "1.0")
return
})
apiv1.Get("/", func(ctx *fasthttp.RequestCtx) (_ bool) {
ctx.SetBodyString(`"Hello world"`)
return
})
apiv1.Post("/", func(ctx *fasthttp.RequestCtx) (_ bool) {
ctx.SetBodyString(`"Hello world"`)
return
})
// Not found handler
notFoundHandler := func(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(404)
}
if len(staticDir) != 0 {
log.Println("Static file serve:", staticDir)
// Go to: https://github.com/valyala/fasthttp/blob/master/examples/fileserver/fileserver.go
// for more informations
fs := &fasthttp.FS{
Root: staticDir,
IndexNames: []string{"index.html"},
GenerateIndexPages: false,
Compress: true,
AcceptByteRange: true,
PathNotFound: notFoundHandler,
}
notFoundHandler = fs.NewRequestHandler()
}
r.NotFound(notFoundHandler)
return r.BuildHandler()
}