-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_context.go
55 lines (44 loc) · 1.53 KB
/
app_context.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
package main
import (
"net/http"
"time"
rice "github.com/GeertJohan/go.rice"
"github.com/didip/tollbooth"
"github.com/didip/tollbooth/limiter"
"github.com/gorilla/mux"
"github.com/timewasted/go-accept-headers"
)
type appOption func(a *appContext)
type appContext struct {
sb *rice.Box
m *mux.Router
}
func newAppContext(staticBox *rice.Box, options ...appOption) *appContext {
a := &appContext{
sb: staticBox,
m: mux.NewRouter().UseEncodedPath(),
}
for _, fn := range options {
fn(a)
}
a.m.Methods("GET").Path("/health").HandlerFunc(a.handleAPIHealth)
a.m.Methods("GET").Path("/y000000000028.cfg").HandlerFunc(a.handleAPIPhoneSettingsFileGet)
a.m.Methods("POST").Path("/api/v1/phone/settings").Handler(tollbooth.LimitFuncHandler(tollbooth.NewLimiter(1, &limiter.ExpirableOptions{DefaultExpirationTTL: time.Second * 2}), a.handleAPIPhoneSettingsPut))
a.m.Methods("GET").Path("/favicon.ico").Handler(http.RedirectHandler("static/favicon.ico", http.StatusFound))
a.m.Methods("GET").Path("/").HandlerFunc(a.handleAPIPhoneSettingsFrontendGet)
a.m.PathPrefix("/static").Handler(http.StripPrefix("/static", http.FileServer(staticBox.HTTPBox())))
return a
}
func (a *appContext) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
acceptable := accept.Parse(r.Header.Get("accept"))
if acceptable.Accepts("text/css") {
ct, err := acceptable.Negotiate("text/html", "application/json", "text/css")
if err != nil {
panic(err)
}
if ct == "text/css" {
rw.Header().Set("content-type", "text/css")
}
}
a.m.ServeHTTP(rw, r)
}