/
main.go
43 lines (36 loc) · 887 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
// Copyright 2022 Fastly, Inc.
package main
import (
"context"
"fmt"
"strconv"
"time"
"github.com/fastly/compute-sdk-go/fsthttp"
)
func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
n := getQueryInt(r, "n", 5)
d := getQueryDuration(r, "d", 250*time.Millisecond)
// If you're using cURL, be sure to use `-N, --no-buffer`.
fmt.Fprintf(w, "n=%d, d=%s\n", n, d)
for i := 1; i <= n; i++ {
time.Sleep(d)
fmt.Fprintf(w, " ʕ◔ϖ◔ʔ")
}
fmt.Fprintln(w)
})
}
func getQueryInt(r *fsthttp.Request, key string, def int) int {
i, err := strconv.Atoi(r.URL.Query().Get(key))
if err != nil {
return def
}
return i
}
func getQueryDuration(r *fsthttp.Request, key string, def time.Duration) time.Duration {
d, err := time.ParseDuration(r.URL.Query().Get(key))
if err != nil {
return def
}
return d
}