/
app.go
78 lines (69 loc) · 1.97 KB
/
app.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
package main
import (
"bytes"
"fmt"
"net/http"
"os"
"path/filepath"
"time"
"gnd.la/app"
"gnd.la/config"
_ "gnd.la/frontend/bootstrap"
"gnd.la/html"
"gnd.la/template"
"gnd.la/util/pathutil"
)
var (
App *app.App
data = pathutil.Relative("data")
)
func fileHandler(name string) app.Handler {
p := filepath.Join(data, name)
return func(ctx *app.Context) {
serveFile(ctx, p)
}
}
func serveFile(ctx *app.Context, path string) {
f, err := os.Open(path)
if err != nil {
ctx.NotFound("")
return
}
defer f.Close()
var modtime time.Time
if fi, err := f.Stat(); err == nil {
modtime = fi.ModTime()
}
http.ServeContent(ctx, ctx.R, filepath.Base(path), modtime, f)
}
func init() {
template.AddFuncs(template.FuncMap{
"#config": func(name string, def string) template.HTML {
var buf bytes.Buffer
buf.WriteString("<h3 class=\"config\">")
buf.WriteString(html.Escape(name))
if def != "" {
buf.WriteString(" <span class=\"label label-success\">optional</span>")
fmt.Fprintf(&buf, " <span class=\"default\">default: %s</span>", html.Escape(def))
} else {
buf.WriteString(" <span class=\"label label-danger\">required</span>")
}
buf.WriteString("</h3>")
return template.HTML(buf.String())
}})
config.MustParse()
App = app.New()
App.SetTrustXHeaders(true)
// Redirect all other possible hosts to governator.io
redir := app.RedirectHandler("http://governator.io${0}", true)
App.Handle("(.*)", redir, app.HostHandler("governator-io.appspot.com"))
App.Handle("(.*)", redir, app.HostHandler("www.governator.io"))
App.HandleAssets("/assets/", pathutil.Relative("assets"))
App.Handle("^/$", app.TemplateHandler("main.html", nil))
App.Handle("^/install\\.sh$", fileHandler("contrib/install.sh"))
App.Handle("^/get/releases/linux/x86_64/latest/governator$", fileHandler("governator"))
App.Handle("^/contrib/(.*)", func(ctx *app.Context) {
path := filepath.Join(data, "contrib", filepath.FromSlash(ctx.IndexValue(0)))
serveFile(ctx, path)
})
}