all repos

mugit @ b59873080d2d6d0520c2240c9f3381973666a848

🐮 git server that your cow will love

mugit/internal/handlers/util.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
feat: improve http logger, 3 months ago
1
package handlers
2
3
import (
4
	"fmt"
5
	"log/slog"
6
	"net/http"
7
	"time"
8
)
9
10
func (h *handlers) templ(w http.ResponseWriter, name string, data any) {
11
	if err := h.t.ExecuteTemplate(w, name, data); err != nil {
12
		w.WriteHeader(http.StatusInternalServerError)
13
		slog.Error("template", "name", name, "err", err)
14
	}
15
}
16
17
func (h *handlers) write404(w http.ResponseWriter, err error) {
18
	slog.Info("404", "err", err)
19
	w.WriteHeader(http.StatusNotFound)
20
	h.templ(w, "404", nil)
21
}
22
23
func (h *handlers) write500(w http.ResponseWriter, err error) {
24
	slog.Info("500", "err", err)
25
	w.WriteHeader(http.StatusInternalServerError)
26
	h.templ(w, "500", nil)
27
}
28
29
func (h *handlers) recoverMiddleware(next http.Handler) http.Handler {
30
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
31
		defer func() {
32
			if err := recover(); err != nil {
33
				h.write500(w, fmt.Errorf("panic: %v", err))
34
			}
35
		}()
36
		next.ServeHTTP(w, r)
37
	})
38
}
39
40
func (h *handlers) loggingMiddleware(next http.Handler) http.Handler {
41
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
42
		start := time.Now()
43
		wrapped := wrapResponseWriter(w)
44
		next.ServeHTTP(wrapped, r)
45
46
		slog.Info("http request",
47
			"method", r.Method,
48
			"status", wrapped.status,
49
			"path", r.URL.Path,
50
			"latency", time.Since(start).String(),
51
			"ua", r.UserAgent(),
52
		)
53
	})
54
}
55
56
type responseWriter struct {
57
	http.ResponseWriter
58
	status      int
59
	wroteHeader bool
60
}
61
62
func wrapResponseWriter(w http.ResponseWriter) *responseWriter {
63
	return &responseWriter{ResponseWriter: w}
64
}
65
66
func (rw *responseWriter) Status() int {
67
	return rw.status
68
}
69
70
func (rw *responseWriter) WriteHeader(code int) {
71
	if rw.wroteHeader {
72
		return
73
	}
74
75
	rw.status = code
76
	rw.ResponseWriter.WriteHeader(code)
77
	rw.wroteHeader = true
78
}