-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
86 lines (66 loc) · 1.82 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
package goa
import (
"encoding/json"
"errors"
"strings"
)
// Context 中间件的上下文对象, 包含了请求和响应对象
type Context struct {
Request Request
Response Response
}
// SID 获取请求头的中的SID
func (ctx *Context) SID() (string, error) {
authorizationHeaders, ok := ctx.Request.Header["Authorization"]
if !ok || len(authorizationHeaders) == 0 {
return "", errors.New("请求头中缺少Authorization字段")
}
if strings.Compare("", authorizationHeaders[0]) == 0 {
return "", errors.New("请求头中有Authorization字段, 但为空")
}
return authorizationHeaders[0], nil
}
// JSON 返回JSON格式的响应数据
func (ctx *Context) JSON(m interface{}) error {
w := ctx.Response
headers := w.Header()
headers.Set("Content-Type", "application/json")
encoder := json.NewEncoder(ctx.Response.ResponseWriter)
err := encoder.Encode(m)
if err != nil {
ctx.Error(err)
return err
}
return nil
}
type format struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
// FormatJSON 格式化输出
func (ctx *Context) FormatJSON(code int, message string, data interface{}) error {
ret := format{code, message, data}
return ctx.JSON(ret)
}
// Success 返回成功
func (ctx *Context) Success(data interface{}) error {
return ctx.FormatJSON(0, "SUCCESS", data)
}
// Fail 返回失败
func (ctx *Context) Fail(message string) error {
return ctx.FormatJSON(1, message, nil)
}
// String 返回String格式的响应数据
func (ctx *Context) String(content string) {
w := ctx.Response
headers := w.Header()
headers.Set("Content-Type", "text/plain")
ctx.Response.Write([]byte(content))
}
// Error 返回一个错误
func (ctx *Context) Error(err error) {
w := ctx.Response
w.WriteHeader(500)
ctx.Response.Write([]byte(err.Error()))
}