/
context.go
179 lines (147 loc) · 4.13 KB
/
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
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
// Copyright (c) 2017 AnUnnamedProject
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
package framework
import (
"encoding/json"
"fmt"
"net/http"
"runtime/debug"
"strings"
"io/ioutil"
"github.com/AnUnnamedProject/i18n"
)
type (
// Context wraps Request and Response. It also provides methods for handling responses.
Context struct {
Request *Request
Response ResponseWriter
Router *Router
Params map[string]string
meta map[string]string
Session Session
I18n *i18n.I18N
Shared map[string]interface{}
Data map[string]interface{}
}
)
// NewContext creates new context for the given w and r.
func NewContext(w ResponseWriter, r *Request) *Context {
return &Context{
Request: r,
Response: w,
Data: make(map[string]interface{}),
Params: make(map[string]string),
meta: make(map[string]string),
Shared: make(map[string]interface{}),
}
}
// Reset the context information.
func (c *Context) Reset() {
c.Data = make(map[string]interface{})
c.Params = make(map[string]string)
c.meta = make(map[string]string)
}
// Header sets or deletes the response headers.
func (c *Context) Header(key, value string) {
if value == "" {
c.Response.Header().Del(key)
} else {
c.Response.Header().Set(key, value)
}
}
// Plain sends a string.
func (c *Context) Plain(code int, s string) {
c.Response.WriteHeader(code)
_, _ = c.Response.Write([]byte(s))
}
// JSON renders as application/json with the given code.
func (c *Context) JSON(code int, obj interface{}) {
header := c.Response.Header()
header["Content-Type"] = []string{"application/json; charset=utf-8"}
c.Response.WriteHeader(code)
_ = json.NewEncoder(c.Response).Encode(obj)
}
// Redirect the request to other url location.
func (c *Context) Redirect(status int, url string) {
http.Redirect(c.Response, c.Request.Request, url, status)
}
// Render outputs using specified template.
func (c *Context) Render(name string) {
if len(c.meta) > 0 {
c.Data["FrameworkMeta"] = c.meta
}
c.Data["Request"] = c.Request
c.Data["i18n"] = c.I18n
if len(App.sharedData) > 0 {
for key, value := range App.sharedData {
c.Data[key] = value
}
}
_ = App.View.Render(c.Response, name, c.Data)
}
// Meta sets or deletes the meta tags.
func (c *Context) Meta(key, value string) {
if value == "" {
delete(c.meta, key)
} else {
c.meta[key] = value
}
}
// ParseForm detects the post Content-Type and prepares the form fields.
// If the Content-Type is application/json the values are decoded into the request JSON and JSONRaw params.
func (c *Context) ParseForm() error {
c.Request.JSON = &JSONData{}
c.Request.JSONRaw = ""
_ = c.Request.ParseForm()
if strings.Contains(c.Request.Header.Get("Content-Type"), "application/json") {
b, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
return fmt.Errorf("error reading body: %v\n", err)
}
if len(b) > 0 {
parsedJSON, err := PrepareJSON(b)
if err != nil {
_ = c.Request.Body.Close()
return fmt.Errorf("invalid JSON: %v\n", err)
}
_ = c.Request.Body.Close()
c.Request.JSON = parsedJSON
c.Request.JSONRaw = string(b)
}
}
return nil
}
// ParseJSON parse the body into your defined structure.
func (c *Context) ParseJSON(v interface{}) error {
b, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
return fmt.Errorf("error reading body: %v\n", err)
}
err = json.Unmarshal(b, &v)
if err != nil {
_ = c.Request.Body.Close()
return fmt.Errorf("invalid JSON: %v\n", err)
}
return nil
}
// Error manage the error based on code
func (c *Context) Error(code int, err error) {
// In case of error 500, log as critical and print stack trace
if code == http.StatusInternalServerError {
Log.Critical(fmt.Sprintf("%v", err))
debug.PrintStack()
} else {
Log.Error(err)
}
c.Plain(code, err.Error())
}
// AddShared append value to context shared values
func (c *Context) AddShared(key string, value interface{}) {
c.Shared[key] = value
}
// GetShared get value from context shared values
func (c *Context) GetShared(key string) interface{} {
return c.Shared[key]
}
// ParseUploads