-
Notifications
You must be signed in to change notification settings - Fork 13
/
view.go
147 lines (127 loc) · 3.03 KB
/
view.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
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"fmt"
"html/template"
"net/http"
"path/filepath"
"sync"
)
type registry struct {
sync.RWMutex
templates map[string]*template.Template
js map[string]string
}
var r registry
func init() {
r.templates = make(map[string]*template.Template)
r.js = make(map[string]string)
}
func Render(resp http.ResponseWriter, req *http.Request, view string, data interface{}) (err error) {
body, err := render(req, "layout", view, data)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
resp.Write(body)
return
}
func RenderError(resp http.ResponseWriter, req *http.Request, message string, code int) (err error) {
body, err := render(req, "layout", "error", map[string]string{"Error": message})
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
resp.WriteHeader(code)
resp.Write(body)
return
}
func RenderJsonError(resp http.ResponseWriter, req *http.Request, message string, code int) (err error) {
resp.WriteHeader(code)
resp.Write([]byte(fmt.Sprintf("{\"error\":\"%s\"}", message)))
return
}
func render(req *http.Request, layout string, name string, data interface{}) (body []byte, err error) {
file := "views/" + name + ".html"
view, err := parse(req, file, data)
if err != nil {
return
}
r.RLock()
javascript, _ := r.js[file]
r.RUnlock()
body, err = parse(req, "views/layouts/"+layout+".html", map[string]template.HTML{
"Content": template.HTML(view),
"Javascript": template.HTML(javascript),
})
if err != nil {
return
}
return
}
func parse(req *http.Request, file string, data interface{}) (body []byte, err error) {
r.RLock()
t, present := r.templates[file]
r.RUnlock()
var jsFiles []string
if !present {
buildURL := func(name string, full bool, args ...string) (urlString string) {
route := router.Get(name)
if route == nil {
return ""
}
url, err := route.URL(args...)
if err != nil {
return ""
}
urlString = url.String()
if full {
if urlString[0:1] != "/" {
urlString = "/" + urlString
}
urlString = "http://" + req.Host + urlString
}
return
}
t = template.New(filepath.Base(file))
t.Funcs(template.FuncMap{
"full_url": func(name string, args ...string) string {
return buildURL(name, true, args...)
},
"url": func(name string, args ...string) string {
return buildURL(name, false, args...)
},
"load_js": func(file string, args ...string) string {
if file != "" {
jsFiles = append(jsFiles, file)
}
return ""
},
})
_, err = t.ParseFiles(file)
if err != nil {
return
}
r.Lock()
r.templates[file] = t
r.Unlock()
}
var buf bytes.Buffer
err = t.Execute(&buf, data)
if err != nil {
return
}
if !present && len(jsFiles) > 0 {
javascript := ""
for i, file := range jsFiles {
if i > 0 {
javascript += "\n"
}
javascript += fmt.Sprintf("<script type=\"text/javascript\" src=\"%s\"></script>", file)
}
r.Lock()
r.js[file] = javascript
r.Unlock()
}
return buf.Bytes(), nil
}