From bff2fb1bb17817efeab5c29e53b953de0437c3da Mon Sep 17 00:00:00 2001 From: Meutel Date: Wed, 22 Jan 2014 20:20:43 +0100 Subject: [PATCH] use sqlite --- bin/bootstrap | 15 ++++++++++++--- config/config.json | 3 ++- lib/bouquins.js | 16 ++++++++++++---- lib/endpoint/author.js | 16 +++++++++------- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/bin/bootstrap b/bin/bootstrap index 9801c2f..252fccc 100755 --- a/bin/bootstrap +++ b/bin/bootstrap @@ -27,15 +27,24 @@ bouquins.loadconfig(configfile, function(err, config) { GLOBAL.config = config; // global logger GLOBAL.logger = bouquins.initLogger(); - start(config); + logger.debug(config); + // database + bouquins.initDB(function(err) { + if (err) { + logger.error('Fatal error, cannot load database: ' + err); + logger.error(err.stack); + process.exit(1); + } + }); + start(); }); /** * Initialise application */ -function start(config){ +function start(){ logger.info('Starting '+APP_NAME); - + var starReqListener=function(req, res) { // defaut request listener res.writeHead(200, {'Content-Type': 'text/plain'}); diff --git a/config/config.json b/config/config.json index 661778f..a550a99 100644 --- a/config/config.json +++ b/config/config.json @@ -5,5 +5,6 @@ "library":"endpoint/library.js", "author":"endpoint/author.js" }, - "urlPrefix": "http://127.0.0.1:8080/bouquins" + "urlPrefix": "http://127.0.0.1:8080/bouquins", + "dbfile": "/home/meutel/metadata.db" } diff --git a/lib/bouquins.js b/lib/bouquins.js index 2872a76..8579c80 100644 --- a/lib/bouquins.js +++ b/lib/bouquins.js @@ -4,9 +4,10 @@ */ GLOBAL.PATH_RE=/\/([a-zA-Z0-9]+)(?:\/|$)([a-zA-Z0-9]+)?/; -var config = require('./util/config'), +var Config = require('./util/config'), logger = require('./util/logger'), Router = require('./router/router'), + sqlite3 = require('sqlite3').verbose(), bouquins = exports; var router = null; @@ -14,7 +15,7 @@ var router = null; * Load config file. */ bouquins.loadconfig = function(configfile, callback) { - config.loadconfig(configfile, callback); + Config.loadconfig(configfile, callback); }; /** * Init logger. @@ -24,7 +25,14 @@ bouquins.initLogger = function() { logger.debugLevel = config.debugLevel; } return logger; -} +}; +/** + * Init database. + */ +bouquins.initDB = function(callback) { + logger.debug('Database: '+config.dbfile); + GLOBAL.db = new sqlite3.Database(config.dbfile, callback); +}; /** * Make main router. */ @@ -33,4 +41,4 @@ bouquins.makeRouter = function() { router = new Router(); } return router; -} +}; diff --git a/lib/endpoint/author.js b/lib/endpoint/author.js index 3a64548..a0402b6 100644 --- a/lib/endpoint/author.js +++ b/lib/endpoint/author.js @@ -3,6 +3,7 @@ * Endpoint author. */ var Endpoint = require('./endpoint.js'); + function Author() { Endpoint.call(this); this.authorId = null; @@ -14,12 +15,12 @@ Author.prototype = Object.create(Endpoint.prototype, { switch (action.name) { case 'show': action.resId = this.authorId; + //TODO related action.loadResource = function(resId, callback) { logger.debug('loading author ' + resId); // TODO load from db - callback(null, { - id: resId, - name: 'Test test' + db.get('SELECT * FROM authors WHERE id = '+ resId, function(err, row) { + callback(err, row); }); }; callback(null, action); @@ -27,10 +28,11 @@ Author.prototype = Object.create(Endpoint.prototype, { case 'list': action.loadResources = function(onload, onend) { //TODO load from bdd - onload(null, { id: 1, name: 'Homere' }); - onload(null, { id: 2, name: 'Victor Hugo' }); - onload(null, { id: 3, name: 'Frank Herbert' }); - onend(); + db.each('SELECT * FROM authors LIMIT 30', function (err, row) { + onload(err, row); + }, function() { + onend(); + }); }; callback(null, action); break;