Skip to content

Commit 95f9249

Browse files
committed
feat: support IPFS_GATEWAY env
ipfs/specs#280 License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent e631101 commit 95f9249

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,16 @@ in multiple locations, and stored as the new `GEOIP_ROOT` in `src/lookup.js`
159159

160160
## Testing in CLI
161161

162+
It is possible to run tests against a local gateway by passing `HTTP_GATEWAY`:
163+
164+
```console
165+
$ IPFS_GATEWAY="http://127.0.0.1:8080" npm test
166+
```
167+
162168
You can find an example of how to use this in [`example/lookup.js`](example/lookup.js), which you can use like this:
163169

164170
```bash
171+
$ export IPFS_GATEWAY="http://127.0.0.1:8080"
165172
$ node example/lookup.js 66.6.44.4
166173
Result: {
167174
"country_name": "USA",

example/lookup.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as geoip from '../src/index.js'
22
import { create } from 'ipfs-http-client'
33

4-
// This CLI tool requires Kubo RPC on 127.0.0.1:5001 to be running
5-
const ipfs = create(new URL('http://127.0.0.1:5001'))
4+
const ipfsGw = process?.env?.IPFS_GATEWAY || 'https://ipfs.io'
65

76
if (process.argv.length !== 3) {
87
console.log('usage: node lookup.js <ip4-adr>')
@@ -11,14 +10,14 @@ if (process.argv.length !== 3) {
1110

1211
(async function() {
1312
try {
14-
const result = await geoip.lookup(ipfs, process.argv[2])
13+
const result = await geoip.lookup(ipfsGw, process.argv[2])
1514
console.log('Result: ' + JSON.stringify(result, null, 2))
1615
} catch (err) {
1716
console.log('Error: ' + err)
1817
}
1918

2019
try {
21-
const result = await geoip.lookupPretty(ipfs, '/ip4/' + process.argv[2])
20+
const result = await geoip.lookupPretty(ipfsGw, '/ip4/' + process.argv[2])
2221
console.log('Pretty result: %s', result.formatted)
2322
} catch (err) {
2423
console.log('Error: ' + err)

test/lookup.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as geoip from '../src/index.js'
44
describe('lookup via HTTP Gateway supporting application/vnd.ipld.raw responses', function () {
55
this.timeout(100 * 1000)
66

7-
const ipfsGW = 'https://ipfs.io'
7+
const ipfsGW = process?.env?.IPFS_GATEWAY || 'https://ipfs.io'
88

99
it('fails on 127.0.0.1', async () => {
1010
try {

0 commit comments

Comments
 (0)