Skip to content

Commit

Permalink
feat: support IPFS_GATEWAY env
Browse files Browse the repository at this point in the history
ipfs/specs#280

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
lidel committed Oct 18, 2022
1 parent e631101 commit 95f9249
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,16 @@ in multiple locations, and stored as the new `GEOIP_ROOT` in `src/lookup.js`

## Testing in CLI

It is possible to run tests against a local gateway by passing `HTTP_GATEWAY`:

```console
$ IPFS_GATEWAY="http://127.0.0.1:8080" npm test
```

You can find an example of how to use this in [`example/lookup.js`](example/lookup.js), which you can use like this:

```bash
$ export IPFS_GATEWAY="http://127.0.0.1:8080"
$ node example/lookup.js 66.6.44.4
Result: {
"country_name": "USA",
Expand Down
7 changes: 3 additions & 4 deletions example/lookup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as geoip from '../src/index.js'
import { create } from 'ipfs-http-client'

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

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

(async function() {
try {
const result = await geoip.lookup(ipfs, process.argv[2])
const result = await geoip.lookup(ipfsGw, process.argv[2])
console.log('Result: ' + JSON.stringify(result, null, 2))
} catch (err) {
console.log('Error: ' + err)
}

try {
const result = await geoip.lookupPretty(ipfs, '/ip4/' + process.argv[2])
const result = await geoip.lookupPretty(ipfsGw, '/ip4/' + process.argv[2])
console.log('Pretty result: %s', result.formatted)
} catch (err) {
console.log('Error: ' + err)
Expand Down
2 changes: 1 addition & 1 deletion test/lookup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as geoip from '../src/index.js'
describe('lookup via HTTP Gateway supporting application/vnd.ipld.raw responses', function () {
this.timeout(100 * 1000)

const ipfsGW = 'https://ipfs.io'
const ipfsGW = process?.env?.IPFS_GATEWAY || 'https://ipfs.io'

it('fails on 127.0.0.1', async () => {
try {
Expand Down

0 comments on commit 95f9249

Please sign in to comment.