-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.go
123 lines (115 loc) · 3.1 KB
/
router.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
package main
import (
"encoding/json"
"github.com/julienschmidt/httprouter"
"net/http"
"strconv"
)
// Write 输出返回结果
func Write(w http.ResponseWriter, response []byte) {
//公共的响应头设置
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, OPTIONS")
w.Header().Set("Content-Type", "application/json;charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(string(response))))
_, _ = w.Write(response)
return
}
// Ping 检测服务连通性
func Ping(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
response, _ := json.Marshal(struct {
Ping string `json:"ping"`
}{
Ping: "PONG",
})
Write(w, response)
}
// TokenAuth 检测Token合法性
func TokenAuth(token string) bool {
if token != config.Token {
return false
}
return true
}
// Login 登录
func Login(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if !TokenAuth(r.URL.Query().Get("token")) {
response, _ := json.Marshal(struct {
Code int `json:"code"`
Errors string `json:"errors"`
}{
Code: 500,
Errors: "token error",
})
Write(w, response)
}
response, _ := json.Marshal(struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}{
Code: 200,
Message: "ok",
Data: config.Token,
})
Write(w, response)
}
// GetUploadAPI 获取快捷上传接口
func GetUploadAPI(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
response, _ := json.Marshal(struct {
Code int `json:"code"`
Utoken string `json:"utoken"`
Url interface{} `json:"url"`
}{
Code: 200,
Utoken: config.UToken,
Url: config.Default,
})
Write(w, response)
}
// API 请求处理
func API(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
// 检测token 为空或验证不通过
if r.Header.Get("utoken") == config.UToken {
goto UNCHECK
}
if r.Header.Get("token") == "" || !TokenAuth(r.Header.Get("token")) {
response, _ := json.Marshal(struct {
Errors string `json:"errors"`
}{
Errors: "token error",
})
Write(w, response)
return
}
UNCHECK:
var result []byte
if ps.ByName("type") == "cos" {
result = CosHandler(r)
} else if ps.ByName("type") == "oss" {
result = OssHandler(r)
} else if ps.ByName("type") == "ups" {
result = UpsHandler(r)
}
Write(w, result)
}
func Router() *httprouter.Router {
router := httprouter.New()
router.GlobalOPTIONS = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set CORS headers
header := w.Header()
header.Set("Access-Control-Allow-Origin", "*")
header.Set("Access-Control-Allow-Headers", "*")
header.Set("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, OPTIONS")
// Adjust status code to 204
w.WriteHeader(http.StatusNoContent)
})
router.GET("/ping", Ping)
router.GET("/#", Login)
router.GET("/get_upload_config", GetUploadAPI)
router.GET("/api/:type", API)
router.POST("/api/:type", API)
router.NotFound = http.FileServer(http.Dir("dist"))
return router
}