Skip to content
Snippets Groups Projects
Commit 93c17df6 authored by Tom Wiesing's avatar Tom Wiesing
Browse files

Finish dumping implementation

parent a46cef93
No related branches found
No related tags found
No related merge requests found
// import some modules
var bodyParser = require("body-parser"),
express = require('express'),
http = require('http'),
fs = require("fs"),
path = require("path");
require("string.prototype.endswith");
// counter and prefix for jobs
var job_counter = 0;
var job_prefix = (new Date()).getTime();
var jobs = [];
// get the path for the docs and the dumps
var pathDocs = path.resolve(__dirname+'/docs');
var pathDumps = path.resolve(__dirname+'/dumps');
// Create a really simple API
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}))
// serve only *.xhtml documents
app.use('/docs', function(req, res, next){
if(req.path.endsWith('.xhtml')){
next();
} else {
res.status(404).send('Not found');
}
}, express.static('docs/'));
app.post('/dumps/', function(req, res, next){
// read post data from the body
var post_data = req.body || {};
var id, doc;
if(!post_data.hasOwnProperty('id')){
return res.jsonp({'success': false, 'message': "Missing 'id' parameter. "});
} else {
id = post_data.id;
}
if(jobs.indexOf(id) === -1){
return res.jsonp({'success': false, 'message': "Unknown JOB id. "})
}
if(!post_data.hasOwnProperty('content')){
return res.jsonp({'success': false, 'message': "Missing 'content' parameter. "});
} else {
content = post_data.content;
}
// remove this job from the jobs list
jobs.splice(jobs.indexOf(id));
try{
fs.writeFileSync(pathDumps + '/'+id+'.xhtml', content);
} catch(e) {
console.log(e);
return res.jsonp({'success': false, 'message': 'Unable to save file. '});
}
res.jsonp({
'success': true,
'path': '/dumps/'+id+'.xhtml'
});
});
app.use('/dumps', function(req, res, next){
if(req.path.endsWith('.xhtml')){
next();
} else {
res.status(404).send('Not found');
}
}, express.static('dumps/'));
app.get('/get_task', function(req, res){
// make a job id and name
var jobid = job_counter++;
var jobname = job_prefix+'_'+jobid
jobs.push(jobname);
// get a document
var documents = fs.readdirSync(pathDocs).filter(function(e,i){return e.endsWith('.xhtml');});
var doc = documents[jobid % documents.length];
// get a mode
var mode = ['annotate', 'review'][Math.random() > 0.5 ? 0 : 1]
// TODO: Serve a proper document and result
res.jsonp({
'id': jobname, // a job id. Should be posted as a job id to the result page.
'path': 'docs/'+doc, // the path to the document to annotate
'post': 'dumps/'+doc, // the path you want to post results to. Should contain a 'content' field with the document and a 'id' field with the job id
'mode': mode, // the mode. One of 'annotate', 'review'
'annotations': null, // might contain another path to existing annotations in the future
});
});
// TODO: Implement document dumping & re-reading
var server = http.Server(app);
var port = 3000 || process.env.port;
server.listen(port, function(){
console.log('CorTeXDummy listening at '+port);
})
......@@ -7,11 +7,14 @@
"doc": "docs"
},
"dependencies": {
"express": "^4.13.3"
"express": "^4.13.3",
"body-parser": "^1.10.0",
"string.prototype.endswith": "^0.2.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./index.js"
},
"author": "Tom Wiesing",
"license": "MIT"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment