This commit is contained in:
fallen-angle
2022-01-17 17:38:08 +08:00
parent 0a5379c8f3
commit 2280679053
19 changed files with 283 additions and 19 deletions

24
middleware/cors.go Normal file
View File

@@ -0,0 +1,24 @@
package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
)
func Cors() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin")
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
if method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
}
c.Next()
}
}

View File

@@ -0,0 +1,55 @@
package middleware
import (
"nCovTrack-Backend/utils"
"net"
"net/http"
"net/http/httputil"
"os"
"runtime/debug"
"strings"
"github.com/gin-gonic/gin"
)
func GinRecovery(stack bool) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
var brokenPipe bool
if ne, ok := err.(*net.OpError); ok {
if se, ok := ne.Err.(*os.SyscallError); ok {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
}
}
}
httpRequest, _ := httputil.DumpRequest(c.Request, true)
var logParams []interface{}
logParams = append(logParams,
"uri", c.Request.RequestURI,
"method", c.Request.Method,
"request", string(httpRequest),
"error", interface{}(err),
)
if brokenPipe {
utils.RequestLogPanic(logParams...)
c.Error(err.(error))
c.Abort()
return
}
logParams = append(logParams,
"status", "recovery from panic",
)
if stack {
logParams = append(logParams,
"stack", string(debug.Stack()),
)
}
utils.RequestLogPanic(logParams...)
c.AbortWithStatus(http.StatusInternalServerError)
}
}()
c.Next()
}
}

54
middleware/request_log.go Normal file
View File

@@ -0,0 +1,54 @@
package middleware
import (
"nCovTrack-Backend/utils"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
func RequestLog() gin.HandlerFunc {
return func(c *gin.Context) {
var logParams []interface{}
// All requests' log;
logParams = append(logParams,
"uri", c.Request.RequestURI,
"mothod", c.Request.Method,
)
startTime := time.Now()
c.Next()
cost := time.Since(startTime)
logParams = append(logParams, "cost", cost.String())
// Log while request is allright;
if c.Writer.Status() == http.StatusOK && cost.Milliseconds() < 800 {
utils.RequestLogInfo(logParams...)
return
}
// Log while request cost an unexpected time;
if cost >= 800 {
logParams = append(logParams,
"status", c.Writer.Status(),
"query", c.Request.URL.Query(),
"header", c.Request.Header,
"body", c.Request.Body,
"ip", c.ClientIP(),
)
utils.RequestLogWarn(logParams...)
return
}
// Log while response occurred some error;
if c.Writer.Status() != http.StatusOK {
logParams = append(logParams,
"error", c.Errors.ByType(gin.ErrorTypePrivate).String(),
)
utils.RequestLogError(logParams...)
}
}
}