Improve HTTP server.
Linting and tests / Linting (push) Successful in 19s
Linting and tests / Tests (push) Successful in 17s

This commit is contained in:
2026-06-10 10:39:12 +05:00
parent 0db14666d0
commit 7cc06a9ef1
4 changed files with 21 additions and 15 deletions
+2
View File
@@ -1,3 +1,5 @@
module go.dev.pztrn.name/vikunja-notifier module go.dev.pztrn.name/vikunja-notifier
go 1.26.3 go 1.26.3
require github.com/felixge/httpsnoop v1.0.4
+2
View File
@@ -0,0 +1,2 @@
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
@@ -3,22 +3,24 @@ package httpserver
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"time"
"github.com/felixge/httpsnoop"
) )
func (h *httpServer) requestLoggingMiddleware(fn http.HandlerFunc) http.HandlerFunc { func (h *httpServer) requestLoggingMiddleware(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, req *http.Request) {
startTime := time.Now() metrics := httpsnoop.CaptureMetrics(fn, w, req)
fn(w, r)
h.logger.Info( h.logger.Info(
"HTTP request.", "HTTP request.",
"remote_addr", r.RemoteAddr, "remote_addr", req.RemoteAddr,
"user_agent", r.UserAgent(), "user_agent", req.UserAgent(),
"host", r.Host, "host", req.Host,
"path", fmt.Sprintf("%s %s", r.Method, r.RequestURI), "path", fmt.Sprintf("%s %s", req.Method, req.RequestURI),
"duration", time.Since(startTime), "duration", metrics.Duration,
"req_size", req.ContentLength,
"resp_size", metrics.Written,
"http_code", metrics.Code,
) )
} }
} }
+1 -1
View File
@@ -49,12 +49,12 @@ func (h *httpServer) configureHTTPServer() error {
} }
func (h *httpServer) RegisterHandler(method, path string, handler http.HandlerFunc) { func (h *httpServer) RegisterHandler(method, path string, handler http.HandlerFunc) {
h.httpMux.HandleFunc(fmt.Sprintf("%s %s", method, path), func(w http.ResponseWriter, r *http.Request) {
//nolint:modernize //nolint:modernize
for i := len(h.middlewares) - 1; i >= 0; i-- { for i := len(h.middlewares) - 1; i >= 0; i-- {
handler = h.middlewares[i](handler) handler = h.middlewares[i](handler)
} }
h.httpMux.HandleFunc(fmt.Sprintf("%s %s", method, path), func(w http.ResponseWriter, r *http.Request) {
handler(w, r) handler(w, r)
}) })
} }