-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
52 lines (43 loc) · 1.03 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
package main
import (
"io/ioutil"
"net/http"
"os"
"strings"
"html/template"
"github.com/apex/log"
jsonlog "github.com/apex/log/handlers/json"
"github.com/apex/log/handlers/text"
"github.com/gorilla/pat"
)
func init() {
if os.Getenv("UP_STAGE") == "" {
log.SetHandler(text.Default)
} else {
log.SetHandler(jsonlog.Default)
}
}
func main() {
addr := ":" + os.Getenv("PORT")
app := pat.New()
s := http.StripPrefix("/static/", http.FileServer(http.Dir("static")))
app.PathPrefix("/static/").Handler(s)
app.Get("/", get)
if err := http.ListenAndServe(addr, app); err != nil {
log.WithError(err).Fatal("error listening")
}
}
func get(w http.ResponseWriter, r *http.Request) {
t := template.Must(template.New("").ParseGlob("*.html"))
content, err := ioutil.ReadFile("bins")
if err != nil {
http.Error(w, err.Error(), 500)
return
}
bins := strings.Split(string(content), "\n")
if r.URL.Query().Get("polyfill") != "" {
t.ExecuteTemplate(w, "polyfill.html", bins)
} else {
t.ExecuteTemplate(w, "index.html", bins)
}
}