Personal Git forge.
package middlewareimport ( "context" "encoding/json" "farga/lib" "net/http" "strings")func Messages(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { message := r.URL.Query().Get(lib.MessageKey) err := r.URL.Query().Get(lib.ErrorKey) ctx := r.Context() if message != "" { if m, ok := lib.Messages[message]; ok { ctx = context.WithValue(ctx, lib.MessageKey, m) } } if err != "" { if e, ok := lib.Errors[err]; ok { ctx = context.WithValue(ctx, lib.ErrorKey, e) } else if err, ok := strings.CutPrefix(err, lib.ValidationErrorKey); ok { decoded := make([]byte, lib.Base64Encoder.DecodedLen(len(err))) n, err := lib.Base64Encoder.Decode(decoded, []byte(err)) if err == nil { decoded = decoded[:n] var unmarshalled any if err := json.Unmarshal(decoded, &unmarshalled); err != nil { http.Error(w, "Invalid marshalled JSON inside Base64 errors.", http.StatusBadRequest) return } ctx = context.WithValue(ctx, lib.ErrorKey, unmarshalled) } } } next.ServeHTTP(w, r.WithContext(ctx)) })}