refactor(api): 禁用访问日志记录功能
- 移除日志记录相关的依赖和导入 - 注释掉访问日志记录的实现代码 - 保留响应头设置和OPTIONS请求的处理逻辑 - 保留状态码和字节数统计但不作日志输出 - 优化代码格式和缩进,提高可读性
This commit is contained in:
parent
b442209bfa
commit
2f2cef905f
|
|
@ -1,52 +1,41 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
"server/internal/logging"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type statusWriter struct{
|
||||
http.ResponseWriter
|
||||
status int
|
||||
bytes int
|
||||
type statusWriter struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
bytes int
|
||||
}
|
||||
|
||||
func (w *statusWriter) WriteHeader(code int){
|
||||
w.status = code
|
||||
w.ResponseWriter.WriteHeader(code)
|
||||
func (w *statusWriter) WriteHeader(code int) {
|
||||
w.status = code
|
||||
w.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func (w *statusWriter) Write(b []byte)(int, error){
|
||||
n, err := w.ResponseWriter.Write(b)
|
||||
w.bytes += n
|
||||
return n, err
|
||||
func (w *statusWriter) Write(b []byte) (int, error) {
|
||||
n, err := w.ResponseWriter.Write(b)
|
||||
w.bytes += n
|
||||
return n, err
|
||||
}
|
||||
|
||||
func withAccess(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
|
||||
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-Headers", "Content-Type, Authorization")
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
start := time.Now()
|
||||
sw := &statusWriter{ResponseWriter: w, status: 200}
|
||||
h.ServeHTTP(sw, r)
|
||||
dur := time.Since(start)
|
||||
m := MetaFrom(r)
|
||||
logging.JSON("INFO", map[string]interface{}{
|
||||
"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(),
|
||||
})
|
||||
})
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
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-Headers", "Content-Type, Authorization")
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
start := time.Now()
|
||||
sw := &statusWriter{ResponseWriter: w, status: 200}
|
||||
h.ServeHTTP(sw, r)
|
||||
// Access log disabled
|
||||
_ = time.Since(start)
|
||||
_ = MetaFrom(r)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue