-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement API, tests, build infrastructure
* Complete the implementation of the API to POST/GET messages. * Add tests for /messages URLs. * Add build infrastructure for linting and running tests. * Add support for different database back-ends via Sequelize. * Add migration for the messages table on the database.
- Loading branch information
1 parent
c7e49f6
commit d8e2a30
Showing
14 changed files
with
600 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
#!/usr/bin/env node | ||
require('../src/app.js'); | ||
var path = require('path'); | ||
require(path.join(__dirname, '..', 'src', 'main')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"db": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var gulp = require('gulp'); | ||
var jshint = require('gulp-jshint'); | ||
var mocha = require('gulp-mocha'); | ||
|
||
gulp.task('lint', function() { | ||
return gulp.src('./src/*.js') | ||
.pipe(jshint()) | ||
.pipe(jshint.reporter('default')); | ||
}); | ||
|
||
gulp.task('lint-tests', function() { | ||
return gulp.src('./test/**/*.js') | ||
.pipe(jshint()) | ||
.pipe(jshint.reporter('default')); | ||
}); | ||
|
||
gulp.task('test-integration', function () { | ||
return gulp.src('./test/integration/test.*.js') | ||
// gulp-mocha needs filepaths so you can't have any plugins before it | ||
.pipe(mocha({reporter: 'nyan'})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: function (queryInterface, Sequelize) { | ||
// create "messages" table | ||
queryInterface.createTable( | ||
'messages', | ||
{ | ||
id: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
createdAt: { | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
type: Sequelize.DATE | ||
}, | ||
text: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
} | ||
} | ||
); | ||
}, | ||
|
||
down: function (queryInterface, Sequelize) { | ||
// drop "message" table | ||
queryInterface.dropTable('message'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
var dbSetup = require('./db'); | ||
var startServer = require('./server'); | ||
var Promise = require('bluebird'); | ||
|
||
// TODO move to config | ||
var PORT = 3000; | ||
/** | ||
* Start the message-store app on hostname:port | ||
* | ||
* @param {string} dbConfigFilePath - Path to the JSON configuration file | ||
* for the database; see README.md | ||
* @param {string} hostname - Host to run the app on | ||
* @param {integer} port - HTTP port to run the app on | ||
* | ||
* @returns {Promise} - Resolves to the Express Application once the server | ||
* has started | ||
*/ | ||
var startApp = function (dbConfigFilePath, hostname, port) { | ||
// TODO pass db config path to dbSetup from env | ||
return dbSetup(dbConfigFilePath).then(function (models) { | ||
return startServer(hostname, port, models); | ||
}); | ||
}; | ||
|
||
// TODO make IP address configurable, defaulting to 0.0.0.0, and pass | ||
// to startServer | ||
|
||
startServer(PORT); | ||
module.exports = startApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// database connection configuration and setup | ||
|
||
var nconf = require('nconf'); | ||
var Sequelize = require('sequelize'); | ||
|
||
// SQLite dialect is the default | ||
var SQLITE_DIALECT = 'sqlite'; | ||
|
||
// dialects available to Sequelize | ||
var DIALECTS = [SQLITE_DIALECT, 'mysql', 'mssql', 'postgres']; | ||
|
||
// string for storage option, to set up SQLite in memory | ||
var IN_MEMORY_STORAGE = ':memory:'; | ||
|
||
// default settings for the Sequelize pool | ||
var DEFAULT_POOL = { | ||
max: 5, | ||
min: 0, | ||
idle: 10000 | ||
}; | ||
|
||
/** | ||
* Configure and return a Sequelize instance; | ||
* the configuration file format is shown in the README. | ||
* | ||
* @param {string} configFilePath - Path to JSON configuration file to load | ||
* db configuration from | ||
* | ||
* @returns {Sequelize} - Configured Sequelize instance | ||
*/ | ||
var getConnection = function (configFilePath) { | ||
// configure from command line, then from env | ||
nconf.argv().env(); | ||
|
||
// configure from file if supplied | ||
if (configFilePath) { | ||
nconf.file({file: configFilePath}); | ||
} | ||
|
||
// default to in-memory sqlite | ||
nconf.defaults({ | ||
dialect: SQLITE_DIALECT, | ||
storage: IN_MEMORY_STORAGE, | ||
pool: DEFAULT_POOL | ||
}); | ||
|
||
// check dialect is valid | ||
var dialect = nconf.get('dialect'); | ||
if (DIALECTS.indexOf(dialect) === -1) { | ||
throw new Error('specified dialect ' + dialect + ' is not available; ' + | ||
'please specify one of ' + JSON.stringify(DIALECTS)); | ||
} | ||
|
||
// check required options | ||
if (dialect === SQLITE_DIALECT) { | ||
nconf.required(['storage']); | ||
} | ||
else { | ||
nconf.required(['database', 'username', 'password']); | ||
if (!(nconf.get('host') || nconf.get('socketPath'))) { | ||
throw new Error('host (or host or socketPath for MySQL) must be ' + | ||
'specified'); | ||
} | ||
} | ||
|
||
// config ok, so set up the connection | ||
var database = nconf.get('database'); | ||
var username = nconf.get('username'); | ||
var password = nconf.get('password'); | ||
var config = nconf.get(); | ||
|
||
return new Sequelize(database, username, password, config); | ||
}; | ||
|
||
module.exports = getConnection; |
Oops, something went wrong.