/
context.go
146 lines (128 loc) · 3.14 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
// Package core @Author Bing
// @Date 2023/10/7 17:04:00
// @Desc
package core
import (
"encoding/json"
"fmt"
"github.com/learnselfs/gee/config"
"github.com/learnselfs/gee/utils"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
type (
// Context
// @Description: request response Context management
Context struct {
engine *Engine
//request
r *http.Request `info:"request"`
path string `info:"path"`
method string `info:"method"`
params map[string]interface{} `info:"params"`
maxMultipartMemory int64 `info:"maxMultipartMemory"`
middleware []Handle
index int `info:"index"`
// response
w http.ResponseWriter `info:"response"`
stateCode int `info:"stateCode"`
}
)
func newContext(w http.ResponseWriter, r *http.Request) *Context {
return &Context{
r: r,
method: r.Method,
path: r.URL.Path,
middleware: make([]Handle, 0),
index: -1,
w: w,
params: make(config.H, 0),
maxMultipartMemory: 8 << 20,
}
}
func (c *Context) SetHeader(rType string) {
c.w.Header().Set("Content-Type", rType)
}
func (c *Context) JSON(obj config.H) {
utils.Log.Println(c.method, c.r.URL.Path, obj)
c.SetHeader("application/json")
buf := json.NewEncoder(c.w)
err := buf.Encode(obj)
if err != nil {
utils.Log.Fatalln(err)
}
}
func (c *Context) HTML(code int, name string, data any) {
c.stateCode = code
c.SetHeader("text/html")
err := c.engine.template.ExecuteTemplate(c.w, name, data)
if err != nil {
utils.Log.Fatalln(err)
return
}
}
func (c *Context) String(code int, format string, data ...config.H) {
c.SetHeader("text/plain")
c.stateCode = code
_, i := c.w.Write([]byte(fmt.Sprintf(format, data)))
if i != nil {
return
}
}
func (c *Context) Data(code int, data []byte) {
c.stateCode = code
_, err := c.w.Write(data)
if err != nil {
return
}
}
func (c *Context) Param(key string) any {
return c.params[key]
}
func (c *Context) PostForm(key string) string {
return c.r.PostFormValue(key)
}
func (c *Context) PostFile(name string) (*multipart.FileHeader, error) {
if c.r.MultipartForm == nil {
if err := c.r.ParseMultipartForm(c.maxMultipartMemory); err != nil {
return nil, err
}
}
_, fileHeader, err := c.r.FormFile(name)
if err != nil {
return nil, err
}
return fileHeader, nil
}
func (c *Context) MultipartFile() (*multipart.Form, error) {
err := c.r.ParseMultipartForm(c.maxMultipartMemory)
return c.r.MultipartForm, err
}
func (c *Context) SaveUploadFile(fileHeader *multipart.FileHeader, fp string) error {
source, err := fileHeader.Open()
if err != nil {
return err
}
defer source.Close()
err = os.MkdirAll(filepath.Dir(fp), 0750)
if err != nil {
return err
}
destination, err := os.Create(fp)
if err != nil {
return err
}
defer destination.Close()
_, err = io.Copy(destination, source)
return err
}
func (c *Context) Next() {
c.index++
length := len(c.middleware)
for ; c.index < length; c.index++ {
c.middleware[c.index](c)
}
}