aboutsummaryrefslogtreecommitdiff
path: root/runtime.js
blob: 4cfb589ff457d7bfc083d7afa77d27afdc4224c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const http = require('http');
const chalk = require('chalk');
const qs = require('querystring');
const formidable = require('formidable'); 

global.core = {
    'finder': require('./core/finder.js'),
    'access': require('./core/access.js'),
    'request': require('./core/request.js'),
    'headClean': require('./core/headClean.js'),
    'cookies': require('./core/cookies.js'),
}

global.php = {
    'runtime': require('./php/runtime.js'),
    'cache': require('./php/cache.js'),
    'cleanup': require('./php/cleanup.js'),
}

global.version = require('./package.json').version;

try {
    global.config = {
        'network': require('./config/network.json'),
        'product': require('./config/product.json'),
    }
} catch (e) {
    console.error(chalk.gray(cluster.worker.id + " ") + chalk.red("error:") + " invalid config: " + e.message);
    process.exit(2);
}

var server = http.createServer(function (req, res) {
    global.REQUEST_START = Math.floor(new Date() / 1000)
    global.REQUEST_START_FLOAT = new Date() / 1000
    try {
        if (req.method == 'POST') {
            var form = new formidable.IncomingForm();
            form.parse(req, function (err, fields, files) {
                if (err) {
                    res.writeHead(500, { 'Content-Type': 'text/html' });
                    res.write('<html><head><title>' + error.name + ' - Internal error</title></head><body><h1>' + error.name + '</h1><p>Unable to process received data</p><hr><address>' + config.product.name + ' version ' + version + '</address></body></html>');
                    console.log(chalk.gray(cluster.worker.id + " ") + chalk.red("error:") + " " + error.name + ": " + error.message);
                    console.log(error.stack);
                    res.end();
                } else {
                    core.request(req, res, fields, files);
                }
            });
        } else {
            var post = {};
            core.request(req, res, post);
        }
    } catch (error) {
        res.writeHead(500, { 'Content-Type': 'text/html' });
        res.write('<html><head><title>' + error.name + ' - Internal error</title></head><body><h1>' + error.name + '</h1><p>An internal server error ocurred while trying to give back the file</p><hr><address>' + config.product.name + ' version ' + version + '</address></body></html>');
        console.log(chalk.gray(cluster.worker.id + " ") + chalk.red("error:") + " " + error.name + ": " + error.message);
        console.log(error.stack);
        res.end();
    }
});

server.listen(config.network.port);

console.log(chalk.gray(cluster.worker.id + " ") + chalk.blue("info:") + " server listening on port " + config.network.port)