Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950package middleware
import ( "net/http" "strings"
"github.com/go-chi/chi/v5" "tangled.org/core/appview/state/userutil")
func Normalize() middlewareFunc { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { pat := chi.URLParam(r, "*") pathParts := strings.SplitN(pat, "/", 2) if len(pathParts) == 0 { next.ServeHTTP(w, r) return }
firstPart := pathParts[0]
// if using a flattened DID (like you would in go modules), unflatten if userutil.IsFlattenedDid(firstPart) { unflattenedDid := userutil.UnflattenDid(firstPart) redirectPath := strings.Join(append([]string{unflattenedDid}, pathParts[1:]...), "/")
redirectURL := *r.URL redirectURL.Path = "/" + redirectPath
http.Redirect(w, r, redirectURL.String(), http.StatusFound) return }
// if using a handle with @, rewrite to work without @ if normalized := strings.TrimPrefix(firstPart, "@"); userutil.IsHandle(normalized) { redirectPath := strings.Join(append([]string{normalized}, pathParts[1:]...), "/")
redirectURL := *r.URL redirectURL.Path = "/" + redirectPath
http.Redirect(w, r, redirectURL.String(), http.StatusFound) return }
next.ServeHTTP(w, r) }) }}