node-http-server – Rev 24

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.
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) =>
        handler.process(config, request, response, root, (error, level) => {
            switch (level) {
                case handler.error.level.INFO:
                    log.info(error);
                    break;
                case handler.error.level.WARN:
                    log.warn(error);
                    break;
                case handler.error.level.ERROR:
                    log.error(error);
                    break;
            }
        })
    ).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) =>
                    handler.process(config, request, response, root, (error, level) => {
                        switch (level) {
                            case handler.error.level.INFO:
                                log.info(error);
                                break;
                            case handler.error.level.WARN:
                                log.warn(error);
                                break;
                            case handler.error.level.ERROR:
                                log.error(error);
                                break;
                        }
                    })
                ).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.