-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathhelloworldserver.go
More file actions
97 lines (80 loc) · 2.69 KB
/
Copy pathhelloworldserver.go
File metadata and controls
97 lines (80 loc) · 2.69 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Command helloworldserver displays various request info with 0 allocs/op.
package main
import (
"flag"
"log"
"net"
"strconv"
"sync"
"github.com/valyala/fasthttp"
)
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
compress = flag.Bool("compress", false, "Whether to enable transparent response compression")
)
func main() {
flag.Parse()
h := requestHandler
if *compress {
// Note: compression itself allocates. The 0 allocs/op claim covers
// requestHandler, i.e. the uncompressed path.
h = fasthttp.CompressHandler(h)
}
if err := fasthttp.ListenAndServe(*addr, h); err != nil {
log.Fatalf("Error in ListenAndServe: %v", err)
}
}
// scratchPool holds reusable buffers for formatting non-[]byte values.
var scratchPool = sync.Pool{
New: func() any {
b := make([]byte, 0, 64)
return &b
},
}
func requestHandler(ctx *fasthttp.RequestCtx) {
buf := scratchPool.Get().(*[]byte) //nolint:forcetypeassert
defer scratchPool.Put(buf)
resp := &ctx.Response
resp.AppendBodyString("Hello, world!\n\n")
resp.AppendBodyString("Request method is \"")
resp.AppendBody(ctx.Method())
resp.AppendBodyString("\"\nRequestURI is \"")
resp.AppendBody(ctx.RequestURI())
resp.AppendBodyString("\"\nRequested path is \"")
resp.AppendBody(ctx.Path())
resp.AppendBodyString("\"\nHost is \"")
resp.AppendBody(ctx.Host())
resp.AppendBodyString("\"\nQuery string is \"")
*buf = ctx.QueryArgs().AppendBytes((*buf)[:0])
resp.AppendBody(*buf)
resp.AppendBodyString("\"\nUser-Agent is \"")
resp.AppendBody(ctx.UserAgent())
resp.AppendBodyString("\"\nConnection has been established at ")
resp.AppendBody(fasthttp.AppendHTTPDate((*buf)[:0], ctx.ConnTime()))
resp.AppendBodyString("\nRequest has been started at ")
resp.AppendBody(fasthttp.AppendHTTPDate((*buf)[:0], ctx.Time()))
resp.AppendBodyString("\nSerial request number for the current connection is ")
resp.AppendBody(strconv.AppendUint((*buf)[:0], ctx.ConnRequestNum(), 10))
resp.AppendBodyString("\nYour ip is \"")
resp.AppendBody(appendIP((*buf)[:0], ctx.RemoteIP()))
resp.AppendBodyString("\"\n\n")
ctx.SetContentType("text/plain; charset=utf8")
// Set arbitrary headers
resp.Header.Set("X-My-Header", "my-header-value")
// Set cookies. AcquireCookie avoids the allocation a zero-value Cookie
// would cause.
c := fasthttp.AcquireCookie()
defer fasthttp.ReleaseCookie(c)
c.SetKey("cookie-name")
c.SetValue("cookie-value")
resp.Header.SetCookie(c)
}
// appendIP appends ip to dst without allocating. AppendIPv4 only handles
// IPv4, so IPv6 falls back to net.IP.AppendText.
func appendIP(dst []byte, ip net.IP) []byte {
if ip.To4() != nil {
return fasthttp.AppendIPv4(dst, ip)
}
dst, _ = ip.AppendText(dst)
return dst
}