-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
35 lines (29 loc) · 780 Bytes
/
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
package main
import (
"github.com/sviterok/panichandler"
"github.com/valyala/fasthttp"
)
func main() {
requestHandler := func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/hello":
helloHandler(ctx)
case "/danger":
dangerousHandler(ctx)
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
}
// Create a limiter struct.
safeRequestHandler := panichandler.SimplePanicHandler(requestHandler)
fasthttp.ListenAndServe(":4444", safeRequestHandler)
}
func helloHandler(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetBody([]byte("Hello, World!"))
}
func dangerousHandler(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetBody([]byte("Doing science..."))
panic("oh no!!!")
}