-
Notifications
You must be signed in to change notification settings - Fork 5
/
controller.go
134 lines (120 loc) · 3.26 KB
/
controller.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package web
import (
"fmt"
"html"
"io"
"net/http"
"github.com/go-martini/martini"
"github.com/martini-contrib/auth"
"github.com/martini-contrib/render"
"github.com/martini-contrib/secure"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/tyler-sommer/stick"
)
var requestCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total web request count.",
}, []string{"proto", "method", "url"})
func init() {
prometheus.MustRegister(requestCounter)
}
type generatedTemplate func(env *stick.Env, output io.Writer, ctx map[string]stick.Value)
type generatedEnv struct {
*stick.Env
mapping map[string]generatedTemplate
}
func (env *generatedEnv) Execute(tpl string, out io.Writer, ctx map[string]stick.Value) error {
if fn, ok := env.mapping[tpl]; ok {
fn(env.Env, out, ctx)
return nil
}
return env.Env.Execute(tpl, out, ctx)
}
type StickHandler struct {
env *generatedEnv
res http.ResponseWriter
}
func (h *StickHandler) HTML(status int, name string, ctx map[string]stick.Value) {
h.res.WriteHeader(200)
err := h.env.Execute(name, h.res, ctx)
if err != nil {
fmt.Println(err)
}
}
func newStickHandler() martini.Handler {
env := stick.New(newTemplateLoader())
env.Functions["escape"] = func(ctx stick.Context, args ...stick.Value) stick.Value {
if len(args) < 1 {
return nil
}
return html.EscapeString(stick.CoerceString(args[0]))
}
env.Filters["join"] = func(ctx stick.Context, val stick.Value, args ...stick.Value) stick.Value {
if len(args) == 0 {
return nil
}
if !stick.IsIterable(val) {
return nil
}
sep := []byte(stick.CoerceString(args[0]))
var res []byte
_, err := stick.Iterate(val, func(k, v stick.Value, l stick.Loop) (brk bool, err error) {
if len(res) > 0 {
res = append(append(res, sep...), stick.CoerceString(v)...)
} else {
res = append(res, stick.CoerceString(v)...)
}
return false, nil
})
if err != nil {
// ?
}
return string(res)
}
env.Filters["length"] = func(ctx stick.Context, val stick.Value, args ...stick.Value) stick.Value {
if stick.IsIterable(val) {
l, _ := stick.Iterate(val, func(k, v stick.Value, l stick.Loop) (brk bool, err error) {
return
})
return l
}
return len(stick.CoerceString(val))
}
genv := &generatedEnv{env, templateMapping}
return func(res http.ResponseWriter, req *http.Request, c martini.Context) {
c.Map(&StickHandler{genv, res})
}
}
func counterHandler(req *http.Request) {
proto := "http"
if req.TLS != nil {
proto = "https"
}
requestCounter.With(prometheus.Labels{"proto": proto, "method": req.Method, "url": req.URL.Path}).Add(1)
}
func (s *Server) Configure() {
s.Handlers(
counterHandler,
newStaticHandler(),
newStickHandler(),
render.Renderer(),
secure.Secure(secure.Options{
BrowserXssFilter: true,
FrameDeny: true,
SSLRedirect: s.conf.RequireHTTPS,
SSLHost: s.conf.SSLHostPort,
DisableProdCheck: true,
}),
)
s.NotFound(func(req *http.Request, r render.Render, l log.FieldLogger) {
r.Error(404)
})
if s.conf.HTTPAuth && len(s.conf.AuthUsername) > 0 && len(s.conf.AuthPassword) > 0 {
s.Use(auth.Basic(s.conf.AuthUsername, s.conf.AuthPassword))
}
err := Configure(s)
if err != nil {
s.l.Errorln(err)
}
}