-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.go
More file actions
170 lines (146 loc) · 3.86 KB
/
context.go
File metadata and controls
170 lines (146 loc) · 3.86 KB
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
package sola
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
)
func newContext() *context {
return &context{
store: map[string]interface{}{},
}
}
// === Set/Get ===
// Store of Context (include origin)
func (c *context) Store() (s map[string]interface{}) {
if c.origin == nil || c.origin == c {
s = make(map[string]interface{})
} else {
s = c.origin.Store()
}
for k, v := range c.store {
s[k] = v
}
return
}
// Origin Context
func (c *context) Origin() Context {
return c.origin
}
// Shadow Context will search origin context if no match key
func (c *context) Shadow() Context {
shadow := newContext()
shadow.origin = c
return shadow
}
// Set Ctx
func (c *context) Set(key string, value interface{}) {
c.lock.Lock()
defer c.lock.Unlock()
c.store[key] = value
}
// Get Ctx
func (c *context) Get(key string) interface{} {
c.lock.RLock()
defer c.lock.RUnlock()
v := c.store[key]
if v == nil && c.origin != nil {
v = c.origin.Get(key)
}
return v
}
// === API ===
// Sola Impl
func (c *context) Sola() *Sola {
return c.Get(CtxSola).(*Sola)
}
// SetCookie proxy http.SetCookie
func (c *context) SetCookie(cookie *http.Cookie) {
http.SetCookie(c.Response(), cookie)
}
// Request in context
func (c *context) Request() *http.Request {
return c.Get(CtxRequest).(*http.Request)
}
// Response in context
func (c *context) Response() http.ResponseWriter {
return c.Get(CtxResponse).(http.ResponseWriter)
}
// === Writer ===
const (
charsetUTF8 = "charset=UTF-8"
)
// MIME types
const (
MIMEApplicationJSON = "application/json"
MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8
MIMEApplicationJavaScript = "application/javascript"
MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
MIMEApplicationXML = "application/xml"
MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8
MIMETextXML = "text/xml"
MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + charsetUTF8
MIMEApplicationForm = "application/x-www-form-urlencoded"
MIMEApplicationProtobuf = "application/protobuf"
MIMEApplicationMsgpack = "application/msgpack"
MIMETextHTML = "text/html"
MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8
MIMETextPlain = "text/plain"
MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8
MIMEMultipartForm = "multipart/form-data"
MIMEOctetStream = "application/octet-stream"
)
// Blob Writer
func (c *context) Blob(code int, contentType string, bs []byte) (err error) {
w := c.Response()
w.Header().Set("Content-Type", contentType)
w.WriteHeader(code)
_, err = w.Write(bs)
return
}
// HTML Writer
func (c *context) HTML(code int, data string) error {
return c.Blob(code, MIMETextHTMLCharsetUTF8, []byte(data))
}
// String Writer
func (c *context) String(code int, data string) error {
return c.Blob(code, MIMETextPlainCharsetUTF8, []byte(data))
}
// JSON Writer
func (c *context) JSON(code int, data interface{}) error {
bs, err := json.Marshal(data)
if err != nil {
return err
}
return c.Blob(code, MIMEApplicationJSONCharsetUTF8, bs)
}
// File for Reader
type File interface {
io.ReadSeeker
Close() error
Stat() (os.FileInfo, error)
}
// File Writer
func (c *context) File(f File) (err error) {
defer f.Close()
var fi os.FileInfo
if fi, err = f.Stat(); err != nil {
return err
}
http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f)
return
}
// === Reader ===
// JSON Reader
func (c *context) GetJSON(data interface{}) error {
r := c.Request()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
if err = json.Unmarshal(body, data); err != nil {
return err
}
return nil
}