add back skip logger logic

This commit is contained in:
iacore
2024-08-28 14:24:38 +00:00
parent 28ed201d49
commit a3b34323bb
10 changed files with 50 additions and 47 deletions
-1
View File
@@ -72,7 +72,6 @@ type ServerPerformance struct {
Path string
Status int
Error error
SkipLogging bool
}
type APIPerformance struct {
+2 -2
View File
@@ -12,7 +12,7 @@ import (
"time"
)
var GlobalServerConfig ServerConfig
var GlobalConfig ServerConfig
type ServerConfig struct {
// Required
@@ -135,7 +135,7 @@ func (s *ServerConfig) setVersion() {
}
func GetRandomDefaultToken() string {
defaultToken := GlobalServerConfig.Token[rand.Intn(len(GlobalServerConfig.Token))]
defaultToken := GlobalConfig.Token[rand.Intn(len(GlobalConfig.Token))]
return defaultToken
}
+1 -1
View File
@@ -35,7 +35,7 @@ func StartProxyChecker(r context.Context) {
return
default:
checkProxies(r)
if t := GlobalServerConfig.ProxyCheckInterval; t > 0 {
if t := GlobalConfig.ProxyCheckInterval; t > 0 {
time.Sleep(t)
} else {
log.Print("Proxy check interval set to 0, disabling auto-check from now on.")
+2 -2
View File
@@ -232,10 +232,10 @@ func GetRelatedArtworks(r *http.Request, id string) ([]ArtworkBrief, error) {
}
func GetArtworkByID(r *http.Request, id string, full bool) (*Illust, error) {
URL := GetArtworkInformationURL(id)
urlArtInfo := GetArtworkInformationURL(id)
token := session.GetPixivToken(r)
response, err := API_GET_UnwrapJson(r.Context(), URL, token)
response, err := API_GET_UnwrapJson(r.Context(), urlArtInfo, token)
if err != nil {
return nil, err
}
+2 -2
View File
@@ -40,8 +40,8 @@ func _API_GET(context context.Context, url string, token string) (SimpleHTTPResp
return res, nil, err
}
req.Header.Add("User-Agent", config.GlobalServerConfig.UserAgent)
req.Header.Add("Accept-Language", config.GlobalServerConfig.AcceptLanguage)
req.Header.Add("User-Agent", config.GlobalConfig.UserAgent)
req.Header.Add("Accept-Language", config.GlobalConfig.AcceptLanguage)
if token == "" {
req.AddCookie(&http.Cookie{
+2 -2
View File
@@ -62,8 +62,8 @@ func (lim *IPRateLimiter) Allow(ip string) bool {
var limiter *IPRateLimiter
func InitializeRateLimiter() {
r := float64(config.GlobalServerConfig.RequestLimit) / 30.0
if config.GlobalServerConfig.RequestLimit < 1 {
r := float64(config.GlobalConfig.RequestLimit) / 30.0
if config.GlobalConfig.RequestLimit < 1 {
r = math.Inf(1)
}
limiter = NewIPRateLimiter(rate.Limit(r), 3)
+28 -24
View File
@@ -6,6 +6,7 @@ import (
"time"
"codeberg.org/vnpower/pixivfe/v2/audit"
"codeberg.org/vnpower/pixivfe/v2/config"
"codeberg.org/vnpower/pixivfe/v2/handlers/user_context"
)
@@ -25,34 +26,37 @@ func CanRequestSkipLogger(r *http.Request) bool {
return strings.HasPrefix(path, "/img/") ||
strings.HasPrefix(path, "/css/") ||
strings.HasPrefix(path, "/js/") ||
strings.HasPrefix(path, "/proxy/s.pximg.net/") ||
strings.HasPrefix(path, "/proxy/i.pximg.net/")
(config.GlobalConfig.InDevelopment &&
(strings.HasPrefix(path, "/proxy/s.pximg.net/") || strings.HasPrefix(path, "/proxy/i.pximg.net/")))
}
func LogRequest(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
return func(w_ http.ResponseWriter, r *http.Request) {
w := &ResponseWriterInterceptStatus{
statusCode: 0,
ResponseWriter: w_,
if CanRequestSkipLogger(r) {
f(w_, r)
} else {
w := &ResponseWriterInterceptStatus{
statusCode: 0,
ResponseWriter: w_,
}
// set user context
r = r.WithContext(user_context.WithContext(r.Context()))
start_time := time.Now()
f(w, r)
end_time := time.Now()
audit.LogServerRoundTrip(r.Context(), audit.ServerPerformance{
StartTime: start_time,
EndTime: end_time,
RemoteAddr: r.RemoteAddr,
Method: r.Method,
Path: r.URL.Path,
Status: w.statusCode,
Error: GetUserContext(r).Err,
})
}
// set user context
r = r.WithContext(user_context.WithContext(r.Context()))
start_time := time.Now()
f(w, r)
end_time := time.Now()
audit.LogServerRoundTrip(r.Context(), audit.ServerPerformance{
StartTime: start_time,
EndTime: end_time,
RemoteAddr: r.RemoteAddr,
Method: r.Method,
Path: r.URL.Path,
Status: w.statusCode,
Error: GetUserContext(r).Err,
SkipLogging: CanRequestSkipLogger(r),
})
}
}
+8 -8
View File
@@ -18,9 +18,9 @@ import (
)
func main() {
config.GlobalServerConfig.LoadConfig()
audit.Init(config.GlobalServerConfig.InDevelopment)
template.Init(config.GlobalServerConfig.InDevelopment)
config.GlobalConfig.LoadConfig()
audit.Init(config.GlobalConfig.InDevelopment)
template.Init(config.GlobalConfig.InDevelopment)
// Initialize and start the proxy checker
ctx_timeout, cancel := context.WithTimeout(context.Background(), config.ProxyCheckerTimeout)
@@ -38,7 +38,7 @@ func main() {
main_handler = handlers.LogRequest(main_handler)
// run sass when in development mode
if config.GlobalServerConfig.InDevelopment {
if config.GlobalConfig.InDevelopment {
go func() {
cmd := exec.Command("sass", "--watch", "assets/css")
cmd.Stdout = os.Stderr // Sass quirk
@@ -54,15 +54,15 @@ func main() {
// Listen
var l net.Listener
if config.GlobalServerConfig.UnixSocket != "" {
ln, err := net.Listen("unix", config.GlobalServerConfig.UnixSocket)
if config.GlobalConfig.UnixSocket != "" {
ln, err := net.Listen("unix", config.GlobalConfig.UnixSocket)
if err != nil {
panic(err)
}
l = ln
log.Printf("Listening on domain socket %v\n", config.GlobalServerConfig.UnixSocket)
log.Printf("Listening on domain socket %v\n", config.GlobalConfig.UnixSocket)
} else {
addr := config.GlobalServerConfig.Host + ":" + config.GlobalServerConfig.Port
addr := config.GlobalConfig.Host + ":" + config.GlobalConfig.Port
ln, err := net.Listen("tcp", addr)
if err != nil {
log.Panicf("failed to listen: %v", err)
+4 -4
View File
@@ -7,9 +7,9 @@ import (
func AboutPage(w http.ResponseWriter, r *http.Request) error {
return Render(w, r, Data_about{
Time: config.GlobalServerConfig.StartingTime,
Version: config.GlobalServerConfig.Version,
ImageProxy: config.GlobalServerConfig.ProxyServer.String(),
AcceptLanguage: config.GlobalServerConfig.AcceptLanguage,
Time: config.GlobalConfig.StartingTime,
Version: config.GlobalConfig.Version,
ImageProxy: config.GlobalConfig.ProxyServer.String(),
AcceptLanguage: config.GlobalConfig.AcceptLanguage,
})
}
+1 -1
View File
@@ -25,7 +25,7 @@ func GetImageProxy(r *http.Request) url.URL {
return *proxyUrl
}
}
return config.GlobalServerConfig.ProxyServer
return config.GlobalConfig.ProxyServer
}
func ProxyImageUrl(r *http.Request, s string) string {