-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
56 lines (48 loc) · 1.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
47
48
49
50
51
52
53
54
55
56
package main
import (
_ "embed"
"errors"
"log"
"net/http"
"time"
)
func latexHandler(w http.ResponseWriter, r *http.Request) error {
defer func(t time.Time) {
log.Println("execution time", time.Since(t))
}(time.Now())
mr, err := r.MultipartReader()
if err != nil {
return err
}
return printMultipart(w, mr)
}
//go:embed index.html
var index []byte
func indexHandler(w http.ResponseWriter, r *http.Request) error {
_, err := w.Write(index)
return err
}
func errHandler(w http.ResponseWriter, err error, code int) {
if err == nil {
err = errors.New(http.StatusText(code))
}
http.Error(w, err.Error(), code)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
if err := indexHandler(w, r); err != nil {
errHandler(w, err, http.StatusInternalServerError)
}
case http.MethodPost:
if err := latexHandler(w, r); err != nil {
errHandler(w, err, http.StatusInternalServerError)
}
default:
errHandler(w, nil, http.StatusMethodNotAllowed)
}
}
func main() {
http.HandleFunc("/", rootHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}