Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

黄成意 / go_sku_server

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Settings
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Find file
Normal viewHistoryPermalink
Switch branch/tag
  • go_sku_server
  • controller
  • middleware_controller.go
middleware_controller.go 1.7 KB
huangchengyi's avatar
1.0
59acfdf8
 
huangchengyi committed 4 years ago
1 2 3
package controller

import (
wang's avatar
新增sku进度-spu新增
76ecfcc4
 
wang committed 4 years ago
4
	"fmt"
huangchengyi's avatar
1.0
59acfdf8
 
huangchengyi committed 4 years ago
5
	"github.com/gin-gonic/gin"
huangchengyi's avatar
1.0
f238237a
 
huangchengyi committed 4 years ago
6
	"go_sku_server/framework/gin_"
wang's avatar
新增sku进度-spu新增
76ecfcc4
 
wang committed 4 years ago
7
	"go_sku_server/pkg/common"
huangchengyi's avatar
1.0
f238237a
 
huangchengyi committed 4 years ago
8
	"go_sku_server/pkg/config"
wang's avatar
新增sku进度-spu新增
76ecfcc4
 
wang committed 4 years ago
9
	"net/http"
huangchengyi's avatar
1.0
59acfdf8
 
huangchengyi committed 4 years ago
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
)

//放 通用中间件

//检查服务是否可用
func Check_Middleware() gin_.Middleware {
	return func(next gin_.Endpoint) gin_.Endpoint {
		return func(context *gin.Context, request interface{}) (response interface{}, err error) {
			return next(context, request)
		}
	}
}

func Cors_Middleware() gin_.Middleware {
	return func(next gin_.Endpoint) gin_.Endpoint {
		return func(c *gin.Context, request interface{}) (response interface{}, err error) {
			method := c.Request.Method
			corsDomains := config.Get("web.cors.domain").Strings(",")
			for _, domain := range corsDomains {
				c.Header("Access-Control-Allow-Origin", domain)
			}
			c.Header("Access-Control-Allow-Origin", "https://bom.ichunt.com")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
			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")

			//放行所有OPTIONS方法
			if method == "OPTIONS" {
				c.AbortWithStatus(http.StatusNoContent)
			}
			// 处理请求
			return next(c, request)
		}
	}
}
wang's avatar
新增sku进度-spu新增
76ecfcc4
 
wang committed 4 years ago
46 47 48 49 50 51 52 53 54 55

/**
@author wangsong
捕获 系统内部 panic 异常,并加入日志
 */
func Error_Middleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		defer func() {
			if err:=recover(); err!=nil{
				errMsg:=fmt.Sprintf("%s",err)
wang's avatar
解决冲突后提交
691491e6
 
wang committed 4 years ago
56
				common.NResponse(errMsg).SetLogHandel(saveLogHandle).OutPut(ctx)
wang's avatar
新增sku进度-spu新增
76ecfcc4
 
wang committed 4 years ago
57 58 59 60 61
			}
		}()
		ctx.Next()
	}
}