-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
85 lines (72 loc) · 1.84 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
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
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/apex/log"
jsonhandler "github.com/apex/log/handlers/json"
"github.com/tj/go/env"
"github.com/apex/gui/components"
)
// static files.
var static = http.FileServer(http.Dir("dist"))
// main function.
func main() {
log.SetHandler(jsonhandler.Default)
addr := ":" + env.GetDefault("PORT", "3000")
err := http.ListenAndServe(addr, http.HandlerFunc(handle))
if err != nil {
log.Fatalf("error: %s", err)
}
}
// handle the request.
func handle(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/_health":
fmt.Fprintln(w, ":)")
case "/component":
component(w, r)
default:
static.ServeHTTP(w, r)
}
}
// component renders a component.
func component(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
config := r.URL.Query().Get("config")
var c components.Config
if strings.TrimSpace(name) == "" {
http.Error(w, "Component `name` parameter is required.", http.StatusBadRequest)
return
}
if err := json.Unmarshal([]byte(config), &c); err != nil {
log.WithError(err).Error("parsing config")
http.Error(w, "Component `config` parameter is malformed.", http.StatusBadRequest)
return
}
ctx := log.WithField("name", name)
ctx.Info("rendering")
var buf bytes.Buffer
err := components.Render(&buf, name, c)
if err != nil {
ctx.WithError(err).Error("rendering")
http.Error(w, "Error rendering.", http.StatusBadRequest)
return
}
setETag(w, buf.Bytes())
w.Header().Set("Content-Type", "image/svg+xml")
w.Header().Set("Cache-Control", "private")
io.Copy(w, &buf)
}
// setETag sets the etag for body.
func setETag(w http.ResponseWriter, body []byte) {
hash := md5.New()
hash.Write(body)
etag := hex.EncodeToString(hash.Sum(nil))
w.Header().Set("ETag", `w/"`+etag+`"`)
}