-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.go
60 lines (48 loc) · 1.78 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"flag"
"fmt"
"log"
"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.GET("/", handleHelloWorld)
s := &fasthttp.Server{
Name: "HelloWorld",
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
}
if err := g.ListenAndServe(*addr, "", s); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func handleHelloWorld(r *fastglue.Request) error {
fmt.Fprintf(r.RequestCtx, "Hello, world!\n\n")
fmt.Fprintf(r.RequestCtx, "Request method is %q\n", r.RequestCtx.Method())
fmt.Fprintf(r.RequestCtx, "RequestURI is %q\n", r.RequestCtx.RequestURI())
fmt.Fprintf(r.RequestCtx, "Requested path is %q\n", r.RequestCtx.Path())
fmt.Fprintf(r.RequestCtx, "Host is %q\n", r.RequestCtx.Host())
fmt.Fprintf(r.RequestCtx, "Query string is %q\n", r.RequestCtx.QueryArgs())
fmt.Fprintf(r.RequestCtx, "User-Agent is %q\n", r.RequestCtx.UserAgent())
fmt.Fprintf(r.RequestCtx, "Connection has been established at %s\n", r.RequestCtx.ConnTime())
fmt.Fprintf(r.RequestCtx, "Request has been started at %s\n", r.RequestCtx.Time())
fmt.Fprintf(r.RequestCtx, "Serial request number for the current connection is %d\n", r.RequestCtx.ConnRequestNum())
fmt.Fprintf(r.RequestCtx, "Your ip is %q\n\n", r.RequestCtx.RemoteIP())
fmt.Fprintf(r.RequestCtx, "Raw request is:\n---CUT---\n%s\n---CUT---", &r.RequestCtx.Request)
r.RequestCtx.SetContentType("text/plain; charset=utf8")
// Set arbitrary headers
r.RequestCtx.Response.Header.Set("X-My-Header", "my-header-value")
// Set cookies
var c fasthttp.Cookie
c.SetKey("cookie-name")
c.SetValue("cookie-value")
r.RequestCtx.Response.Header.SetCookie(&c)
return nil
}