Skip to content

Commit

Permalink
tutorial: implement hostname resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
robertkowalski committed Dec 21, 2017
1 parent b429ff5 commit 18bde1c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,28 @@ node worker.js --env=development --wtype=wrk-dns-service-api --apiPort 1337

## Grenache API

### action: 'getHelloWorld'
### action: 'getHostname'

- `args`: <Array>
- `0`: <Object>
- `name`: <String> Name to greet
- `ip`: <String> Ip to resolve

**Response:**

- <String> The Greeting
- <Array>
- `0`: <String> Ip that was resolved
- `1`: <Array> Hostnames

**Example Payload:**

```js
args: [ { name: 'Paolo' } ]
args: [ { ip: '8.8.8.8' } ]
```

**Example Response:**

```js
'Hello Paolo'
[ '8.8.8.8', [ 'google-public-dns-a.google.com' ] ]
```

Example: [example.js](example.js)
4 changes: 2 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const peer = new Peer(link, {})
peer.init()

const query = {
action: 'getHelloWorld',
args: [ { name: 'Paolo' } ]
action: 'getHostname',
args: [ { ip: '8.8.8.8' } ]
}

peer.request('dns:service', query, { timeout: 10000 }, (err, data) => {
Expand Down
12 changes: 12 additions & 0 deletions workers/loc.api/service.dns.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict'

const dns = require('dns')

const { Api } = require('bfx-wrk-api')

class dnsService extends Api {
Expand All @@ -8,6 +10,16 @@ class dnsService extends Api {
return space
}

getHostname (space, args, cb) {
const ip = args.ip
if (!ip) return cb(new Error('ERR_ARGS_NO_IP'))

dns.reverse(ip, (err, res) => {
if (err) return cb(err)

cb(null, [ip, res])
})
}
}

module.exports = dnsService

0 comments on commit 18bde1c

Please sign in to comment.