2016-05-07 02:12:45 +03:00
|
|
|
var fs = require('fs');
|
|
|
|
|
|
|
|
var uploaddir = __dirname + '/uploads'; // Upload directory
|
|
|
|
var directoryToSentence = {}; // dirname to sentence
|
|
|
|
|
|
|
|
// Here's the program:
|
|
|
|
readConfigFile();
|
|
|
|
startServer();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Synchronous startup stuff before we start handling requests.
|
|
|
|
* This reads the sentences.txt configuration file, creates directories
|
|
|
|
* as needed, and figures out the next file number in each directory.
|
|
|
|
*/
|
|
|
|
function readConfigFile() {
|
2017-03-08 03:51:37 +03:00
|
|
|
var configFile = __dirname + '/screenplays.txt';
|
|
|
|
var totalItens = 0;
|
2016-05-07 02:12:45 +03:00
|
|
|
try {
|
|
|
|
fs.readFileSync(configFile, 'utf8')
|
|
|
|
.trim()
|
|
|
|
.split('\n')
|
|
|
|
.forEach(function(line) {
|
|
|
|
var trimmed = line.trim();
|
|
|
|
if (trimmed === '' || trimmed[0] === '#') {
|
|
|
|
return; // ignore blanks and comments
|
|
|
|
}
|
|
|
|
|
2017-03-08 03:51:37 +03:00
|
|
|
directoryToSentence[totalItens++] = trimmed;
|
|
|
|
//directories.push(directory);
|
2016-05-07 02:12:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
console.error('Error reading configuration file:', configFile,
|
|
|
|
'\n', e);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2017-03-08 03:51:37 +03:00
|
|
|
if (directoryToSentence.length === 0) {
|
2016-05-07 02:12:45 +03:00
|
|
|
console.error('No sentences defined in sentences.txt. Exiting.');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function startServer() {
|
2016-05-09 22:35:59 +03:00
|
|
|
var LEX = require('letsencrypt-express')/*.testing()*/;
|
2016-05-09 22:11:30 +03:00
|
|
|
var http = require('http');
|
|
|
|
var https = require('spdy');
|
|
|
|
var express = require('express');
|
|
|
|
var bodyParser = require('body-parser');
|
2016-12-02 12:21:36 +03:00
|
|
|
var sqlite3 = require('sqlite3').verbose();
|
2016-05-09 22:11:30 +03:00
|
|
|
|
|
|
|
// Read the server configuration file. It must define
|
|
|
|
// letsEncryptHostname and letsEncryptEmailAddress for the
|
|
|
|
// certificate registration process
|
2016-05-09 22:39:13 +03:00
|
|
|
try {
|
|
|
|
var config = JSON.parse(fs.readFileSync('server.conf'));
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
console.error("Failed to read server.conf:", e);
|
|
|
|
console.error("Exiting");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2016-05-09 22:11:30 +03:00
|
|
|
|
2016-12-02 12:21:36 +03:00
|
|
|
var db = new sqlite3.Database(config.db);
|
|
|
|
|
2016-05-09 22:11:30 +03:00
|
|
|
var lex = LEX.create({
|
|
|
|
configDir: __dirname + '/letsencrypt.conf',
|
2016-07-29 22:05:42 +03:00
|
|
|
approveRegistration: function (hostname, approve) {
|
2016-05-09 22:11:30 +03:00
|
|
|
console.log("approveRegistration:", hostname);
|
2016-07-29 22:05:42 +03:00
|
|
|
if (hostname === config.letsEncryptHostname) {
|
|
|
|
approve(null, {
|
|
|
|
domains: [config.letsEncryptHostname],
|
|
|
|
email: config.letsEncryptEmailAddress,
|
|
|
|
agreeTos: true
|
|
|
|
});
|
|
|
|
}
|
2016-05-09 22:11:30 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-05-07 02:12:45 +03:00
|
|
|
var app = express();
|
|
|
|
|
|
|
|
// Serve static files in the public/ directory
|
|
|
|
app.use(express.static('public'));
|
|
|
|
|
|
|
|
// When the client issues a GET request for the list of sentences
|
|
|
|
// create that dynamically from the data we parsed from the config file
|
|
|
|
app.get('/sentences.json', function(request, response) {
|
|
|
|
response.send(directoryToSentence);
|
|
|
|
});
|
|
|
|
|
|
|
|
// When we get POSTs, handle the body like this
|
|
|
|
app.use(bodyParser.raw({
|
2016-05-14 01:25:10 +03:00
|
|
|
type: 'audio/*',
|
|
|
|
limit: 1*1024*1024 // max file size 1 mb
|
2016-05-07 02:12:45 +03:00
|
|
|
}));
|
|
|
|
|
|
|
|
// This is how we handle WAV file uploads
|
|
|
|
app.post('/upload/:dir', function(request, response) {
|
2017-03-08 03:51:37 +03:00
|
|
|
// user id
|
|
|
|
var uid = request.headers.uid
|
|
|
|
// the folder we should write is the sentence hash
|
2016-05-07 02:12:45 +03:00
|
|
|
var dir = request.params.dir;
|
2017-03-08 03:51:37 +03:00
|
|
|
// the sentence itself
|
|
|
|
var sentence = request.headers.sentence
|
|
|
|
|
|
|
|
var extension = '.ogg'; // Firefox gives us opus in ogg
|
|
|
|
if (request.headers['content-type'].startsWith('audio/webm')) {
|
|
|
|
extension = '.webm'; // Chrome gives us opus in webm
|
|
|
|
} else if (request.headers['content-type'].startsWith('audio/mp4a')) {
|
|
|
|
extension = '.m4a'; // iOS gives us mp4a
|
2016-05-07 02:12:45 +03:00
|
|
|
}
|
2017-03-08 03:51:37 +03:00
|
|
|
|
|
|
|
// if the folder does not exist, we create it
|
|
|
|
var folder = uploaddir + "/" + dir + "/";
|
|
|
|
if (!fs.existsSync(folder)) {
|
|
|
|
fs.mkdirSync(folder);
|
|
|
|
fs.writeFileSync(folder + '/sentence.txt', sentence);
|
2016-05-07 02:12:45 +03:00
|
|
|
}
|
2017-03-08 03:51:37 +03:00
|
|
|
|
|
|
|
var path = folder + uid + extension;
|
|
|
|
fs.writeFile(path, request.body, {}, function(err) {
|
|
|
|
response.send('Thanks for your contribution!');
|
|
|
|
if (err) {
|
|
|
|
console.warn(err);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log('wrote file:', path);
|
|
|
|
}
|
|
|
|
});
|
2016-05-07 02:12:45 +03:00
|
|
|
});
|
|
|
|
|
2016-12-02 12:21:36 +03:00
|
|
|
app.get('/data/', function(request,response) {
|
|
|
|
db.serialize(function() {
|
|
|
|
var id = Math.random() * Date.now() * (request.headers.gender + request.headers.age + request.headers.langs1 + request.headers.langs2);
|
|
|
|
var stmt = db.prepare("INSERT INTO usr VALUES (?,?,?,?,?)");
|
|
|
|
stmt.run(id, request.headers.gender, request.headers.age, request.headers.langs1, request.headers.langs2);
|
|
|
|
stmt.finalize();
|
|
|
|
response.send({ uid: id });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-15 04:48:34 +03:00
|
|
|
app.get('/data/ios', function(request,response) {
|
|
|
|
db.serialize(function() {
|
|
|
|
var stmt = db.prepare("INSERT INTO usr VALUES (?,?,?,?,?)");
|
|
|
|
stmt.run(request.headers.id, request.headers.gender, request.headers.age, request.headers.langs1, request.headers.langs2);
|
|
|
|
stmt.finalize();
|
|
|
|
response.send({ uid: request.headers.id });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-05-09 23:41:47 +03:00
|
|
|
// In test mode, just run the app over http to localhost:8000
|
|
|
|
if (process.argv[2] === 'test') {
|
|
|
|
app.listen(8000, function() {
|
|
|
|
console.log("listening on port 8000");
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-09 22:11:30 +03:00
|
|
|
// Redirect all HTTP requests to HTTPS
|
|
|
|
http.createServer(LEX.createAcmeResponder(lex, function(req, res) {
|
|
|
|
res.setHeader('Location', 'https://' + req.headers.host + req.url);
|
|
|
|
res.statusCode = 302;
|
|
|
|
res.end('<!-- Please use https:// links instead -->');
|
2016-12-02 12:21:36 +03:00
|
|
|
})).listen(config.httpPort || 8080);
|
2016-05-09 22:11:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
// Handle HTTPs requests using LEX and the Express app defined above
|
|
|
|
https.createServer(lex.httpsOptions,
|
|
|
|
LEX.createAcmeResponder(lex, app))
|
|
|
|
.listen(config.httpsPort || 443);
|
2016-05-07 02:12:45 +03:00
|
|
|
}
|