-
Notifications
You must be signed in to change notification settings - Fork 52
/
render.go
262 lines (235 loc) · 5.42 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* @Author: seekwe
* @Date: 2019-05-23 19:16:32
* @Last Modified by: seekwe
* @Last Modified time: 2019-05-29 15:40:30
*/
package znet
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"mime"
"net/http"
"os"
"path/filepath"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/zutil"
)
type (
render interface {
Content(c *Context) (content []byte)
}
renderByte struct {
Data []byte
Type string
ContentDate []byte
}
renderString struct {
Format string
Data []interface{}
ContentDate []byte
}
renderJSON struct {
Data interface{}
ContentDate []byte
}
renderFile struct {
Data string
FileExist bool
ContentDate []byte
}
renderHTML struct {
Template *template.Template
Name string
Data interface{}
ContentDate []byte
FuncMap template.FuncMap
}
ApiData struct {
Code int `json:"code" example:"200"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
Data map[string]interface{}
PrevData struct {
Code int
Type string
Content []byte
}
)
var (
ContentTypePlain = "text/plain; charset=utf-8"
ContentTypeHTML = "text/html; charset=utf-8"
ContentTypeJSON = "application/json; charset=utf-8"
)
func (c *Context) renderProcessing(code int, r render) {
c.Lock()
c.Code = code
c.render = r
c.StopHandle = true
c.Unlock()
}
func (r *renderByte) Content(c *Context) []byte {
c.SetContentType(zutil.IfVal(r.Type != "", r.Type, ContentTypePlain).(string))
return r.Data
}
func (r *renderString) Content(c *Context) []byte {
if r.ContentDate != nil {
return r.ContentDate
}
c.SetContentType(ContentTypePlain)
if len(r.Data) > 0 {
r.ContentDate = zstring.String2Bytes(fmt.Sprintf(r.Format, r.Data...))
} else {
r.ContentDate = zstring.String2Bytes(r.Format)
}
return r.ContentDate
}
func (r *renderJSON) Content(c *Context) []byte {
if r.ContentDate != nil {
return r.ContentDate
}
c.SetContentType(ContentTypeJSON)
bf := zutil.GetBuff()
defer zutil.PutBuff(bf)
jsonEncoder := json.NewEncoder(bf)
jsonEncoder.SetEscapeHTML(false)
if err := jsonEncoder.Encode(r.Data); err != nil {
r.ContentDate, _ = json.Marshal(r.Data)
} else {
r.ContentDate = bf.Bytes()
}
return r.ContentDate
}
func (r *renderFile) Content(c *Context) []byte {
if !r.FileExist {
return []byte{}
}
if r.ContentDate != nil {
return r.ContentDate
}
fType := mime.TypeByExtension(filepath.Ext(r.Data))
c.SetContentType(fType)
r.ContentDate, _ = ioutil.ReadFile(r.Data)
return r.ContentDate
}
func (r *renderHTML) Content(c *Context) []byte {
if r.ContentDate != nil {
return r.ContentDate
}
c.SetContentType(ContentTypeHTML)
if r.Name != "" {
var t *template.Template
var err error
t, err = templateParse(r.Name, r.FuncMap)
if err != nil {
panic(err)
}
var buf bytes.Buffer
err = t.Execute(&buf, r.Data)
if err != nil {
panic(err)
}
r.ContentDate = buf.Bytes()
} else {
r.ContentDate = zstring.String2Bytes(fmt.Sprint(r.Data))
}
return r.ContentDate
}
func (c *Context) Byte(code int, value []byte) {
c.renderProcessing(code, &renderByte{Data: value})
}
func (c *Context) String(code int, format string, values ...interface{}) {
c.renderProcessing(code, &renderString{Format: format, Data: values})
}
func (c *Context) SetContent(data *PrevData) {
c.renderProcessing(data.Code, &renderByte{Data: data.Content, Type: data.Type})
}
func (c *Context) File(path string) {
fileExist := zfile.FileExist(path)
code := zutil.IfVal(fileExist, 200, 404).(int)
if fileExist {
if f, err := os.Stat(path); err == nil {
c.SetHeader("Last-Modified", f.ModTime().UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT"))
}
}
c.renderProcessing(code, &renderFile{Data: path, FileExist: fileExist})
}
func (c *Context) JSON(code int, values interface{}) {
c.renderProcessing(code, &renderJSON{Data: values})
}
// ApiJSON ApiJSON
func (c *Context) ApiJSON(code int, msg string, data interface{}) {
c.renderProcessing(http.StatusOK, &renderJSON{Data: ApiData{Code: code, Data: data,
Msg: msg}})
}
// HTML export html
func (c *Context) HTML(code int, html string) {
c.renderProcessing(code, &renderHTML{
Name: "",
Data: html,
})
}
// Template export template
func (c *Context) Template(code int, name string, data interface{}, funcMap ...map[string]interface{}) {
var fn template.FuncMap
if len(funcMap) > 0 {
fn = funcMap[0]
}
c.renderProcessing(code, &renderHTML{
Name: name,
Data: data,
FuncMap: fn,
})
}
// Abort Abort
func (c *Context) Abort(code ...int) {
c.Lock()
c.StopHandle = true
c.Unlock()
if len(code) > 0 {
c.SetStatus(code[0])
}
}
// Redirect Redirect
func (c *Context) Redirect(link string, statusCode ...int) {
c.Writer.Header().Set("Location", c.CompletionLink(link))
code := http.StatusFound
if len(statusCode) > 0 {
code = statusCode[0]
}
c.SetStatus(code)
}
func (c *Context) SetStatus(code int) *Context {
c.Lock()
c.Code = code
c.Unlock()
return c
}
func (c *Context) SetContentType(contentType string) *Context {
c.SetHeader("Content-Type", contentType)
return c
}
func (c *Context) PrevContent() *PrevData {
c.RLock()
r := c.render
code := c.Code
ctype, hasType := c.heades["Content-Type"]
c.RUnlock()
if !hasType {
ctype = ContentTypePlain
}
var content []byte
if r != nil {
content = r.Content(c)
}
return &PrevData{
Code: code,
Type: ctype,
Content: content,
}
}