-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
39 lines (31 loc) · 814 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
package main
import (
"bytes"
"io"
"log"
"github.com/klauspost/compress/gzip"
"github.com/valyala/fasthttp"
)
const serverAddr = ":8081"
func main() {
server := &fasthttp.Server{}
server.Handler = func(ctx *fasthttp.RequestCtx) {
body := ctx.Request.Body()
ctx.Response.SetBody([]byte(`{"msg": "Hello!"}`))
ctx.Response.SetStatusCode(200)
if string(ctx.Request.Header.Peek("Content-Type")) == "gzip" {
gzipReader, err := gzip.NewReader(bytes.NewReader(body))
if err != nil {
return
}
parsedBody, err := io.ReadAll(gzipReader)
if err != nil {
return
}
body = parsedBody
}
log.Printf("%s %s %s\n", ctx.Method(), ctx.Request.URI().RequestURI(), body)
}
log.Println("Running on", serverAddr)
log.Fatalln(fasthttp.ListenAndServe(serverAddr, server.Handler))
}