forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

knotserver: redirect handle-path to did-path

this will allow handle based url when cloning from knot over https
usually appview will resolve this before redirecting to the knot

this implementation just redirects to resolved path instead of passing
the entire identity for performance. As we won't want to resolve same
identity twice when proxied by appview.

Signed-off-by: Seongmin Lee <git@boltless.me>

boltless.me 57d2cd55 37ef6248

verified
Changed files
+25
knotserver
+25
knotserver/router.go
···
"fmt"
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
"tangled.org/core/idresolver"
···
})
r.Route("/{did}", func(r chi.Router) {
r.Route("/{name}", func(r chi.Router) {
// routes for git operations
r.Get("/info/refs", h.InfoRefs)
···
}
return xrpc.Router()
}
func (h *Knot) configureOwner() error {
···
"fmt"
"log/slog"
"net/http"
+
"strings"
"github.com/go-chi/chi/v5"
"tangled.org/core/idresolver"
···
})
r.Route("/{did}", func(r chi.Router) {
+
r.Use(h.resolveDidRedirect)
r.Route("/{name}", func(r chi.Router) {
// routes for git operations
r.Get("/info/refs", h.InfoRefs)
···
}
return xrpc.Router()
+
}
+
+
func (h *Knot) resolveDidRedirect(next http.Handler) http.Handler {
+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+
didOrHandle := chi.URLParam(r, "did")
+
if strings.HasPrefix(didOrHandle, "did:") {
+
next.ServeHTTP(w, r)
+
return
+
}
+
+
trimmed := strings.TrimPrefix(didOrHandle, "@")
+
id, err := h.resolver.ResolveIdent(r.Context(), trimmed)
+
if err != nil {
+
// invalid did or handle
+
h.l.Error("failed to resolve did/handle", "handle", trimmed, "err", err)
+
http.Error(w, fmt.Sprintf("failed to resolve did/handle: %s", trimmed), http.StatusInternalServerError)
+
return
+
}
+
+
suffix := strings.TrimPrefix(r.URL.Path, "/"+didOrHandle)
+
newPath := fmt.Sprintf("/%s/%s?%s", id.DID.String(), suffix, r.URL.RawQuery)
+
http.Redirect(w, r, newPath, http.StatusTemporaryRedirect)
+
})
}
func (h *Knot) configureOwner() error {