Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
何 广一
intern_project_backend
Commits
69760c9b
Commit
69760c9b
authored
Jun 24, 2025
by
IronHammer Std
Browse files
优化了日志输出
parent
918d1658
Changes
2
Hide whitespace changes
Inline
Side-by-side
Server.go
View file @
69760c9b
...
@@ -21,8 +21,11 @@ type CommentList struct {
...
@@ -21,8 +21,11 @@ type CommentList struct {
Comments
[]
db
.
Comment
`json:"comments"`
// 评论列表
Comments
[]
db
.
Comment
`json:"comments"`
// 评论列表
}
}
// 发送统一格式的响应
// 发送统一格式的响应
func
sendResponse
(
w
http
.
ResponseWriter
,
code
int
,
msg
string
,
data
interface
{})
{
func
sendResponse
(
w
http
.
ResponseWriter
,
code
int
,
msg
string
,
data
interface
{})
{
log
.
Printf
(
"Sending response: Code=%d Msg=%s"
,
code
,
msg
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
"*"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
"*"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Methods"
,
"GET, POST, OPTIONS"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Methods"
,
"GET, POST, OPTIONS"
)
...
@@ -41,15 +44,24 @@ func respondOptions(w http.ResponseWriter) {
...
@@ -41,15 +44,24 @@ func respondOptions(w http.ResponseWriter) {
w
.
Header
()
.
Set
(
"Access-Control-Allow-Methods"
,
"GET, POST, OPTIONS"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Methods"
,
"GET, POST, OPTIONS"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Headers"
,
"Content-Type"
)
w
.
Header
()
.
Set
(
"Access-Control-Allow-Headers"
,
"Content-Type"
)
w
.
WriteHeader
(
http
.
StatusOK
)
w
.
WriteHeader
(
http
.
StatusOK
)
}
// 获取评论处理函数
log
.
Printf
(
"Response : CORS headers set"
)
func
getCommentHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
}
log
.
Printf
(
"Received request: %s %s"
,
r
.
Method
,
r
.
URL
.
Path
)
func
headerProcess
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
bool
{
log
.
Printf
(
"Received request: Method=%s URL=%s"
,
r
.
Method
,
r
.
URL
.
Path
)
// 提前处理 OPTIONS 请求
// 提前处理 OPTIONS 请求
if
r
.
Method
==
http
.
MethodOptions
{
if
r
.
Method
==
http
.
MethodOptions
{
respondOptions
(
w
)
respondOptions
(
w
)
return
true
}
return
false
}
// 获取评论处理函数
func
getCommentHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
if
headerProcess
(
w
,
r
)
{
return
return
}
}
...
@@ -107,13 +119,8 @@ func getCommentHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -107,13 +119,8 @@ func getCommentHandler(w http.ResponseWriter, r *http.Request) {
// 添加评论处理函数
// 添加评论处理函数
func
addCommentHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
addCommentHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// 输出Request信息到控制台:
headerProcess
(
w
,
r
)
log
.
Printf
(
"Received request: %s %s"
,
r
.
Method
,
r
.
URL
.
Path
)
// 提前处理 OPTIONS 请求
if
r
.
Method
==
http
.
MethodOptions
{
respondOptions
(
w
)
return
}
// 只处理POST请求
// 只处理POST请求
...
@@ -155,12 +162,7 @@ func addCommentHandler(w http.ResponseWriter, r *http.Request) {
...
@@ -155,12 +162,7 @@ func addCommentHandler(w http.ResponseWriter, r *http.Request) {
// 删除评论处理函数
// 删除评论处理函数
func
deleteCommentHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
deleteCommentHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
log
.
Printf
(
"Received request: %s %s"
,
r
.
Method
,
r
.
URL
.
Path
)
headerProcess
(
w
,
r
)
// 提前处理 OPTIONS 请求
if
r
.
Method
==
http
.
MethodOptions
{
respondOptions
(
w
)
return
}
// 只处理POST请求
// 只处理POST请求
if
r
.
Method
!=
http
.
MethodPost
{
if
r
.
Method
!=
http
.
MethodPost
{
...
...
readme.md
0 → 100644
View file @
69760c9b
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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