diff --git a/app.js b/app.js index a67fc63655..435a82d4f4 100644 --- a/app.js +++ b/app.js @@ -4,10 +4,6 @@ var express = require('express'), log4js = require('log4js'), logger = require('./lib/logger'), routes = require('./lib/routes'), - eventApi = require('./lib/eventApi'), - featureApi = require('./lib/featureApi'), - featureArchiveApi = require('./lib/featureArchiveApi'), - strategyApi = require('./lib/strategyApi'), validator = require('express-validator'), app = express(), router = express.Router(), // eslint-disable-line @@ -45,11 +41,8 @@ app.use(bodyParser.json({ strict: false })); app.use(cookieParser()); -eventApi(router); -featureApi(router); -featureArchiveApi(router); -strategyApi(router); -routes(router); +routes.create(router); + app.use(baseUriPath, router); module.exports = app; diff --git a/lib/dbPool.js b/lib/db/dbPool.js similarity index 97% rename from lib/dbPool.js rename to lib/db/dbPool.js index 5c7cb0ff29..640238a928 100644 --- a/lib/dbPool.js +++ b/lib/db/dbPool.js @@ -1,4 +1,4 @@ -var logger = require('./logger'); +var logger = require('../logger'); var nconf = require('nconf'); var fs = require('fs'); var ini = require('ini'); diff --git a/lib/eventDb.js b/lib/db/event.js similarity index 100% rename from lib/eventDb.js rename to lib/db/event.js diff --git a/lib/featureDb.js b/lib/db/feature.js similarity index 93% rename from lib/featureDb.js rename to lib/db/feature.js index 28f51bd173..9a7d2dad14 100644 --- a/lib/featureDb.js +++ b/lib/db/feature.js @@ -1,8 +1,8 @@ -var eventStore = require('./eventStore'); -var eventType = require('./eventType'); -var logger = require('./logger'); +var eventStore = require('../eventStore'); +var eventType = require('../eventType'); +var logger = require('../logger'); var knex = require('./dbPool'); -var NotFoundError = require('./error/NotFoundError'); +var NotFoundError = require('../error/NotFoundError'); var FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategy_name', 'parameters']; eventStore.on(eventType.featureCreated, function (event) { diff --git a/lib/strategyDb.js b/lib/db/strategy.js similarity index 89% rename from lib/strategyDb.js rename to lib/db/strategy.js index 2972bdfae1..faebcfb36b 100644 --- a/lib/strategyDb.js +++ b/lib/db/strategy.js @@ -1,8 +1,8 @@ -var eventStore = require('./eventStore'); -var eventType = require('./eventType'); -var logger = require('./logger'); +var eventStore = require('../eventStore'); +var eventType = require('../eventType'); +var logger = require('../logger'); var knex = require('./dbPool'); -var NotFoundError = require('./error/NotFoundError'); +var NotFoundError = require('../error/NotFoundError'); var STRATEGY_COLUMNS = ['name', 'description', 'parameters_template']; eventStore.on(eventType.strategyCreated, function (event) { diff --git a/lib/eventStore.js b/lib/eventStore.js index 67802c3cfa..8e71fcd61b 100644 --- a/lib/eventStore.js +++ b/lib/eventStore.js @@ -1,5 +1,5 @@ var util = require('util'), - eventDb = require('./eventDb'), + eventDb = require('./db/event'), EventEmitter = require('events').EventEmitter; function EventStore() { diff --git a/lib/eventApi.js b/lib/routes/event.js similarity index 87% rename from lib/eventApi.js rename to lib/routes/event.js index 6c8e2b33e6..6461b1f58a 100644 --- a/lib/eventApi.js +++ b/lib/routes/event.js @@ -1,5 +1,5 @@ -var eventDb = require('./eventDb'); -var eventDiffer = require('./eventDiffer'); +var eventDb = require('../db/event'); +var eventDiffer = require('../eventDiffer'); module.exports = function (app) { app.get('/events', function (req, res) { diff --git a/lib/featureArchiveApi.js b/lib/routes/feature-archive.js similarity index 75% rename from lib/featureArchiveApi.js rename to lib/routes/feature-archive.js index 430b0c82a7..d9b260349f 100644 --- a/lib/featureArchiveApi.js +++ b/lib/routes/feature-archive.js @@ -1,9 +1,9 @@ -var logger = require('./logger'); -var eventStore = require('./eventStore'); -var eventType = require('./eventType'); -var featureDb = require('./featureDb'); -var ValidationError = require('./error/ValidationError'); -var validateRequest = require('./error/validateRequest'); +var logger = require('../logger'); +var eventStore = require('../eventStore'); +var eventType = require('../eventType'); +var featureDb = require('../db/feature'); +var ValidationError = require('../error/ValidationError'); +var validateRequest = require('../error/validateRequest'); module.exports = function (app) { app.get('/archive/features', function (req, res) { diff --git a/lib/featureApi.js b/lib/routes/feature.js similarity index 88% rename from lib/featureApi.js rename to lib/routes/feature.js index 5a4ba8a817..51ab2a8055 100644 --- a/lib/featureApi.js +++ b/lib/routes/feature.js @@ -1,13 +1,13 @@ var Promise = require("bluebird"); -var logger = require('./logger'); -var eventStore = require('./eventStore'); -var eventType = require('./eventType'); -var featureDb = require('./featureDb'); -var NameExistsError = require('./error/NameExistsError'); -var NotFoundError = require('./error/NotFoundError'); -var ValidationError = require('./error/ValidationError'); -var validateRequest = require('./error/validateRequest'); -var extractUser = require('./extractUser'); +var logger = require('../logger'); +var eventStore = require('../eventStore'); +var eventType = require('../eventType'); +var featureDb = require('../db/feature'); +var NameExistsError = require('../error/NameExistsError'); +var NotFoundError = require('../error/NotFoundError'); +var ValidationError = require('../error/ValidationError'); +var validateRequest = require('../error/validateRequest'); +var extractUser = require('../extractUser'); module.exports = function (app) { app.get('/features', function (req, res) { diff --git a/lib/routes.js b/lib/routes/health-check.js similarity index 85% rename from lib/routes.js rename to lib/routes/health-check.js index f354178c10..76b84afe1c 100644 --- a/lib/routes.js +++ b/lib/routes/health-check.js @@ -1,5 +1,5 @@ -var knex = require('./dbPool'); -var logger = require('./logger'); +var knex = require('../db/dbPool'); +var logger = require('../logger'); module.exports = function (app) { app.get('/health', function (req, res) { diff --git a/lib/routes/index.js b/lib/routes/index.js new file mode 100644 index 0000000000..0c25412d3d --- /dev/null +++ b/lib/routes/index.js @@ -0,0 +1,11 @@ +/** + * TODO: we should also inject config and + * services to the routes to ease testing. +**/ +exports.create = function (app) { + require('./event')(app); + require('./feature')(app); + require('./feature-archive')(app); + require('./strategy')(app); + require('./health-check')(app); +}; diff --git a/lib/strategyApi.js b/lib/routes/strategy.js similarity index 85% rename from lib/strategyApi.js rename to lib/routes/strategy.js index 2837f3ff89..20e635d897 100644 --- a/lib/strategyApi.js +++ b/lib/routes/strategy.js @@ -1,13 +1,13 @@ var Promise = require("bluebird"); -var eventStore = require('./eventStore'); -var eventType = require('./eventType'); -var strategyDb = require('./strategyDb'); -var logger = require('./logger'); -var NameExistsError = require('./error/NameExistsError'); -var ValidationError = require('./error/ValidationError'); -var NotFoundError = require('./error/NotFoundError'); -var validateRequest = require('./error/validateRequest'); -var extractUser = require('./extractUser'); +var eventStore = require('../eventStore'); +var eventType = require('../eventType'); +var strategyDb = require('../db/strategy'); +var logger = require('../logger'); +var NameExistsError = require('../error/NameExistsError'); +var ValidationError = require('../error/ValidationError'); +var NotFoundError = require('../error/NotFoundError'); +var validateRequest = require('../error/validateRequest'); +var extractUser = require('../extractUser'); module.exports = function (app) { app.get('/strategies', function (req, res) { diff --git a/test/specHelper.js b/test/specHelper.js index 3f8f7552ca..65db1d5882 100644 --- a/test/specHelper.js +++ b/test/specHelper.js @@ -4,9 +4,9 @@ process.env.NODE_ENV = 'test'; var Promise = require('bluebird'); var request = require('supertest'); var app = require('../app'); -var knex = require('../lib/dbPool'); -var featureDb = require('../lib/featureDb'); -var strategyDb = require('../lib/strategyDb'); +var knex = require('../lib/db/dbPool'); +var featureDb = require('../lib/db/feature'); +var strategyDb = require('../lib/db/strategy'); Promise.promisifyAll(request); request = request(app);