From a3b34323bbdc0d22b2d35b4e8b44e95079979207 Mon Sep 17 00:00:00 2001 From: iacore Date: Wed, 28 Aug 2024 14:24:38 +0000 Subject: [PATCH] add back skip logger logic --- audit/tracing.go | 1 - config/config.go | 4 ++-- config/proxy_checker.go | 2 +- core/artwork.go | 4 ++-- core/requests.go | 4 ++-- handlers/limiter.go | 4 ++-- handlers/logger.go | 52 ++++++++++++++++++++++------------------- main.go | 16 ++++++------- routes/about.go | 8 +++---- session/aux.go | 2 +- 10 files changed, 50 insertions(+), 47 deletions(-) diff --git a/audit/tracing.go b/audit/tracing.go index f941343..81b3b84 100644 --- a/audit/tracing.go +++ b/audit/tracing.go @@ -72,7 +72,6 @@ type ServerPerformance struct { Path string Status int Error error - SkipLogging bool } type APIPerformance struct { diff --git a/config/config.go b/config/config.go index e316a0b..048a4d5 100644 --- a/config/config.go +++ b/config/config.go @@ -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 } diff --git a/config/proxy_checker.go b/config/proxy_checker.go index 453163a..d246730 100644 --- a/config/proxy_checker.go +++ b/config/proxy_checker.go @@ -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.") diff --git a/core/artwork.go b/core/artwork.go index 135826d..62c6502 100644 --- a/core/artwork.go +++ b/core/artwork.go @@ -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 } diff --git a/core/requests.go b/core/requests.go index 909512e..0437b62 100644 --- a/core/requests.go +++ b/core/requests.go @@ -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{ diff --git a/handlers/limiter.go b/handlers/limiter.go index ce0d851..bf84f55 100644 --- a/handlers/limiter.go +++ b/handlers/limiter.go @@ -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) diff --git a/handlers/logger.go b/handlers/logger.go index 29dd6cd..235d6ef 100644 --- a/handlers/logger.go +++ b/handlers/logger.go @@ -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), - }) } } diff --git a/main.go b/main.go index 98c74db..62b759e 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/routes/about.go b/routes/about.go index 75b8adc..d5672f8 100644 --- a/routes/about.go +++ b/routes/about.go @@ -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, }) } diff --git a/session/aux.go b/session/aux.go index f666d39..40e0a10 100644 --- a/session/aux.go +++ b/session/aux.go @@ -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 {