node-http-server – Rev 35

Subversion Repositories:
Rev:
#!/usr/bin/env node

/*************************************************************************/
/*    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    */
/*************************************************************************/

// Import packages.
const https = require('https');
const http = require('http');
const path = require('path');
const fs = require('fs');
const url = require('url');
const moment = require('moment');
const winston = require('winston');
const yargs = require('yargs');
const dns = require('dns');

// Local imports.
const Handler = require(
    path
    .resolve(
        path.dirname(require.main.filename),
        'src',
        'handler'
    )
);
const certs = require(
    path
    .resolve(
        path.dirname(require.main.filename),
        'src',
        'certs'
    )
);

// Load configuration file.
const config = require(
    path
    .resolve(
        path.dirname(require.main.filename),
        'config'
    )
);

// Get command-line arguments.
const argv = yargs
    .version()
    .option('root', {
        alias: 'd',
        describe: 'Path to the document root',
        demandOption: true
    })
    .help()
    .argv

// Create various logging mechanisms.
// RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 }
winston.setLevels(winston.config.syslog.levels);
const log = new winston.Logger({
    transports: [
        new winston.transports.File({
            level: 'info',
            filename: path.resolve(
                path.dirname(require.main.filename),
                config.log.file
            ),
            handleExceptions: true,
            json: false,
            maxsize: 1048576, // 1MiB.
            maxFiles: 10, // Ten rotations.
            colorize: false,
            timestamp: () => moment()
                .format('YYYYMMDDTHHmmss')
        }),
        new winston.transports.Console({
            level: 'info',
            handleExceptions: true,
            json: false,
            colorize: true,
            timestamp: () => moment()
                .format('YYYYMMDDTHHmmss')
        })
    ],
    exitOnError: false
});

fs.realpath(argv.root, (error, root) => {
    if (error) {
        log.error('Could not find document root: ' + argv.root);
        process.exit(1);
    }

    // Start HTTP server.
    http.createServer(
        // authentication,
        (request, response) =>
        new Handler().process(config, request, response, root)
        .on('log', (data) => {
            log.log(data.severity, data.message);
        })
        .on('data', (result) => {
            response.setHeader('Content-Type', result.type);
            response.writeHead(result.status);
            result.data
                .on('readable', () => result.data.pipe(response))
                .on('end', () => response.end());
        })
    ).listen(config.net.port, config.net.address, () => {
        log.info('HTTP Server accessible at: http://' +
            config.net.address +
            ':' +
            config.net.port +
            ' and serving files from directory: ' +
            root
        );
    });

    // Start HTTPs server if enabled.
    if (config.ssl.enable) {
        // Generate certificates for HTTPs.
        certs.generate(
            config.site.name,
            config.net.address,
            config.ssl.privateKeySize,
            (certificates) => {
                https.createServer(
                    // authentication,
                    {
                        key: certificates.privateKey,
                        cert: certificates.certificate,
                    },
                    (request, response) =>
                    new Handler().process(config, request, response, root)
                    .on('log', (data) => {
                        log.log(data.severity, data.message);
                    })
                    .on('data', (result) => {
                        response.setHeader('Content-Type', result.type);
                        response.writeHead(result.status);
                        result.data
                            .on('readable', () => result.data.pipe(response))
                            .on('end', () => response.end());
                    })
                ).listen(config.ssl.port, config.ssl.address, () => {
                    log.info('HTTPs Server accessible at: https://' +
                        config.ssl.address +
                        ':' +
                        config.ssl.port +
                        ' and serving files from directory: ' +
                        root
                    );
                })
            }
        );
    }
});

Generated by GNU Enscript 1.6.5.90.