forked from zerodha/fastglue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (45 loc) · 979 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
package main
import (
"flag"
"fmt"
"log"
"os"
"time"
"github.com/valyala/fasthttp"
"github.com/zerodha/fastglue"
)
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
)
// App singleton.
type App struct {
version string
log *log.Logger
}
func main() {
flag.Parse()
app := &App{
version: "0.1.0",
log: log.New(os.Stdout, "SINGLETON", log.Llongfile),
}
g := fastglue.New()
g.SetContext(app)
g.GET("/", handleIndex)
s := &fasthttp.Server{
Name: "Singleton",
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 {
var (
app = r.Context.(*App)
)
app.log.Printf(fmt.Sprintf("index called. user-agent:%s, args: %q", r.RequestCtx.UserAgent(), r.RequestCtx.QueryArgs()))
return r.SendEnvelope(map[string]string{
"version": app.version,
})
}