refactor(api): 禁用访问日志记录功能
- 移除日志记录相关的依赖和导入 - 注释掉访问日志记录的实现代码 - 保留响应头设置和OPTIONS请求的处理逻辑 - 保留状态码和字节数统计但不作日志输出 - 优化代码格式和缩进,提高可读性
This commit is contained in:
parent
b442209bfa
commit
2f2cef905f
|
|
@ -3,28 +3,27 @@ package api
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
"server/internal/logging"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type statusWriter struct{
|
type statusWriter struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
status int
|
status int
|
||||||
bytes int
|
bytes int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *statusWriter) WriteHeader(code int){
|
func (w *statusWriter) WriteHeader(code int) {
|
||||||
w.status = code
|
w.status = code
|
||||||
w.ResponseWriter.WriteHeader(code)
|
w.ResponseWriter.WriteHeader(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *statusWriter) Write(b []byte)(int, error){
|
func (w *statusWriter) Write(b []byte) (int, error) {
|
||||||
n, err := w.ResponseWriter.Write(b)
|
n, err := w.ResponseWriter.Write(b)
|
||||||
w.bytes += n
|
w.bytes += n
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func withAccess(h http.Handler) http.Handler {
|
func withAccess(h http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PATCH,DELETE,OPTIONS")
|
w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PATCH,DELETE,OPTIONS")
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||||
|
|
@ -35,18 +34,8 @@ func withAccess(h http.Handler) http.Handler {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
sw := &statusWriter{ResponseWriter: w, status: 200}
|
sw := &statusWriter{ResponseWriter: w, status: 200}
|
||||||
h.ServeHTTP(sw, r)
|
h.ServeHTTP(sw, r)
|
||||||
dur := time.Since(start)
|
// Access log disabled
|
||||||
m := MetaFrom(r)
|
_ = time.Since(start)
|
||||||
logging.JSON("INFO", map[string]interface{}{
|
_ = MetaFrom(r)
|
||||||
"kind": "access",
|
|
||||||
"trace_id": TraceIDFrom(r),
|
|
||||||
"method": m.Method,
|
|
||||||
"path": m.Path,
|
|
||||||
"query": m.Query,
|
|
||||||
"remote": m.Remote,
|
|
||||||
"status": sw.status,
|
|
||||||
"bytes": sw.bytes,
|
|
||||||
"duration_ms": dur.Milliseconds(),
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue