-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
82 lines (68 loc) · 1.43 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
package main
import (
"bytes"
"log"
"net/http"
"os"
"text/template"
)
type fileServer struct {
root http.FileSystem
file string
}
var tmpl = `window.myodcConfig = {
'apiBaseURL': '{{.}}'
};
`
var configJS []byte
func (fs *fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f, err := fs.root.Open(fs.file)
if err != nil {
http.NotFound(w, r)
return
}
defer f.Close()
d, err1 := f.Stat()
if err1 != nil {
http.NotFound(w, r)
return
}
if d.IsDir() {
http.NotFound(w, r)
return
}
// serveContent will check modification time
http.ServeContent(w, r, d.Name(), d.ModTime(), f)
}
func serveFile(root http.FileSystem, file string) *fileServer {
return &fileServer{
root: root,
file: file,
}
}
func initConfigJS() []byte {
t, err := template.New("config").Parse(tmpl)
if err != nil {
return nil
}
b := bytes.NewBuffer(nil)
defer b.Reset()
err = t.Execute(b, os.Getenv("API_BASE_URL"))
return b.Bytes()
}
func init() {
configJS = initConfigJS()
// Static content
http.Handle("/", http.FileServer(http.Dir("_site")))
http.Handle("/p/", serveFile(http.Dir("_site"), "index.html"))
http.HandleFunc("/config.js", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/javascript;charset=UTF-8")
w.Write(configJS)
})
}
func main() {
log.Println("Starting web server on :8081")
if err := http.ListenAndServe(":8081", nil); err != nil {
panic(err.Error())
}
}