-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
123 lines (82 loc) · 2.24 KB
/
render.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
package render
import (
log "github.com/Sirupsen/logrus"
"github.com/flosch/pongo2"
_ "github.com/postgres-ci/app-server/src/tools/render/filters"
"github.com/postgres-ci/http200ok"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
)
type Context map[string]interface{}
var templates = make(map[string]*pongo2.Template)
func Init(root string) error {
if _, err := os.Open(root); err != nil {
return err
}
return filepath.Walk(root, func(path string, fi os.FileInfo, _ error) error {
if fi == nil || fi.IsDir() {
return nil
}
if name, err := filepath.Rel(root, path); err == nil {
template, err := pongo2.FromFile(path)
if err != nil {
return err
}
templates[name] = template
} else {
return err
}
return nil
})
}
func HTML(c *http200ok.Context, name string, context Context) error {
if currentUser := c.Get("CurrentUser"); currentUser != nil {
if context == nil {
context = make(Context)
}
context["CurrentUser"] = currentUser
}
if template, found := templates[name]; found {
if err := template.ExecuteWriterUnbuffered(pongo2.Context(context), c.Response); err != nil {
log.Errorf("Render: %v", err)
return err
}
return nil
}
log.Errorf("Template '%s' not found", name)
return fmt.Errorf("template '%s' not found", name)
}
func NotFound(c *http200ok.Context) {
http.NotFound(c.Response, c.Request)
}
func InternalServerError(c *http200ok.Context, err error) {
c.Response.WriteHeader(http.StatusInternalServerError)
}
func JSON(c *http200ok.Context, v interface{}) error {
c.Response.Header().Add("Content-Type", "application/json")
return json.NewEncoder(c.Response).Encode(v)
}
func JSONok(c *http200ok.Context) {
JSON(c, struct {
Success bool `json:"success"`
}{
Success: true,
})
}
func JSONError(c *http200ok.Context, code int, format string, a ...interface{}) error {
c.Response.Header().Add("Content-Type", "application/json")
c.Response.Header().Set("X-Content-Type-Options", "nosniff")
c.Response.WriteHeader(code)
return json.NewEncoder(c.Response).Encode(struct {
Success bool `json:"success"`
Code int `json:"code"`
Error string `json:"error"`
}{
Success: false,
Code: code,
Error: fmt.Sprintf(format, a...),
})
}