node-http-server – Rev 10
?pathlinks?
#!/usr/bin/env node
/*************************************************************************/
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
/*************************************************************************/
// Import packages.
const auth = require("http-auth");
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(__dirname, 'src', 'handler')
);
const certs = require(
path
.resolve(__dirname, 'src', 'certs')
);
// Configuration file.
const config = require(
path
.resolve(__dirname, '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(__dirname, 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);
}
// Create digest authentication.
const authentication = auth.digest({
realm: config.auth.realm,
file: path.resolve(__dirname, config.auth.digest)
});
// Start HTTP server.
http.createServer(
// authentication,
(request, response) =>
handler.handleClient(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 is listening on: ' +
config.net.address +
' and port: ' +
config.net.port +
' whilst serving files from: ' +
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.handleClient(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, () => {
// Print information on server startup.
log.info('HTTPs Server is listening on: ' +
config.net.address +
' and port: ' +
config.net.port +
' whilst serving files from: ' +
root
);
})
}
);
}
});
Generated by GNU Enscript 1.6.5.90.