允许跨域

parent d91ee4fe
Showing with 14 additions and 13 deletions
......@@ -6,6 +6,7 @@ import (
"runtime"
"search_server/pkg/logger"
"strconv"
"strings"
)
type Response struct {
......@@ -23,9 +24,9 @@ type BomResponse struct {
}
type RecommendResponse struct {
Flag int `json:"flag"`
Total int `json:"total"`
Data interface{} `json:"data"`
Flag int `json:"flag"`
Total int `json:"total"`
Data interface{} `json:"data"`
}
func SuccessResponse(errCode int, errMsg string, data interface{}) Response {
......@@ -45,7 +46,7 @@ func ErrorResponse(errCode int, errMsg string) Response {
}
//统一输出,里面还要去处理jsonp
func Output(ctx *gin.Context,errCode int, errMsg string, data interface{}) {
func Output(ctx *gin.Context, errCode int, errMsg string, data interface{}) {
if data == nil {
data = []string{}
}
......@@ -55,15 +56,15 @@ func Output(ctx *gin.Context,errCode int, errMsg string, data interface{}) {
Data: data,
}
if errCode >= 100 {
SearchApiLogger(ctx,errCode, errMsg)
SearchApiLogger(ctx, errCode, errMsg)
}
if ctx.DefaultQuery("callback", "") != "" {
ctx.JSONP(200, response)
} else {
//referer := ctx.Request.Referer()
//referer = strings.TrimRight(referer, "/")
//ctx.Header("Access-Control-Allow-Origin", referer)
//ctx.Header("Access-Control-Allow-Credentials", "true")
referer := ctx.Request.Referer()
referer = strings.TrimRight(referer, "/")
ctx.Header("Access-Control-Allow-Origin", referer)
ctx.Header("Access-Control-Allow-Credentials", "true")
//允许跨站访问的站点域名
//跨域请求头设置
ctx.JSON(200, response)
......@@ -71,7 +72,7 @@ func Output(ctx *gin.Context,errCode int, errMsg string, data interface{}) {
}
//简单的返回数据方法
func ReturnData(ctx *gin.Context,errCode int, errMsg string, data interface{}) {
func ReturnData(ctx *gin.Context, errCode int, errMsg string, data interface{}) {
if data == nil {
data = []string{}
if errCode == 0 {
......@@ -87,12 +88,12 @@ func ReturnData(ctx *gin.Context,errCode int, errMsg string, data interface{}) {
}
//错误的搜索日志记录
func SearchApiLogger(ctx *gin.Context,code int, msg string) {
func SearchApiLogger(ctx *gin.Context, code int, msg string) {
pc, file, line, _ := runtime.Caller(2)
f := runtime.FuncForPC(pc)
errMsg := "提示信息:" + msg + ",请求url:" + ctx.Request.URL.String()
lineNo := strconv.Itoa(line)
searchLog := AnalyzeSearchError(ctx,code, ctx.ClientIP(), errMsg, file, lineNo, f.Name())
searchLog := AnalyzeSearchError(ctx, code, ctx.ClientIP(), errMsg, file, lineNo, f.Name())
searchLogByte, _ := json.Marshal(searchLog)
logger.Log(string(searchLogByte),"elk")
logger.Log(string(searchLogByte), "elk")
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment