Skip to content

Commit 33c3413

Browse files
committed
first commit
0 parents  commit 33c3413

File tree

6 files changed

+115
-0
lines changed

6 files changed

+115
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

app.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require('dotenv').load({ silent: true });
2+
3+
const express = require('express');
4+
const co = require('co');
5+
const bodyParser = require('body-parser');
6+
const Client = require('ssh2');
7+
const fs = require('fs');
8+
const path = require('path');
9+
const config = require('./config');
10+
const { notFound, errorMiddleware } = require('./errors');
11+
12+
const app = new express();
13+
const FILENAME = 'tps.dat';
14+
const connSettings = {
15+
host: config.sftpHost,
16+
port: config.sftpPort,
17+
username: config.sftpUsername,
18+
password: config.sftpPassword
19+
};
20+
const numbers = {};
21+
22+
app.use(bodyParser.json());
23+
24+
app.post('/search', (req, res, next) => {
25+
if (!Array.isArray(req.body)) {
26+
return next({ message: 'Must provide array of numbers', status: 400 })
27+
}
28+
const results = req.body.map((num) => {
29+
return {
30+
number: num,
31+
canCall: numbers[num.replace(/\s/g, '')] || false
32+
};
33+
});
34+
35+
res.json({ results });
36+
});
37+
38+
app.use(notFound);
39+
app.use(errorMiddleware);
40+
41+
const conn = new Client();
42+
conn.on('ready', () => {
43+
const moveFrom = `./${FILENAME}`;
44+
const moveTo = `/tmp/${FILENAME}`;
45+
46+
conn.sftp((err, sftp) => {
47+
if (err) {
48+
throw err;
49+
}
50+
51+
sftp.fastGet(moveFrom, moveTo, {}, (downloadErr) => {
52+
if (downloadErr) {
53+
throw downloadErr;
54+
}
55+
56+
// turn into map for constant lookup
57+
// trim spaces from numbers
58+
fs.readFileSync(`/tmp/${FILENAME}`).toString().split(/\r?\n/).forEach(num => {
59+
numbers[num] = true;
60+
});
61+
62+
app.listen(config.PORT, () => {
63+
console.log(`App listening on ${config.PORT}`);
64+
});
65+
66+
});
67+
});
68+
}).connect(connSettings);

config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const config = exports;
2+
3+
config.PORT = process.env.PORT || 3000;
4+
config.sftpHost = process.env.SFTP_HOST;
5+
config.sftpPort = process.env.SFTP_PORT;
6+
config.sftpUsername = process.env.SFTP_USERNAME;
7+
config.sftpPassword = process.env.SFTP_PASSWORD;

errors.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function notFound(req, res, next) {
2+
const err = new Error('Page Not Found');
3+
err.status = 404;
4+
next(err);
5+
}
6+
7+
// eslint-disable-next-line
8+
function errorMiddleware(err, req, res, next) {
9+
console.log(err);
10+
res.status(err.status || err.statusCode || 500);
11+
return res.json({
12+
message: err.message,
13+
errors: err.errors || []
14+
});
15+
}
16+
17+
module.exports = {
18+
notFound,
19+
errorMiddleware
20+
};

package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "tps-lookup",
3+
"version": "1.0.0",
4+
"description": "Lookup TPS numbers",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Andrew Snead",
10+
"license": "ISC",
11+
"dependencies": {
12+
"body-parser": "^1.17.2",
13+
"co": "^4.6.0",
14+
"dotenv": "^4.0.0",
15+
"express": "^4.15.3",
16+
"ssh2": "^0.5.5"
17+
}
18+
}

tmp/tps.dat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
07752193947

0 commit comments

Comments
 (0)