-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_context.go
197 lines (170 loc) · 4.52 KB
/
http_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package gohttp
import (
"encoding/json"
"encoding/xml"
"io"
"log"
"net/http"
"time"
)
type HttpContext interface {
io.Closer
BasicContext
RequestReader
ResponseWriter
}
type BasicContext interface {
HttpRequest() *http.Request
HttpResponseWriter() http.ResponseWriter
Session() Session
}
type Session interface {
Exists(key string) bool
Get(key string) interface{}
Set(key string, value interface{}, expiration time.Duration)
Delete(key string) bool
}
type RequestReader interface {
GetQueryStrings() map[string][]string
GetQueryStringValues(key string) []string
GetQueryStringValue(key string) string
GetRequestBodyAsBytes() ([]byte, error)
GetRequestBodyAsString() (string, error)
GetRequestBodyAsXml(v interface{}) error
GetRequestBodyAsJson(v interface{}) error
}
type ResponseWriter interface {
HttpError(statusCode int)
Redirect(url string)
String(response string)
Xml(r interface{})
Json(r interface{})
}
func NewHttpContext(w http.ResponseWriter, r *http.Request, sm SessionManager) HttpContext {
return &httpContext{
responseWriter: w,
request: r,
sessionManager: sm,
}
}
type httpContext struct {
responseWriter http.ResponseWriter
request *http.Request
sessionManager SessionManager
}
func (c *httpContext) Close() error {
return nil
}
func (c *httpContext) HttpRequest() *http.Request {
return c.request
}
func (c *httpContext) HttpResponseWriter() http.ResponseWriter {
return c.responseWriter
}
const CookieSession = "SESSION"
func (c *httpContext) Session() Session {
var session Session
cookie, err := c.request.Cookie(CookieSession)
if err == nil {
session = c.sessionManager.GetSession(cookie.Value)
}
var sid string
if session == nil {
sid, session = c.sessionManager.CreateSession()
cookie := http.Cookie{Name: CookieSession, Value: sid, Path: "/"}
if c.request.URL.Scheme == "https" {
cookie.SameSite = http.SameSiteNoneMode
cookie.Secure = true
}
http.SetCookie(c.responseWriter, &cookie)
}
return session
}
func (c *httpContext) GetQueryStrings() map[string][]string {
return c.request.URL.Query()
}
func (c *httpContext) GetQueryStringValues(key string) []string {
v, ok := c.GetQueryStrings()[key]
if !ok {
return nil
}
return v
}
func (c *httpContext) GetQueryStringValue(key string) string {
v := c.GetQueryStringValues(key)
if len(v) == 0 {
return ""
}
return v[0]
}
func (c *httpContext) GetRequestBodyAsBytes() ([]byte, error) {
b, err := io.ReadAll(c.request.Body)
if err != nil {
return nil, err
}
return b, nil
}
func (c *httpContext) GetRequestBodyAsString() (string, error) {
bytes, err := c.GetRequestBodyAsBytes()
if err != nil {
return "", err
}
return string(bytes), nil
}
func (c *httpContext) GetRequestBodyAsXml(v interface{}) error {
bytes, err := c.GetRequestBodyAsBytes()
if err != nil {
return err
}
return xml.Unmarshal(bytes, v)
}
func (c *httpContext) GetRequestBodyAsJson(v interface{}) error {
bytes, err := c.GetRequestBodyAsBytes()
if err != nil {
return err
}
return json.Unmarshal(bytes, v)
}
func (c *httpContext) HttpError(statusCode int) {
c.responseWriter.WriteHeader(statusCode)
}
func (c *httpContext) Redirect(url string) {
c.responseWriter.Header().Add("Location", url)
c.responseWriter.WriteHeader(http.StatusTemporaryRedirect)
}
func (c *httpContext) String(response string) {
c.responseWriter.Header().Add("Content-Type", "text/plain; charset=utf-8")
c.responseWriter.WriteHeader(http.StatusOK)
_, err := c.responseWriter.Write([]byte(response))
if err != nil {
log.Println("failed to write response", err.Error(), response)
}
}
func (c *httpContext) Xml(r interface{}) {
xmlString, err := xml.Marshal(r)
if err != nil {
log.Println("failed to encode xml", err.Error(), r)
c.responseWriter.WriteHeader(http.StatusInternalServerError)
return
}
c.responseWriter.Header().Add("Content-Type", "application/xml; charset=utf-8")
c.responseWriter.WriteHeader(http.StatusOK)
_, err = c.responseWriter.Write(xmlString)
if err != nil {
log.Println("failed to write response", err.Error(), string(xmlString))
}
}
func (c *httpContext) Json(r interface{}) {
jsonString, err := json.Marshal(r)
if err != nil {
log.Println("failed to encode json", err.Error(), r)
c.responseWriter.WriteHeader(http.StatusInternalServerError)
return
}
c.responseWriter.Header().Add("Content-Type", "application/json; charset=utf-8")
c.responseWriter.WriteHeader(http.StatusOK)
_, err = c.responseWriter.Write(jsonString)
if err != nil {
log.Println("failed to write response", err.Error(), jsonString)
}
}