Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
search_server
This project
Loading...
Sign in
Toggle navigation
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
Commit
c4afff33
authored
Feb 02, 2023
by
SUDPTDUBLXEROFX\Administrator
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
允许跨域
parent
d91ee4fe
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
13 deletions
pkg/common/response.go
pkg/common/response.go
View file @
c4afff33
...
@@ -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"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment