-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
39 lines (34 loc) · 1.01 KB
/
main.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
package main
import (
"bytes"
"github.com/buaazp/fasthttprouter"
"github.com/hanjm/fasthttpmiddleware"
"github.com/hanjm/zaplog"
"github.com/valyala/fasthttp"
)
func exampleAuthFunc(ctx *fasthttp.RequestCtx) bool {
if bytes.HasPrefix(ctx.Path(), []byte("/protect")) {
return false
}
return true
}
func requestHandler(ctx *fasthttp.RequestCtx) {
ctx.WriteString("hello")
}
func panicHandler(ctx *fasthttp.RequestCtx) {
panic("test panic")
}
func main() {
logger := zaplog.NewNoCallerLogger(false)
mo := fasthttpmiddleware.NewNormalMiddlewareOnion(exampleAuthFunc, logger)
moWithoutAuth := fasthttpmiddleware.NewMiddlewareOnion(
fasthttpmiddleware.NewLogMiddleware(logger, false),
fasthttpmiddleware.NewRecoverMiddleware(logger),
)
router := fasthttprouter.New()
router.GET("/", mo.Apply(requestHandler))
router.GET("/protect", mo.Apply(requestHandler))
router.GET("/panic", mo.Apply(panicHandler))
router.GET("/noAuth", moWithoutAuth.Apply(requestHandler))
fasthttp.ListenAndServe(":8000", router.Handler)
}