-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
57 lines (44 loc) · 1004 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/valyala/fasthttp"
"github.com/zerodha/fastglue"
)
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
)
func main() {
flag.Parse()
g := fastglue.New()
g.Before(setTime)
g.After(calculateTime)
g.GET("/", handleIndex)
s := &fasthttp.Server{
Name: "Before After",
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
if err := g.ListenAndServe(*addr, "", s); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func handleIndex(r *fastglue.Request) error {
name := "world!"
return r.SendString(http.StatusOK, fmt.Sprintf("Hello %s!", name))
}
func setTime(r *fastglue.Request) *fastglue.Request {
r.RequestCtx.SetUserValue("now", time.Now())
return r
}
func calculateTime(r *fastglue.Request) *fastglue.Request {
now, ok := r.RequestCtx.UserValue("now").(time.Time)
if !ok {
return r
}
log.Print("time taken", time.Since(now))
return r
}