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,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'});

View File

@ -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"
}

View File

@ -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;
}
};

View File

@ -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;