Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
refactor: IPNS_RECORD_GATEWAY_URL
Browse files Browse the repository at this point in the history
Ensures IPNS_RECORD_GATEWAY_URL is prefered, then we fallback to
PROXY_GATEWAY_URL, and finally KUBO_RPC_URL
  • Loading branch information
lidel committed Oct 4, 2023
1 parent 523a02d commit b36c58d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
6 changes: 3 additions & 3 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
8 changes: 4 additions & 4 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Check warning on line 82 in handlers.go

View check run for this annotation

Codecov / codecov/patch

handlers.go#L73-L82

Added lines #L73 - L82 were not covered by tests

// Sets up a cache to store blocks in
Expand Down
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Check warning on line 117 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L112-L117

Added lines #L112 - L117 were not covered by tests

gatewaySrv, err := makeGatewayHandler(bs, kuboRPC, ipnsRecordGateway, gatewayPort, blockCacheSize, cdns, useGraphBackend)

Check warning on line 119 in main.go

View check run for this annotation

Codecov / codecov/patch

main.go#L119

Added line #L119 was not covered by tests
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))], "/")

Check warning on line 279 in routing.go

View check run for this annotation

Codecov / codecov/patch

routing.go#L278-L279

Added lines #L278 - L279 were not covered by tests
}

0 comments on commit b36c58d

Please sign in to comment.