mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-23 15:32:15 +08:00
gin 中间件
This commit is contained in:
parent
9e8ae1c4bc
commit
e358d07a0d
12
gin/internal/middleware/cors.go
Normal file
12
gin/internal/middleware/cors.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func CorsMiddleWare() gin.HandlerFunc {
|
||||||
|
return func(ctx *gin.Context) {
|
||||||
|
ctx.Header("Access-Control-Allow-Origin", "*")
|
||||||
|
ctx.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
|
||||||
|
ctx.Header("Access-Control-Allow-Headers", "Action, Module, X-PINGOTHER, Content-Type, Content-Disposition")
|
||||||
|
ctx.Next()
|
||||||
|
}
|
||||||
|
}
|
38
gin/internal/middleware/error.go
Normal file
38
gin/internal/middleware/error.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
"gin-study/pkg/errorx"
|
||||||
|
)
|
||||||
|
|
||||||
|
// gin 全局异常处理
|
||||||
|
func ErrorHandler() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
err := parseError(r)
|
||||||
|
//打印错误堆栈信息
|
||||||
|
debug.PrintStack()
|
||||||
|
//封装通用JSON返回
|
||||||
|
c.JSON(http.StatusOK, errorx.NewDefaultError(err))
|
||||||
|
//终止后续接口调用,不加的话recover到异常后,还会继续执行接口里后续代码
|
||||||
|
c.Abort()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
//加载完 defer recover,继续后续的插件及代码执行
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseError(r interface{}) string {
|
||||||
|
switch v := r.(type) {
|
||||||
|
case error:
|
||||||
|
return v.Error()
|
||||||
|
default:
|
||||||
|
return r.(string)
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"gin-study/internal/controller"
|
"gin-study/internal/controller"
|
||||||
|
"gin-study/internal/middleware"
|
||||||
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -19,6 +20,8 @@ func main() {
|
|||||||
// 注册 favicon
|
// 注册 favicon
|
||||||
e.Use(favicon.New("./templates/favicon.ico"))
|
e.Use(favicon.New("./templates/favicon.ico"))
|
||||||
e.Use(gin.Recovery())
|
e.Use(gin.Recovery())
|
||||||
|
e.Use(middleware.CorsMiddleWare())
|
||||||
|
e.Use(middleware.ErrorHandler())
|
||||||
|
|
||||||
controller.Register(e)
|
controller.Register(e)
|
||||||
|
|
||||||
|
57
gin/pkg/errorx/errorx.go
Normal file
57
gin/pkg/errorx/errorx.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package errorx
|
||||||
|
|
||||||
|
import "gin-study/pkg/response"
|
||||||
|
|
||||||
|
type CodeError struct {
|
||||||
|
*response.Response
|
||||||
|
}
|
||||||
|
|
||||||
|
type CodeErrorResponse struct {
|
||||||
|
*response.Response
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCustomError(code int, data interface{}, msg string) error {
|
||||||
|
return &CodeError{
|
||||||
|
Response: response.NewCustomResponse(code, data, msg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewErrorWithCode(code int, msg string) error {
|
||||||
|
return &CodeError{
|
||||||
|
Response: response.NewCustomResponse(code, nil, msg),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseError(err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return NewErrorWithCode(response.ERROR, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseErrorWithCode(code int, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return NewErrorWithCode(code, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDefaultError(msg string) error {
|
||||||
|
return NewErrorWithCode(response.ERROR, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *CodeError) Error() string {
|
||||||
|
return e.Msg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *CodeError) Resp() *CodeErrorResponse {
|
||||||
|
return &CodeErrorResponse{
|
||||||
|
Response: &response.Response{
|
||||||
|
Code: e.Code,
|
||||||
|
Data: e.Data,
|
||||||
|
Msg: e.Msg,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
40
gin/pkg/response/response.go
Normal file
40
gin/pkg/response/response.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package response
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Code Code `json:"code" example:"200"`
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
Msg string `json:"msg" example:"OK"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Code = int
|
||||||
|
|
||||||
|
const (
|
||||||
|
SUCCESS = 200
|
||||||
|
UNAUTHORIZED = 401
|
||||||
|
FORBIDDEN = 403
|
||||||
|
ERROR = 500
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewCustomResponse(code int, data interface{}, msg string) *Response {
|
||||||
|
return &Response{
|
||||||
|
Code: code,
|
||||||
|
Data: data,
|
||||||
|
Msg: msg,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewResponseWithCode(code int, data interface{}) *Response {
|
||||||
|
return &Response{
|
||||||
|
Code: SUCCESS,
|
||||||
|
Data: data,
|
||||||
|
Msg: "OK",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewResponse(data interface{}) *Response {
|
||||||
|
return &Response{
|
||||||
|
Code: SUCCESS,
|
||||||
|
Data: data,
|
||||||
|
Msg: "OK",
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user