use sqlite

This commit is contained in:
Meutel 2014-01-22 20:20:43 +01:00
parent 4b3142bf6b
commit bff2fb1bb1
4 changed files with 35 additions and 15 deletions

View File

@ -27,13 +27,22 @@ bouquins.loadconfig(configfile, function(err, config) {
GLOBAL.config = config; GLOBAL.config = config;
// global logger // global logger
GLOBAL.logger = bouquins.initLogger(); 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 * Initialise application
*/ */
function start(config){ function start(){
logger.info('Starting '+APP_NAME); logger.info('Starting '+APP_NAME);
var starReqListener=function(req, res) { var starReqListener=function(req, res) {

View File

@ -5,5 +5,6 @@
"library":"endpoint/library.js", "library":"endpoint/library.js",
"author":"endpoint/author.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"
} }

View File

@ -4,9 +4,10 @@
*/ */
GLOBAL.PATH_RE=/\/([a-zA-Z0-9]+)(?:\/|$)([a-zA-Z0-9]+)?/; 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'), logger = require('./util/logger'),
Router = require('./router/router'), Router = require('./router/router'),
sqlite3 = require('sqlite3').verbose(),
bouquins = exports; bouquins = exports;
var router = null; var router = null;
@ -14,7 +15,7 @@ var router = null;
* Load config file. * Load config file.
*/ */
bouquins.loadconfig = function(configfile, callback) { bouquins.loadconfig = function(configfile, callback) {
config.loadconfig(configfile, callback); Config.loadconfig(configfile, callback);
}; };
/** /**
* Init logger. * Init logger.
@ -24,7 +25,14 @@ bouquins.initLogger = function() {
logger.debugLevel = config.debugLevel; logger.debugLevel = config.debugLevel;
} }
return logger; return logger;
} };
/**
* Init database.
*/
bouquins.initDB = function(callback) {
logger.debug('Database: '+config.dbfile);
GLOBAL.db = new sqlite3.Database(config.dbfile, callback);
};
/** /**
* Make main router. * Make main router.
*/ */
@ -33,4 +41,4 @@ bouquins.makeRouter = function() {
router = new Router(); router = new Router();
} }
return router; return router;
} };

View File

@ -3,6 +3,7 @@
* Endpoint author. * Endpoint author.
*/ */
var Endpoint = require('./endpoint.js'); var Endpoint = require('./endpoint.js');
function Author() { function Author() {
Endpoint.call(this); Endpoint.call(this);
this.authorId = null; this.authorId = null;
@ -14,12 +15,12 @@ Author.prototype = Object.create(Endpoint.prototype, {
switch (action.name) { switch (action.name) {
case 'show': case 'show':
action.resId = this.authorId; action.resId = this.authorId;
//TODO related
action.loadResource = function(resId, callback) { action.loadResource = function(resId, callback) {
logger.debug('loading author ' + resId); logger.debug('loading author ' + resId);
// TODO load from db // TODO load from db
callback(null, { db.get('SELECT * FROM authors WHERE id = '+ resId, function(err, row) {
id: resId, callback(err, row);
name: 'Test test'
}); });
}; };
callback(null, action); callback(null, action);
@ -27,10 +28,11 @@ Author.prototype = Object.create(Endpoint.prototype, {
case 'list': case 'list':
action.loadResources = function(onload, onend) { action.loadResources = function(onload, onend) {
//TODO load from bdd //TODO load from bdd
onload(null, { id: 1, name: 'Homere' }); db.each('SELECT * FROM authors LIMIT 30', function (err, row) {
onload(null, { id: 2, name: 'Victor Hugo' }); onload(err, row);
onload(null, { id: 3, name: 'Frank Herbert' }); }, function() {
onend(); onend();
});
}; };
callback(null, action); callback(null, action);
break; break;