forked from jsdir/blarg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayload.go
More file actions
64 lines (53 loc) · 1.16 KB
/
Copy pathpayload.go
File metadata and controls
64 lines (53 loc) · 1.16 KB
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
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"text/template"
"goji.io"
"goji.io/middleware"
"golang.org/x/net/context"
)
var indexTmpl *template.Template
func init() {
data, err := ioutil.ReadFile("./index.html")
if err != nil {
panic(err)
}
tmpl, err := template.New("index").Parse(string(data))
if err != nil {
panic(err)
}
indexTmpl = tmpl
}
type TemplateContext struct {
Payload string
}
func (s *Server) NotFound(h goji.Handler) goji.Handler {
return goji.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if middleware.Handler(ctx) == nil {
session, err := s.RediStore.Get(r, sessionPrefix)
if err != nil {
handleInternalServerError(err, w)
return
}
payload := map[string]interface{}{}
userID, ok := session.Values[usernameKey].(string)
if ok && userID != "" {
payload["userId"] = userID
}
data, err := json.Marshal(payload)
if err != nil {
handleInternalServerError(err, w)
return
}
ctx := TemplateContext{
Payload: string(data),
}
w.WriteHeader(http.StatusOK)
indexTmpl.Execute(w, &ctx)
} else {
h.ServeHTTPC(ctx, w, r)
}
})
}