From b36c58dad5fd45740edf51ea50fbbfc61ed39692 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 5 Oct 2023 00:56:14 +0200 Subject: [PATCH] refactor: IPNS_RECORD_GATEWAY_URL Ensures IPNS_RECORD_GATEWAY_URL is prefered, then we fallback to PROXY_GATEWAY_URL, and finally KUBO_RPC_URL --- docs/environment-variables.md | 6 +++--- handlers.go | 8 ++++---- main.go | 13 ++++++++++--- routing.go | 12 ++++++------ 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 2b0014d..da7b5eb 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -59,15 +59,15 @@ in the near feature. Single URL or a comma separated list of Gateway endpoints that support `?format=block|car|ipns-record` responses. Either this variable or `STRN_ORCHESTRATOR_URL` must be set. -If this gateway does not support `application/vnd.ipfs.ipns-record`, you can use `IPNS_RECORD_GATEWAY` +If this gateway does not support `application/vnd.ipfs.ipns-record`, you can use `IPNS_RECORD_GATEWAY_URL` to override the gateway address from which to retrieve IPNS Records from. -### `IPNS_RECORD_GATEWAY` +### `IPNS_RECORD_GATEWAY_URL` Single URL or a comma separated list of Gateway endpoints that support requests for `application/vnd.ipfs.ipns-record`. This is used for IPNS Record routing. -`IPNS_RECORD_GATEWAY` also supports [Routing V1 HTTP API](https://specs.ipfs.tech/routing/http-routing-v1/) +`IPNS_RECORD_GATEWAY_URL` also supports [Routing V1 HTTP API](https://specs.ipfs.tech/routing/http-routing-v1/) for IPNS Record routing ([IPIP-379](https://specs.ipfs.tech/ipips/ipip-0379/)). To use it, the provided URL must end with `/routing/v1`. If not set, the IPNS records will be fetched from `KUBO_RPC_URL`. diff --git a/handlers.go b/handlers.go index a09a3ec..d64a8dd 100644 --- a/handlers.go +++ b/handlers.go @@ -70,15 +70,15 @@ func withRequestLogger(next http.Handler) http.Handler { }) } -func makeGatewayHandler(bs bstore.Blockstore, kuboRPC, gatewayURLs []string, port int, blockCacheSize int, cdns *cachedDNS, useGraphBackend bool) (*http.Server, error) { +func makeGatewayHandler(bs bstore.Blockstore, kuboRPC, ipnsRecordGateways []string, port int, blockCacheSize int, cdns *cachedDNS, useGraphBackend bool) (*http.Server, error) { // Sets up the routing system, which will proxy the IPNS routing requests to the given gateway or kubo RPC. var routing routing.ValueStore - if len(gatewayURLs) != 0 { - routing = newProxyRouting(gatewayURLs, cdns) + if len(ipnsRecordGateways) != 0 { + routing = newProxyRouting(ipnsRecordGateways, cdns) } else if len(kuboRPC) != 0 { routing = newRPCProxyRouting(kuboRPC, cdns) } else { - return nil, errors.New("kubo rpc or gateway urls must be provided in order to delegate routing") + return nil, errors.New("either KUBO_RPC_URL, IPNS_RECORD_GATEWAY_URL or PROXY_GATEWAY_URL with support for application/vnd.ipfs.ipns-record must be provided in order to delegate IPNS routing") } // Sets up a cache to store blocks in diff --git a/main.go b/main.go index 4292199..ffd27db 100644 --- a/main.go +++ b/main.go @@ -32,7 +32,7 @@ func main() { const ( EnvKuboRPC = "KUBO_RPC_URL" - EnvIPNSRecordGateway = "IPNS_RECORD_GATEWAY" + EnvIPNSRecordGateway = "IPNS_RECORD_GATEWAY_URL" EnvBlockCacheSize = "BLOCK_CACHE_SIZE" EnvGraphBackend = "GRAPH_BACKEND" RequestIDHeader = "X-Bfid" @@ -108,8 +108,15 @@ See documentation at: https://github.com/ipfs/bifrost-gateway/#readme`, log.Fatalf("Unable to start. bifrost-gateway requires either PROXY_GATEWAY_URL or STRN_ORCHESTRATOR_URL to be set.\n\nRead docs at https://github.com/ipfs/bifrost-gateway/blob/main/docs/environment-variables.md\n\n") } - ipnsProxyGateway := getEnvs(EnvIPNSRecordGateway, "") - gatewaySrv, err := makeGatewayHandler(bs, kuboRPC, ipnsProxyGateway, gatewayPort, blockCacheSize, cdns, useGraphBackend) + // Prefer IPNS_RECORD_GATEWAY_URL when an explicit URL for IPNS routing is set + ipnsRecordGateway := getEnvs(EnvIPNSRecordGateway, "") + if len(ipnsRecordGateway) == 0 { + // Fallback to PROXY_GATEWAY_URL, assuming it is modern + // enough to support application/vnd.ipfs.ipns-record responses + ipnsRecordGateway = proxyGateway + } + + gatewaySrv, err := makeGatewayHandler(bs, kuboRPC, ipnsRecordGateway, gatewayPort, blockCacheSize, cdns, useGraphBackend) if err != nil { return err } diff --git a/routing.go b/routing.go index 9b1b216..17d9f88 100644 --- a/routing.go +++ b/routing.go @@ -156,17 +156,17 @@ func (ps *rpcProxyRouting) getRandomKuboURL() string { } type proxyRouting struct { - gatewayURLs []string - httpClient *http.Client - rand *rand.Rand + ipnsRecordGateways []string + httpClient *http.Client + rand *rand.Rand } -func newProxyRouting(gatewayURLs []string, cdns *cachedDNS) routing.ValueStore { +func newProxyRouting(ipnsRecordGateways []string, cdns *cachedDNS) routing.ValueStore { s := rand.NewSource(time.Now().Unix()) rand := rand.New(s) return &proxyRouting{ - gatewayURLs: gatewayURLs, + ipnsRecordGateways: ipnsRecordGateways, httpClient: &http.Client{ Transport: otelhttp.NewTransport(&customTransport{ // RoundTripper with increased defaults than http.Transport such that retrieving @@ -276,5 +276,5 @@ func (ps *proxyRouting) fetch(ctx context.Context, name ipns.Name) ([]byte, erro } func (ps *proxyRouting) getRandomGatewayURL() string { - return strings.TrimSuffix(ps.gatewayURLs[ps.rand.Intn(len(ps.gatewayURLs))], "/") + return strings.TrimSuffix(ps.ipnsRecordGateways[ps.rand.Intn(len(ps.ipnsRecordGateways))], "/") }