Skip to content

Commit e9021cc

Browse files
New api (#15)
* Working test framework * Switch logging to bunyan, add test config, http test framework * Switch to using rethinkdbdash * Complete switch to rethinkdbdash * Add all deps * Refactored code layout * Basic test framework * Add redis to circle.yml deps * Add rethinkdb * Fix test
1 parent 5cf72a7 commit e9021cc

21 files changed

+1031
-299
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
node_modules
33
npm-debug.log
4+
dump.rdb

circle.yml

+7
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ machine:
1717

1818
services:
1919
- docker
20+
- redis
2021

2122
dependencies:
2223
pre:
24+
- source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
25+
- wget -qO- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -
26+
- sudo apt-get update
27+
- sudo apt-get install rethinkdb
28+
- sudo cp /etc/rethinkdb/default.conf.sample /etc/rethinkdb/instances.d/instance1.conf
29+
- sudo /etc/init.d/rethinkdb restart
2330
- if [[ -z $CIRCLE_TAG ]] ; then npm install; exit $?; fi
2431
- if [[ -z $CIRCLE_TAG ]] ; then docker login -e [email protected] -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD quay.io; exit $?; fi
2532
- if [[ -z $CIRCLE_TAG ]] ; then docker-build -f deploy/common.config; exit $?; fi

index.js

+25-38
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,29 @@
1-
var dns = require('dns');
2-
dnscache = require('dnscache')({
3-
enable: true,
4-
ttl: 300,
5-
cachesize: 1000
6-
});
7-
var db = require('./lib/db');
8-
9-
process.title = 'api';
10-
11-
var routes = require('./lib/routes');
12-
var port = process.env.PORT || 3001;
1+
var app = require('./lib');
2+
var bunyan = require('bunyan');
3+
var log = bunyan(require('./lib/config').bunyan);
134

14-
var express = require('express');
15-
var logger = require('morgan');
16-
var bodyParser = require('body-parser');
17-
var path = require('path');
18-
19-
var app = express();
20-
app.use(logger('dev'));
21-
app.use(bodyParser.json());
22-
app.use(bodyParser.urlencoded({ extended: false }));
23-
24-
app.use(function (error, request, response, next) {
25-
response.status(error.status || 500);
26-
response.json({ error: error.message });
27-
});
28-
29-
app.use(routes);
30-
31-
db.init(function (err) {
5+
app.start(function onStart (err) {
326
if (err) {
33-
console.error('Problem setting up rethinkdb, exiting');
34-
process.exit(1);
7+
log.error(err);
8+
process.exit();
359
}
36-
console.log('Connected to rethinkdb');
37-
var server = app.listen(port, function () {
38-
var host = server.address().address;
39-
var port = server.address().port;
40-
console.log('App is listening on http://%s:%s', host, port);
10+
process.on('SIGTERM', function onSIGTERM () {
11+
log.info('received SIGTERM');
12+
app.stop(function () {
13+
log.info('Application stopped, exiting');
14+
setTimeout(function () {
15+
process.exit(0);
16+
}, 50);
17+
});
4118
});
42-
})
19+
20+
process.on('SIGINT', function onSIGTERM () {
21+
log.info('received SIGINT');
22+
app.stop(function () {
23+
log.info('Application stopped, exiting');
24+
setTimeout(function () {
25+
process.exit(0);
26+
}, 50);
27+
});
28+
});
29+
});

lib/config/config.global.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
var config = module.exports = {};
2+
require('pkginfo')(module);
3+
var os = require('os');
4+
var bunyan = require('bunyan');
5+
6+
config.modules = [
7+
'db',
8+
'queue',
9+
'snapshots'
10+
];
11+
12+
config.redis = {
13+
host: process.env.REDIS_SERVICE_HOST || '127.0.0.1',
14+
port: process.env.REDIS_SERVICE_PORT || 6379
15+
};
16+
17+
config.bull = {
18+
requestQueue: 'screenshot-request',
19+
responseQueue: 'screenshot-response'
20+
};
21+
22+
config.rethinkdb = {
23+
host: process.env.RETHINKDB_PROXY_SERVICE_HOST || process.env.RETHINKDB_SERVICE_HOST || 'localhost',
24+
port: process.env.RETHINKDB_PROXY_SERVICE_PORT || process.env.RETHINKDB_SERVICE_POST || 28015,
25+
db: 'wayback',
26+
table: 'snapshots',
27+
silent: true
28+
};
29+
30+
config.info = {
31+
version: module.exports.version,
32+
name: module.exports.name,
33+
instance: os.hostname()
34+
};
35+
36+
config.port = process.env.PORT || 3001;
37+
38+
config.bunyan = {
39+
name: config.info.name,
40+
streams: [
41+
{
42+
level: process.env.LOG_LEVEL || 'info',
43+
stream: process.stdout
44+
}
45+
],
46+
serializers: bunyan.stdSerializers,
47+
src: true
48+
};

lib/config/config.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var config = require('./config.global');
2+
3+
config.bunyan.streams = [
4+
{
5+
level: process.env.LOG_LEVEL || 'error',
6+
stream: process.stderr
7+
}
8+
];
9+
config.bunyan.src = true;
10+
11+
module.exports = config;

lib/config/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var env = process.env.NODE_ENV;
2+
var configFile = '';
3+
if (env === 'TESTING') {
4+
configFile = './config.test';
5+
} else {
6+
configFile = './config.global';
7+
}
8+
var config = require(configFile);
9+
module.exports = config;

0 commit comments

Comments
 (0)