This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.go
207 lines (170 loc) · 4.89 KB
/
base.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
package kwiscale
import (
"encoding/json"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"os"
"github.com/gorilla/mux"
)
// PayloadType represents a payload type for Websocket.
type PayloadType int
const (
// JSON payload type in Websocket.
JSON PayloadType = iota
// BYTES payload type in Websocket.
BYTES
// STRING payload type in Websocket.
STRING
)
// Enable debug logs.
var debug = false
// SetDebug changes debug mode.
func SetDebug(mode bool) {
debug = mode
}
// WebHandler is the main handler interface that every handler sould
// implement.
type WebHandler interface {
setVars(map[string]string, http.ResponseWriter, *http.Request)
setApp(*App)
App() *App
setRoute(*mux.Route)
getRequest() *http.Request
getResponse() http.ResponseWriter
Request() *http.Request
Response() http.ResponseWriter
GetSession(interface{}) (interface{}, error)
SetSession(interface{}, interface{})
setSessionStore(SessionStore)
Init() (status int, message error)
Destroy()
URL(...string) (*url.URL, error)
// GetApp() *App // deprecated
// GetURL(...string) (*url.URL, error) // deprecated
// GetRequest() *http.Request // deprecated
// GetResponse() http.ResponseWriter // deprecated
}
// BaseHandler is the parent struct of every Handler.
// Implement WebHandler.
type BaseHandler struct {
response http.ResponseWriter
request *http.Request
Vars map[string]string
sessionStore SessionStore
route *mux.Route
app *App
routepath string
}
// Init is called before the begin of response (before Get, Post, and so on).
// If error is not nil, framework will write response with the second argument as http status.
func (b *BaseHandler) Init() (int, error) {
return -1, nil
}
// Destroy is called as defered function after response.
func (b *BaseHandler) Destroy() {}
// setVars initialize vars from url
func (b *BaseHandler) setVars(v map[string]string, w http.ResponseWriter, req *http.Request) {
b.Vars = v
b.response = w
b.request = req
}
// setApp assign App to the handler
func (b *BaseHandler) setApp(a *App) {
b.app = a
}
// setRoute register mux.Route in the handler.
func (b *BaseHandler) setRoute(r *mux.Route) {
b.route = r
}
// getReponse returns the current response.
func (b *BaseHandler) getResponse() http.ResponseWriter {
return b.response
}
// getRequest returns the current request.
func (b *BaseHandler) getRequest() *http.Request {
return b.request
}
// Response returns the current response.
func (b *BaseHandler) Response() http.ResponseWriter {
return b.getResponse()
}
// Request returns the current request.
func (b *BaseHandler) Request() *http.Request {
return b.getRequest()
}
// SetSessionStore defines the session store to use.
func (b *BaseHandler) setSessionStore(store SessionStore) {
b.sessionStore = store
}
// GetSession return the session value of "key".
func (b *BaseHandler) GetSession(key interface{}) (interface{}, error) {
return b.sessionStore.Get(b, key)
}
// SetSession set the "key" session to "value".
func (b *BaseHandler) SetSession(key interface{}, value interface{}) {
b.sessionStore.Set(b, key, value)
}
// CleanSession remove every key/value of the current session.
func (b *BaseHandler) CleanSession() {
b.sessionStore.Clean(b)
}
// Payload returns the Body content.
func (b *BaseHandler) Payload() []byte {
content, err := ioutil.ReadAll(b.request.Body)
if err != nil {
return nil
}
return content
}
// JSONPayload unmarshal body to the "v" interface.
func (b *BaseHandler) JSONPayload(v interface{}) error {
return json.Unmarshal(b.Payload(), v)
}
// PostValue returns the post data for the given "name" argument.
// If POST value is empty, return "def" instead. If no "def" is provided, return an empty string by default.
func (b *BaseHandler) PostValue(name string, def ...string) string {
if res := b.request.PostFormValue(name); res != "" {
return res
}
if len(def) > 0 {
return def[0]
}
return ""
}
// PostValues returns the entire posted values.
func (b *BaseHandler) PostValues() url.Values {
b.request.ParseForm()
return b.request.PostForm
}
// GetPostFile returns the "name" file pointer and information from the post data.
func (b *BaseHandler) GetPostFile(name string) (multipart.File, *multipart.FileHeader, error) {
b.request.ParseForm()
return b.request.FormFile(name)
}
// SavePostFile save the given "name" file to the "to" path.
func (b *BaseHandler) SavePostFile(name, to string) error {
b.request.ParseForm()
file, _, err := b.GetPostFile(name)
if err != nil {
return err
}
defer file.Close()
out, err := os.Create(to)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, file)
return err
}
// URL return an url based on the declared route and given string pair.
func (b *BaseHandler) URL(s ...string) (*url.URL, error) {
return b.route.URL(s...)
}
// App returns the current application.
func (b *BaseHandler) App() *App {
return b.app
}