-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault_middelwares.go
103 lines (91 loc) · 3.8 KB
/
default_middelwares.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
// @author cold bin
// @date 2022/7/27
package mini_web
import (
"fmt"
"log"
"net/http"
"runtime"
"strings"
"time"
"github.com/juju/ratelimit"
)
func Logger() HandlerFunc {
return func(c *Context) {
t := time.Now()
c.Next()
log.Printf("[%d] %s in %v", c.StatusCode, c.Req.RequestURI, time.Since(t))
}
}
func trace(message string) string {
var pcs [32]uintptr
n := runtime.Callers(3, pcs[:]) // skip first 3 caller
var str strings.Builder
str.WriteString(message + "\nTraceback:")
for _, pc := range pcs[:n] {
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
str.WriteString(fmt.Sprintf("\n\t%s:%d", file, line))
}
return str.String()
}
func Recovery() HandlerFunc {
return func(c *Context) {
defer func() {
if err := recover(); err != nil {
message := fmt.Sprintf("%s", err)
log.Printf("%s\n\n", trace(message))
c.Fail(http.StatusInternalServerError, "Internal Server Error")
}
}()
c.Next()
}
}
func Cors() HandlerFunc {
return func(c *Context) {
method := c.Req.Method
origin := c.Req.Header.Get("Origin")
var headerKeys []string
for k, _ := range c.Req.Header {
headerKeys = append(headerKeys, k)
}
headerStr := strings.Join(headerKeys, ", ")
if headerStr != "" {
headerStr = fmt.Sprintf("access-control-allow-origin, access-control-allow-headers, %s", headerStr)
} else {
headerStr = "access-control-allow-origin, access-control-allow-headers"
}
if origin != "" {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.SetHeader("Access-Control-Allow-Origin", "*") // 这是允许访问所有的域,也可以指定某几个特定的域
c.SetHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE") //服务器支持的所有跨域请求的方法,为了避免浏览次请求的多次'预检'请求
//header的类型
c.SetHeader("Access-Control-Allow-Headers", "Authorization, Content-Length, X-CSRF-Token, Token,session,X_Requested_With,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma")
//允许跨域设置 可以返回其他子段
c.SetHeader("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") // 跨域关键设置 让浏览器可以解析
c.SetHeader("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
c.SetHeader("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
c.Set("content-type", "application/json") // 设置返回格式是json
}
////放行所有OPTIONS方法
//if method == "OPTIONS" {
// c.JSON(http.StatusOK, "Options Request!")
//}
if method == "OPTIONS" {
c.Fail(http.StatusNoContent, http.StatusText(http.StatusNoContent))
return
}
// 处理请求
c.Next()
}
}
func RateLimitMiddleware(fillInterval time.Duration, cap int64) func(c *Context) {
bucket := ratelimit.NewBucket(fillInterval, cap)
return func(c *Context) {
if bucket.TakeAvailable(1) < 1 {
c.Fail(http.StatusOK, "rate limit...")
return
}
c.Next()
}
}