feat: Use Logx to log

This commit is contained in:
Roi Feng
2025-04-28 23:33:53 -04:00
parent 705230dd0d
commit e73a786311
7 changed files with 41 additions and 30 deletions

View File

@ -2,13 +2,14 @@ package server
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"server_torii/internal/action"
"server_torii/internal/check"
"server_torii/internal/config"
"server_torii/internal/dataType"
"server_torii/internal/utils"
"time"
)
@ -37,13 +38,13 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte("OK"))
if err != nil {
log.Printf("Error write response: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error writing response: %v", err), "CheckMain")
return
}
} else if bytes.Compare(decision.HTTPCode, []byte("403")) == 0 {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/403.html")
if err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -60,7 +61,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
w.WriteHeader(http.StatusForbidden)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err = tpl.Execute(w, data); err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error executing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -68,7 +69,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
} else if bytes.Compare(decision.HTTPCode, []byte("CAPTCHA")) == 0 {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/CAPTCHA.html")
if err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -76,7 +77,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusServiceUnavailable)
if err = tpl.Execute(w, nil); err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error executing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -84,7 +85,7 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
} else if bytes.Compare(decision.HTTPCode, []byte("429")) == 0 {
tpl, err := template.ParseFiles(cfg.ErrorPage + "/429.html")
if err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error parsing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
@ -100,14 +101,14 @@ func CheckMain(w http.ResponseWriter, userRequestData dataType.UserRequest, rule
w.WriteHeader(http.StatusTooManyRequests)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err = tpl.Execute(w, data); err != nil {
log.Printf("Error template: %v", err)
utils.LogError(userRequestData, fmt.Sprintf("Error executing template: %v", err), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}
} else {
//should never happen
log.Printf("Error access in wrong state: %v", decision)
utils.LogError(userRequestData, fmt.Sprintf("Error access in wrong state: %v", decision), "CheckMain")
http.Error(w, "500 - Internal Server Error", http.StatusInternalServerError)
return
}