Skip to content

Commit

Permalink
fix(route): handle /api/v0/name/resolve/arg (#63)
Browse files Browse the repository at this point in the history
* legacy rpc: handle /api/v0/name/resolve/
* refactor: test arg in path alone
Co-authored-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
ns4plabs authored Jan 29, 2024
1 parent 5067f27 commit c5d0b21
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestRPCRedirectsToKubo(t *testing.T) {

tests := []rpcRedirectTest{
{"/api/v0/name/resolve?arg=some-arg", "http://example.com/api/v0/name/resolve?arg=some-arg", http.StatusTemporaryRedirect},
{"/api/v0/name/resolve/some-arg", "http://example.com/api/v0/name/resolve/some-arg", http.StatusTemporaryRedirect},
{"/api/v0/resolve?arg=some-arg", "http://example.com/api/v0/resolve?arg=some-arg", http.StatusTemporaryRedirect},
{"/api/v0/dag/resolve?arg=some-arg", "http://example.com/api/v0/dag/resolve?arg=some-arg", http.StatusTemporaryRedirect},
{"/api/v0/dns?arg=some-arg", "http://example.com/api/v0/dns?arg=some-arg", http.StatusTemporaryRedirect},
Expand Down
6 changes: 5 additions & 1 deletion handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,16 @@ func newKuboRPCHandler(endpoints []string) http.Handler {
redirectToKubo := func(w http.ResponseWriter, r *http.Request) {
// Naively choose one of the Kubo RPC clients.
endpoint := endpoints[rand.Intn(len(endpoints))]
url := endpoint + r.URL.Path + "?" + r.URL.RawQuery
url := endpoint + r.URL.Path
if r.URL.RawQuery != "" {
url += "?" + r.URL.RawQuery
}
goLog.Debugw("api request redirected to kubo", "url", r.URL, "redirect", url)
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}

mux.HandleFunc("/api/v0/name/resolve", redirectToKubo)
mux.HandleFunc("/api/v0/name/resolve/", redirectToKubo)
mux.HandleFunc("/api/v0/resolve", redirectToKubo)
mux.HandleFunc("/api/v0/dag/resolve", redirectToKubo)
mux.HandleFunc("/api/v0/dns", redirectToKubo)
Expand Down

0 comments on commit c5d0b21

Please sign in to comment.