-
Notifications
You must be signed in to change notification settings - Fork 44
/
gin.go
161 lines (133 loc) · 3.62 KB
/
gin.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
limits "github.com/gin-contrib/size"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
)
// some
const (
USER = "admin"
PWD = "admin"
)
func main() {
r := gin.Default()
store := sessions.NewCookieStore([]byte("jdagldagsdadhsbdgaj"))
store.Options(sessions.Options{
MaxAge: 7200,
Path: "/",
Secure: true,
HttpOnly: true,
})
// 限制文件大小
r.Use(limits.RequestSizeLimiter(4 << 20))
r.Use(sessions.Sessions("httpsgateway", store))
r.NoRoute(func(c *gin.Context) { c.JSON(http.StatusNotFound, "Invaild api request") })
r.POST("login", HandlleLogin)
api := r.Group("api", Auth())
{
api.GET("logout", HandleLogout)
api.GET("hello_world", HandleHelloWorld)
}
t := r.Group("test")
{
t.POST("upload_file", HandleUploadFile)
t.POST("upload_muti_file", HandleUploadMutiFile)
t.GET("download", HandleDownloadFile)
}
// r.RunTLS(":8443", "./cert.pem", "./key.pem")
r.Run(":8888")
}
// Auth doc
func Auth() gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
u := session.Get("user")
if u == nil {
c.JSON(http.StatusUnauthorized, gin.H{"msg": "您暂未登录"})
c.Abort()
return
}
}
}
// HandlleLogin doc
func HandlleLogin(c *gin.Context) {
user := c.PostForm("user")
password := c.PostForm("password")
if user != USER || password != PWD {
c.JSON(http.StatusBadRequest, gin.H{"msg": "用户名或密码不正确"})
return
}
session := sessions.Default(c)
session.Set("user", USER)
session.Save()
c.JSON(http.StatusOK, gin.H{"msg": "login succeed"})
}
// HandleLogout doc
func HandleLogout(c *gin.Context) {
session := sessions.Default(c)
session.Delete("user")
session.Save()
c.JSON(http.StatusOK, gin.H{"data": "See you!"})
}
// HandleHelloWorld doc
func HandleHelloWorld(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"data": "Hello World!"})
}
// HandleUploadFile 上传单个文件
func HandleUploadFile(c *gin.Context) {
file, header, err := c.Request.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "文件上传失败"})
return
}
content, err := ioutil.ReadAll(file)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "文件读取失败"})
return
}
fmt.Println(header.Filename)
fmt.Println(string(content))
c.JSON(http.StatusOK, gin.H{"msg": "上传成功"})
}
// HandleUploadMutiFile 上传多个文件
func HandleUploadMutiFile(c *gin.Context) {
// 限制上传文件大小
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 4<<20)
// 限制放入内存的文件大小
err := c.Request.ParseMultipartForm(4 << 20)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "文件读取失败"})
return
}
formdata := c.Request.MultipartForm
files := formdata.File["file"]
for _, v := range files {
file, err := v.Open()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "文件读取失败"})
return
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"msg": "文件读取失败"})
return
}
fmt.Println(v.Filename)
fmt.Println(string(content))
}
c.JSON(http.StatusOK, gin.H{"msg": "上传成功"})
}
// HandleDownloadFile 下载文件
func HandleDownloadFile(c *gin.Context) {
content := c.Query("content")
content = "hello world, 我是一个文件," + content
c.Writer.WriteHeader(http.StatusOK)
c.Header("Content-Disposition", "attachment; filename=hello.txt")
c.Header("Content-Type", "application/text/plain")
c.Header("Accept-Length", fmt.Sprintf("%d", len(content)))
c.Writer.Write([]byte(content))
}