Skip to content

Commit

Permalink
Merge pull request #14 from QXIP/refator-into-lib
Browse files Browse the repository at this point in the history
Refactor app into lib
  • Loading branch information
sergibondarenko authored Feb 14, 2018
2 parents eb1c7df + 1e33786 commit 1a49657
Show file tree
Hide file tree
Showing 26 changed files with 1,187 additions and 4,201 deletions.
7 changes: 1 addition & 6 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{
"presets": ["env"],
"env": {
"test": {
"plugins": ["istanbul"]
}
},
"presets": ["env"]
}
16 changes: 0 additions & 16 deletions .dockerignore

This file was deleted.

5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Gun data
# Gun
data.json

# Experiments
lab

# Logs
logs
*.log
Expand Down
2 changes: 1 addition & 1 deletion .node_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8.9.4
6.11.5
11 changes: 0 additions & 11 deletions .nycrc

This file was deleted.

23 changes: 0 additions & 23 deletions Dockerfile

This file was deleted.

21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

197 changes: 116 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,99 +1,134 @@
# gun-host
It is a HTTP/HTTPS host to facilitate usage of [Gun](http://gun.js.org)
It is a lib to run [Gun](http://gun.js.org) host in Node.js

1. [Run](#run)
2. [API](#api)
3. [Development](#development)
4. [Docker](#docker)
1. [Install](#install)
2. [Usage](#usage)

# Run
All the following instructions are for Linux.
## Install Node.js
Use [NVM](https://github.com/creationix/nvm) to manage Node.js versions. Remove existing Node.js if it was not installed via NVM. Install it via NVM.
1. Go into app directory
# Install
```
cd gun-host
npm install --save https://github.com/QXIP/gun-host
```
2. Check the required version
```
cat .node_version
8.9.4
```
3. Install
```
nvm install 8.9.4
```
4. Switch to the installed version
```
nvm use 8.9.4
```
## Config host
To config the host, edit properties in `server/config.js`.

## Run host
```
npm start
```
# Usage
The example illustrates creating cluster node `sentinl`, adding a child node `hosts`, then adding `config.host` node as a child of `hosts`.

# API
## Add
Create `dinos.456` node.
```
curl -H "Content-Type: application/json" -d '{"path": "dinos.456", "node": {"name": "velociraptor", "speed": 50, "force": 20}}' -X POST http://localhost:7001/node/create
```
```
{"_":{"#":"jcuri9qkWxVJXvqCZpTZ",">":{"name":1516901686580.001,"speed":1516901686580.001,"force":1516901686580.001}},"name":"velociraptor","speed":50,"force":20}
```
## Get
Get `dinos.456` node.
```
curl -H "Content-Type: application/json" -d '{"path": "dinos.456"}' -X POST http://localhost:7001/node/get
```
```
{"name":"velociraptor","speed":50,"force":20}
```
Get all nodes under `dinos`.
```
curl -H "Content-Type: application/json" -d '{"path": "dinos"}' -X POST http://localhost:7001/node/get
```
```
{"456":{"name":"velociraptor","speed":50,"force":20}}
```
## Delete
```
curl -H "Content-Type: application/json" -d '{"path": "dinos.456"}' -X POST http://localhost:7001/node/delete
## Init and start host
```
const GunHost = require('gun-host');
# Development
## Install dev tools
```
npm install -g eslint eslint-babel nodemon eslint-config-google
```
## Test
```
npm run test
```
const config = {
enabled: true,
name: 'sentinl',
priority_for_master: 0,
absent_time_for_delete: 86400,
absent_time: 15,
loop_delay: 5,
cert: {
selfsigned: true,
valid: 10,
key: null, // full sys path to pem key file
cert: null, // full sys path to cert file
},
gun: {
port: 9000,
host: '0.0.0.0',
cache: 'data.json',
peers: ['https://localhost:9000/gun'],
},
host: {
id: '123',
name: 'velociraptor',
priority: 0,
node: 'hosts',
},
};
# Docker
## Create image
```
docker build -t <your username>/gun-host .
const node = new GunHost({
peers: config.gun.peers,
rootNodeName: config.name,
});
const main = async function() {
try {
let resp = await node.start({
host: config.gun.host,
port: config.gun.port,
cache: config.gun.cache,
cert: config.cert,
});
console.log('1. Start server:', resp);
resp = await node.add(`${config.host.node}.${config.host.id}`, config.host);
console.log('2. Add node:', resp);
resp = await node.get(config.host.node);
console.log('3. Get node:', resp);
} catch (err) {
console.error(err);
}
};
main();
```
## Run container
```
docker run -p 0.0.0.0::7000 -p 0.0.0.0::7001 -p 0.0.0.0::8000 -p 0.0.0.0::8001 -d sergibondarenko/gun-host
```
## Test
### Show the running container process, binded network ports, status etc.
Gun holds
```
docker ps
root: {
senitnl: {}
}
```
### Show logs
## Add/get data
```
docker logs <container id>
const addData = async function() {
try {
resp = await node.add(`${config.host.node}.${config.host.id}`, config.host);
console.log('2. Add node:', resp);
resp = await node.get(config.host.node);
console.log('3. Get node:', resp);
} catch (err) {
console.error(err);
}
};
addData();
```
Gun holds
```
root: {
setninl: {
hosts: {
'123': {
id: '123',
name: 'velociraptor',
priority: 0,
node: 'hosts',
}
}
}
}
```
## Delete data
```
const delete = async function() {
try {
resp = await node.delete(`${config.host.node}.${config.host.id}`);
console.log('4. Delete node:', resp);
resp = await node.get(config.host.node);
console.log('5. Check if node exists after removing', resp);
} catch (err) {
console.error(err);
}
};
delete();
```
## Enter the container
Gun holds
```
docker exec -it <container id> /bin/bash
root: {
setninl: {
hosts: {}
}
}
```
7 changes: 1 addition & 6 deletions bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
require('babel-register')({
presets: ['env'],
});
require('babel-polyfill');
require('./server/server');

require('./examples/usage');
58 changes: 58 additions & 0 deletions examples/usage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const GunHost = require('../src/gun-host');

const config = {
enabled: true,
name: 'sentinl',
priority_for_master: 0,
absent_time_for_delete: 86400,
absent_time: 15,
loop_delay: 5,
cert: {
selfsigned: true,
valid: 10,
key: null,
cert: null,
},
gun: {
port: 9000,
host: '0.0.0.0',
cache: 'data.json',
peers: ['https://localhost:9000/gun'],
},
host: {
id: '123',
name: 'velociraptor',
priority: 0,
node: 'hosts',
},
};

const node = new GunHost({
peers: config.gun.peers,
rootNodeName: config.name,
});

node.start({
host: config.gun.host,
port: config.gun.port,
cache: config.gun.cache,
cert: config.cert,
}).then((resp) => {
console.log('1. Start server:', resp);
}).then(() => {
return node.add(`${config.host.node}.${config.host.id}`, config.host);
}).then((resp) => {
console.log('2. Add node:', resp);
}).then(() => {
return node.get(config.host.node);
}).then((resp) => {
console.log('3. Get node:', resp);
}).then(() => {
return node.delete(`${config.host.node}.${config.host.id}`);
}).then((resp) => {
console.log('4. Delete node:', resp);
}).then(() => {
return node.get(config.host.node);
}).then((resp) => {
console.log('5. Check if node exists after removing', resp);
});
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./src/gun-host');
Loading

0 comments on commit 1a49657

Please sign in to comment.