Skip to content

Commit

Permalink
Made data dir configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkertkoppes committed Oct 26, 2015
1 parent 4f636ba commit 415037a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
23 changes: 15 additions & 8 deletions localserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ var fs = require('fs');
var Q = require('q');
var mkdirp = require("mkdirp");
var dirname = require('path').dirname;
var resolve = require('path').resolve;
var argv = require('minimist')(process.argv.slice(2));
var port = argv.p||1390;
var basicAuth = require('basic-auth-connect');
var basicAuthCreds = argv.u;
var datadir = argv.d||'data';

app.use(express.static('src'));

Expand Down Expand Up @@ -48,6 +50,11 @@ app.use(function(req, res, next) {
});
});

function getDataFilePath(path) {
var fullpath = resolve(__dirname,datadir,path);
return fullpath;
}

function readFile(path) {
return Q.promise(function(resolve,reject) {
fs.exists(path,function(exists) {
Expand Down Expand Up @@ -89,7 +96,7 @@ function sendError(res) {

//reading the "file system"
app.get(/^\/fs\/(.*)$/, function(req, res) {
var path = __dirname + '/data/' + req.params[0];
var path = getDataFilePath(req.params[0]);
fs.stat(path, function(err, stat) {
if (err) {
res.status(404).send('file not found');
Expand Down Expand Up @@ -154,8 +161,8 @@ function reduceToMap(key) {
//get all, grouped by round
app.get('/scores/',function(req,res) {
Q.all([
readFile(__dirname + '/data/scores.json').then(parseFile),
readFile(__dirname + '/data/teams.json').then(parseFile).then(reduceToMap('number'))
readFile(getDataFilePath('scores.json')).then(parseFile),
readFile(getDataFilePath('teams.json')).then(parseFile).then(reduceToMap('number'))
]).spread(function(result,teams) {
var published = result.scores.filter(filterPublished).reduce(function(rounds,score) {
if (!rounds[score.round]) {
Expand All @@ -171,7 +178,7 @@ app.get('/scores/',function(req,res) {

//get scores by round
app.get('/scores/:round',function(req,res) {
var path = __dirname + '/data/scores.json';
var path = getDataFilePath('scores.json');
var round = parseInt(req.params.round,10);

readFile(path).then(parseFile).then(function(result) {
Expand All @@ -184,15 +191,15 @@ app.get('/scores/:round',function(req,res) {

//get the teams info
app.get('/teams',function(req,res) {
var path = __dirname + '/data/teams.json';
var path = getDataFilePath('teams.json');

readFile(path).then(parseFile).then(function(result) {
res.json(result);
}).catch(sendError(res)).done();
});

app.get('/teams/:nr',function(req,res) {
var path = __dirname + '/data/teams.json';
var path = getDataFilePath('teams.json');

readFile(path).then(parseFile).then(function(result) {
var team = result.filter(function(team) {
Expand All @@ -212,7 +219,7 @@ function writeFile(path, contents, cb) {

// writing the "file system"
app.post(/^\/fs\/(.*)$/, function(req, res) {
var path = __dirname + '/data/' + req.params[0];
var path = getDataFilePath(req.params[0]);
writeFile(path, req.body, function(err) {
if (err) {
console.log(err);
Expand All @@ -224,7 +231,7 @@ app.post(/^\/fs\/(.*)$/, function(req, res) {

// deleting in the "file system"
app.delete(/^\/fs\/(.*)$/, function(req, res) {
var path = __dirname + '/data/' + req.params[0];
var path = getDataFilePath(req.params[0]);
fs.unlink(path, function(err) {
if (err) {
res.status(500).send('error removing file');
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ This is mainly used for development.
- `node localserver.js` then open [localhost:1390/nocache.html](http://localhost:1390/nocache.html)
- to specify another port, use `node localserver.js -p 8000`
- to add basic authentication, use `node localserver.js -u username:password`
- to specify a data dir use `node localserver -d datadir` Relative paths are relative to the localserver script. Absolute paths are, well, absolute. Defaults to `data`
- note that we load `nocache.html` to circumvent the offline cache mechanism

Testing
Expand Down

0 comments on commit 415037a

Please sign in to comment.