Skip to content

Commit

Permalink
👻 Major refactoring 😱
Browse files Browse the repository at this point in the history
  • Loading branch information
simov committed May 16, 2018
1 parent 2700d85 commit 1f94d48
Show file tree
Hide file tree
Showing 21 changed files with 328 additions and 622 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
language: node_js

node_js:
- node
- 10
- 8

after_script:
- npm run test-cov
- npm run test:cov
- cat ./coverage/lcov.info | coveralls

sudo: false
172 changes: 31 additions & 141 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,160 +3,50 @@

[![travis-ci]][travis] [![coveralls-status]][coveralls]


# nvm

```bash
nvm install node 4
nvm install node 8
```


# varnalab-whois

```bash
git clone [email protected]:VarnaLab/varnalab-whois.git
cd varnalab-whois
npm i --production
```


# varnalab-cli

```bash
git clone [email protected]:VarnaLab/varnalab-cli.git
cd varnalab-cli
npm i --production
```


# mikrotik.json

```json
{
"development": {
"host": "",
"user": "",
"pass": "",
"port": 0
},
"production": {
"host": "",
"user": "",
"pass": "",
"port": 0
}
}
```


# users.json

```json
[]
```

# devices.json

```json
[]
varnalab-whois \
--users /path/to/users.json \
--devices /path/to/devices.json \
--blacklist /path/to/blacklist.json \
--online /path/to/online.json
```

# whois-online.sh
## [@varnalab/cli][varnalab-cli] + varnalab-whois

```bash
#!/bin/bash

# globals
user=varnalab
nodev4=v4.6.1
nodev8=v8.2.1

# node
node4=/home/$user/.nvm/versions/node/$nodev4/bin/node
node8=/home/$user/.nvm/versions/node/$nodev8/bin/node

# locations
config=/home/$user/simo/config/varnalab-whois
projects=/home/$user/simo/projects
serve=/home/$user/services/public

# projects
cli=$projects/varnalab-cli/bin/whois.js
whois=$projects/varnalab-whois/bin/


#########################################################

if [ $(hostname) = "nuc" ]; then
node4=/home/s/.nvm/versions/node/v4.8.7/bin/node
node=node
elif [ $(hostname) = "box" ]; then
node4=/home/s/.nvm/versions/node/v4.8.6/bin/node
node=/home/s/.nvm/versions/node/v8.9.1/bin/node
fi

# @varnalab/cli
json=$($node4 $cli --config $config/mikrotik.json --env production --output json)

# varnalab.org - active.json
echo $json > $serve/active.json


# --output api - api.varnalab.org
# --output slack - varnalab.slack.com
echo $online |
$node8 $whois \
--users $config/users.json \
--devices $config/devices.json \
--output slack \
> $serve/online.json
```


# crontab
online=$($node4 \
~/projects/varnalab-cli/bin/whois.js \
--config ~/config/varnalab-whois/mikrotik.json \
--env production \
--output json\
)

```bash
# update on every 5 minutes
*/5 * * * * /home/varnalab/simo/config/varnalab-whois/whois-online.sh
```


# Nginx

```nginx
server {
listen *:80;
listen *:443 ssl;
server_name json.varnalab.org ;
root /serve/location/;
# ... set up SSL locations
# convert POST to GET
error_page 405 =200 @405;
location @405 {
root /serve/location/;
proxy_method GET;
proxy_pass http://static_backend;
}
}
# POST STATIC CONTENT
server {
listen 127.0.0.1:89;
server_name _;
root /serve/location/;
}
# varnalab-whois
echo $online | $node \
~/projects/varnalab-whois/bin/cli.js \
--users ~/config/varnalab-api/users.json \
--devices ~/config/varnalab-api/devices.json \
--blacklist ~/config/varnalab-api/blacklist.json \
--online ~/config/varnalab-api/online.json
```


# URLs

- used in *varnalab.org*
- https://json.varnalab.org/services/active.json
- used in *varnalab.github.io*
- https://json.varnalab.org/services/whois-online.json
- used in *varnalab.slack.com*
- https://json.varnalab.org/services/whois-active.json

[travis-ci]: https://img.shields.io/travis/VarnaLab/varnalab-whois/master.svg?style=flat-square (Build Status - Travis CI)
[coveralls-status]: https://img.shields.io/coveralls/VarnaLab/varnalab-whois.svg?style=flat-square (Test Coverage - Coveralls)

[travis-ci]: https://img.shields.io/travis/VarnaLab/varnalab-whois/master.svg?style=flat-square (Build Status - Travis CI)
[coveralls-status]: https://img.shields.io/coveralls/VarnaLab/varnalab-whois.svg?style=flat-square (Test Coverage - Coveralls)
[travis]: https://travis-ci.org/VarnaLab/varnalab-whois
[coveralls]: https://coveralls.io/github/VarnaLab/varnalab-whois

[travis]: https://travis-ci.org/VarnaLab/varnalab-whois
[coveralls]: https://coveralls.io/github/VarnaLab/varnalab-whois
[varnalab-cli]: https://github.com/VarnaLab/varnalab-cli
75 changes: 75 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env node

var argv = require('minimist')(process.argv.slice(2))

if (argv.help) {
console.log(`
--users /path/to/users.json
--devices /path/to/devices.json
--blacklist /path/to/blacklist.json
--online /path/to/online.json
`)
process.exit()
}

;['users', 'devices', 'blacklist', 'online'].forEach((file) => {
if (!argv[file]) {
console.log(`Specify --${file} /path/to/${file}.json`)
process.exit()
}
})


var fs = require('fs')
var path = require('path')

var fpath = {
users: path.resolve(process.cwd(), argv.users),
devices: path.resolve(process.cwd(), argv.devices),
blacklist: path.resolve(process.cwd(), argv.blacklist),
online: path.resolve(process.cwd(), argv.online),
}
var users = require(fpath.users)
var devices = require(fpath.devices)
var blacklist = require(fpath.blacklist)

var device = require('../lib/device')
var resolve = require('../lib/resolve')
var online = require('../lib/online')


;(() => {
var data = ''
process.stdin.on('data', (chunk) => {
data += chunk
})
process.stdin.on('end', (chunk) => {
try {
var json = JSON.parse(data)
run({devices, users, active: json.active})
}
catch (err) {
console.log(JSON.stringify({error: 'Услугата временно не е налична'}))
process.exit()
}
})
})()


async function run ({devices, users, active}) {

var created = device.create({devices, active})
if (created.length) {
var resolved = await resolve({devices: created})
devices = devices.concat(resolved)
}

var dirty = device.update({devices, active})
if (dirty || created.length) {
fs.writeFileSync(fpath.devices, JSON.stringify(devices, null, 2), 'utf8')
}

var active = online.format({users, devices, active})
fs.writeFileSync(fpath.online, JSON.stringify(online.filter(active)), 'utf8')

}
73 changes: 0 additions & 73 deletions bin/index.js

This file was deleted.

Loading

0 comments on commit 1f94d48

Please sign in to comment.