允许跨域

parent d91ee4fe
Showing with 14 additions and 13 deletions
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"runtime" "runtime"
"search_server/pkg/logger" "search_server/pkg/logger"
"strconv" "strconv"
"strings"
) )
type Response struct { type Response struct {
...@@ -23,9 +24,9 @@ type BomResponse struct { ...@@ -23,9 +24,9 @@ type BomResponse struct {
} }
type RecommendResponse struct { type RecommendResponse struct {
Flag int `json:"flag"` Flag int `json:"flag"`
Total int `json:"total"` Total int `json:"total"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
func SuccessResponse(errCode int, errMsg string, data interface{}) Response { func SuccessResponse(errCode int, errMsg string, data interface{}) Response {
...@@ -45,7 +46,7 @@ func ErrorResponse(errCode int, errMsg string) Response { ...@@ -45,7 +46,7 @@ func ErrorResponse(errCode int, errMsg string) Response {
} }
//统一输出,里面还要去处理jsonp //统一输出,里面还要去处理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 { if data == nil {
data = []string{} data = []string{}
} }
...@@ -55,15 +56,15 @@ func Output(ctx *gin.Context,errCode int, errMsg string, data interface{}) { ...@@ -55,15 +56,15 @@ func Output(ctx *gin.Context,errCode int, errMsg string, data interface{}) {
Data: data, Data: data,
} }
if errCode >= 100 { if errCode >= 100 {
SearchApiLogger(ctx,errCode, errMsg) SearchApiLogger(ctx, errCode, errMsg)
} }
if ctx.DefaultQuery("callback", "") != "" { if ctx.DefaultQuery("callback", "") != "" {
ctx.JSONP(200, response) ctx.JSONP(200, response)
} else { } else {
//referer := ctx.Request.Referer() referer := ctx.Request.Referer()
//referer = strings.TrimRight(referer, "/") referer = strings.TrimRight(referer, "/")
//ctx.Header("Access-Control-Allow-Origin", referer) ctx.Header("Access-Control-Allow-Origin", referer)
//ctx.Header("Access-Control-Allow-Credentials", "true") ctx.Header("Access-Control-Allow-Credentials", "true")
//允许跨站访问的站点域名 //允许跨站访问的站点域名
//跨域请求头设置 //跨域请求头设置
ctx.JSON(200, response) ctx.JSON(200, response)
...@@ -71,7 +72,7 @@ func Output(ctx *gin.Context,errCode int, errMsg string, data interface{}) { ...@@ -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 { if data == nil {
data = []string{} data = []string{}
if errCode == 0 { if errCode == 0 {
...@@ -87,12 +88,12 @@ func ReturnData(ctx *gin.Context,errCode int, errMsg string, data interface{}) { ...@@ -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) pc, file, line, _ := runtime.Caller(2)
f := runtime.FuncForPC(pc) f := runtime.FuncForPC(pc)
errMsg := "提示信息:" + msg + ",请求url:" + ctx.Request.URL.String() errMsg := "提示信息:" + msg + ",请求url:" + ctx.Request.URL.String()
lineNo := strconv.Itoa(line) 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) 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