-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
46 lines (38 loc) · 1 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
package main
import (
"fmt"
"image"
"image/png"
"math/rand"
"net/http"
"git.sr.ht/~sbinet/gg"
)
func imageGeneration() image.Image {
dc := gg.NewContext(1000, 200)
dc.SetRGB(1, 1, 1)
if err := dc.LoadFontFace("./LatinmodernmathRegular.otf", 100); err != nil {
panic(err)
}
denom := rand.Intn(4) + 2
prod := rand.Intn(3) + 2
calc := fmt.Sprintf("%d × (%d / %d) + %d =", rand.Intn(8)+2, denom*prod, denom, rand.Intn(19)+1)
dc.DrawStringAnchored(calc, 500, 100, 0.5, 0.5)
return dc.Image()
}
func handleRequest(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method is not supported.", http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Cache-Control", "no-store, no-cache, max-age=0")
img := imageGeneration()
png.Encode(w, img)
}
func main() {
// _ = imageGeneration()
handler := http.HandlerFunc(handleRequest)
http.Handle("/", handler)
http.ListenAndServe(":8080", nil)
}