diff --git a/app.js b/app.js index f4238cc..3aa5360 100644 --- a/app.js +++ b/app.js @@ -3,12 +3,13 @@ var http = require('http'); var path = require('path'); var favicon = require('static-favicon'); var logger = require('morgan'); -// var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes'); var users = require('./routes/user'); +var db = require('./models'); + var app = express(); app.use(favicon()); @@ -18,9 +19,6 @@ app.use(bodyParser.urlencoded()); // app.use(cookieParser()); app.use(app.router); -app.get('/', routes.index); -app.get('/users', users.list); - /// catch 404 and forwarding to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); @@ -52,4 +50,21 @@ app.use(function(err, req, res, next) { module.exports = app; -app.listen(3000); +// routes +app.get('/', routes.index); + +// database and start up +db + .sequelize + .sync({ force: true }) + .complete(function (err) { + if (err) { + throw err; + } + + port = app.get('port') || 3000; + + http.createServer(app).listen(port, function () { + console.log('Express server listening on port ' + port); + }); + }); diff --git a/models/.index.js.swp b/models/.index.js.swp new file mode 100644 index 0000000..4dabc8a Binary files /dev/null and b/models/.index.js.swp differ diff --git a/models/index.js b/models/index.js new file mode 100644 index 0000000..47fe94e --- /dev/null +++ b/models/index.js @@ -0,0 +1,34 @@ +// Taken directly from Sequelize documentation + +var fs = require('fs'), + path = require('path'), + Sequelize = require('sequelize-sqlite').sequelize, + sqlite = require('sequelize-sqlite').sqlite, + lodash = require('lodash'), + db = {}; + +var sequelize = new Sequelize('database', 'username', null, { + dialect: 'sqlite', + storage: './luncho.sq' +}); + +fs + .readdirSync(__dirname) + .filter(function(file) { + return (file.indexOf('.') !== 0) && (file !== 'index.js'); + }) + .forEach(function(file) { + var model = sequelize.import(path.join(__dirname, file)); + db[model.name] = model; + }); + +Object.keys(db).forEach(function(modelName) { + if ('associate' in db[modelName]) { + db[modelName].associate(db); + } +}); + +module.exports = lodash.extend({ + sequelize: sequelize, + Sequelize: Sequelize +}, db); diff --git a/models/users.js b/models/users.js new file mode 100644 index 0000000..fa59a7a --- /dev/null +++ b/models/users.js @@ -0,0 +1,13 @@ +module.exports = function(sequelize, DataTypes) { + var User = sequelize.define('User', { + username: DataTypes.STRING, + passhash: DataTypes.STRING, + token: DataTypes.STRING, // this forces only one hash per user + issuedDate: DataTypes.DATE // since tokens are one way only, we need to make sure the token is valid for today + }, { + classMethods: { + } + }); + + return User; +}; diff --git a/package.json b/package.json index 79dadb3..b8c0d58 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,8 @@ "static-favicon": "~1.0.0", "morgan": "~1.0.0", "body-parser": "~1.0.0", - "debug": "~0.7.4" + "debug": "~0.7.4", + "sequelize-sqlite": "", + "lodash": "" } }