Personal Git forge.

Code git clone ssh://git.poy.ooo:6969/~poyo/farga ZIP
app/middleware/messages.go / 1KiB #b835a695
Raw
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))	})}