feat: translation middleware

This commit is contained in:
2026-03-02 22:37:11 +01:00
parent dd5e32cc4d
commit 9a1f7e0d99
35 changed files with 1467 additions and 83 deletions
+34
View File
@@ -0,0 +1,34 @@
package templer
import (
"context"
"log/slog"
"net/http"
"strings"
)
type ctxKey int
const (
pathKey ctxKey = iota
)
func Middleware(next http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, pathKey, r.URL.Path)
next.ServeHTTP(w, r.WithContext(ctx))
}
}
// Path returns the current path without a leading slash
func Path(ctx context.Context) string {
path, ok := ctx.Value(pathKey).(string)
if !ok {
slog.Error("could not extract request path from context")
return ""
}
return strings.TrimPrefix(path, "/")
}