forked from neldredge/mathgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
191 lines (180 loc) · 4.84 KB
/
http.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* http.go - http frontend
* Mathgen, Golang port.
* Copyright (C) 2025 Andrey V.
*
* Adapted from mathgen.pl from mathgen (https://thatsmathematics.com/mathgen/).
* Portions may be copyright (C) Nathaniel Eldredge.
*
* This, and the original code, are licensed under GPL 2.
*/
package main
import (
"encoding/json"
"embed"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"sync/atomic"
"text/template"
"github.com/joho/godotenv"
"github.com/m00shm00sh/mathgen/mathgen"
)
func writePlaintext(s string, w http.ResponseWriter) {
w.WriteHeader(200)
io.WriteString(w, s)
}
func writeJson(v any, w http.ResponseWriter) {
data, err := json.Marshal(v)
if err != nil {
write500(err, w)
return
}
w.Header().Set("Content-type", "application/json")
w.WriteHeader(200)
w.Write(data)
}
func write400(e error, w http.ResponseWriter) {
w.WriteHeader(400)
io.WriteString(w, e.Error())
}
func write429(s string, w http.ResponseWriter) {
w.WriteHeader(429)
io.WriteString(w, s)
}
func write500(e error, w http.ResponseWriter) {
w.WriteHeader(500)
io.WriteString(w, e.Error())
}
var (
mode2mime = map[mathgen.OutputMode]string{
mathgen.Raw: "text/plain",
mathgen.Pdf: "application/pdf",
mathgen.Zip: "application/zip",
}
concurrentRenderCount atomic.Int32
completedRenderCount atomic.Int32
rateLimit int32
)
//go:embed sci*.in
var sciRules embed.FS
func renderFactory(p mathgen.Product, m mathgen.OutputMode) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if rateLimit > 0 && concurrentRenderCount.Load() >= rateLimit {
write429("rate limit exceeded", w)
return
}
qs := r.URL.Query()
authors := qs["author"]
seedS := qs.Get("seed")
if seedS == "" {
seedS = "0"
}
d := mathgen.NewDriver()
d.FS = sciRules
d.Product = p
d.OutputMode = m
d.Authors = authors
if seed, e := strconv.ParseInt(seedS, 10, 64); e != nil {
write400(fmt.Errorf("decoding seed: %w", e), w)
return
} else {
d.Seed = seed
}
concurrentRenderCount.Add(1)
defer concurrentRenderCount.Add(-1)
if fh, e := d.GenerateOutput(); e != nil {
write500(fmt.Errorf("render: %w", e), w)
return
} else {
w.Header().Set("Content-type", mode2mime[m])
w.WriteHeader(200)
io.Copy(w, fh) // ignore errors and don't bother logging bytes sent
completedRenderCount.Add(1)
}
}
}
type statsT struct {
Concurrent int32 `json:"concurrent"`
Completed int32 `json:"completed"`
}
func doStats(w http.ResponseWriter, r *http.Request) {
k := r.URL.Query().Get("key")
switch k {
case "concurrent":
writePlaintext(strconv.FormatInt(int64(concurrentRenderCount.Load()), 10), w)
case "completed":
writePlaintext(strconv.FormatInt(int64(completedRenderCount.Load()), 10), w)
case "":
st := statsT{
Concurrent: concurrentRenderCount.Load(),
Completed: completedRenderCount.Load(),
}
writeJson(st, w)
default:
write400(errors.New("bad key"), w)
}
}
func main() {
godotenv.Load()
if envRatelimit := os.Getenv("RATE_LIMIT"); envRatelimit != "" {
if rateLimit_, e := strconv.ParseInt(envRatelimit, 10, 31); e != nil {
panic(fmt.Errorf("read ratelimit: %w", e))
} else {
rateLimit = int32(rateLimit_)
}
}
mux := http.NewServeMux()
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.New("html").Parse(
`<!DOCTYPE html>
<html>
<head><title>Mathgen: randomly generated math papers</title></head>
<body>
<p>
A port of <a href="https://thatsmathematics.com/mathgen/">Mathgen</a> to Golang.<br/>
Code available <a href="https://github.com/m00shm00sh/mathgen"/>here</a>.
</p>
<h2>Endpoints</h2>
<table>
{{range .}}
<tr><td><a href="{{.Link}}">{{.Link}}</a></td><td>{{.Text}}</td></tr>
{{end}}
</table>
<br/>
<h2>Query parameters</h2>
<ul>
<li><code>author</code>: author name; use FAMOUS_AUTHOR for a random celebrity</li>
</ul>
</body>
</html>`))
var b strings.Builder
if err := t.Execute(&b, []struct {
Link string
Text string
}{{"/article.pdf", "article (PDF)"},
{"/article.zip", "article (ZIP sources)"},
{"/book.pdf", "book (PDF) (please be patient)"},
{"/book.zip", "book (ZIP sources) (please be patient)"},
{"/blurb", "blurb (raw text)"},
{"/stats", "misc statistics"},
}); err != nil {
write500(err, w)
} else {
writePlaintext(b.String(), w)
}
})
mux.HandleFunc("GET /article.pdf", renderFactory(mathgen.Article, mathgen.Pdf))
mux.HandleFunc("GET /article.zip", renderFactory(mathgen.Article, mathgen.Zip))
mux.HandleFunc("GET /book.pdf", renderFactory(mathgen.Book, mathgen.Pdf))
mux.HandleFunc("GET /book.zip", renderFactory(mathgen.Book, mathgen.Zip))
mux.HandleFunc("GET /blurb", renderFactory(mathgen.Blurb, mathgen.Raw))
mux.HandleFunc("GET /stats", doStats)
s := http.Server{Handler: mux, Addr: ":8080"}
println("listening on :8080")
s.ListenAndServe()
}