node-http-server – Blame information for rev 30

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #!/usr/bin/env node
2  
9 office 3 /*************************************************************************/
4 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
5 /*************************************************************************/
6  
1 office 7 // Import packages.
8 const https = require('https');
7 office 9 const http = require('http');
1 office 10 const path = require('path');
11 const fs = require('fs');
12 const url = require('url');
13 const moment = require('moment');
14 const winston = require('winston');
15 const yargs = require('yargs');
7 office 16 const dns = require('dns');
1 office 17  
8 office 18 // Local imports.
19 const handler = require(
20 path
14 office 21 .resolve(
15 office 22 path.dirname(require.main.filename),
23 'src',
14 office 24 'handler'
25 )
8 office 26 );
27 const certs = require(
28 path
14 office 29 .resolve(
30 path.dirname(require.main.filename),
31 'src',
32 'certs'
33 )
8 office 34 );
35  
12 office 36 // Load configuration file.
8 office 37 const config = require(
38 path
14 office 39 .resolve(
15 office 40 path.dirname(require.main.filename),
14 office 41 'config'
42 )
8 office 43 );
44  
1 office 45 // Get command-line arguments.
46 const argv = yargs
47 .version()
48 .option('root', {
49 alias: 'd',
50 describe: 'Path to the document root',
51 demandOption: true
52 })
53 .help()
54 .argv
55  
56 // Create various logging mechanisms.
29 office 57 // RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 }
58 winston.setLevels(winston.config.syslog.levels);
1 office 59 const log = new winston.Logger({
60 transports: [
61 new winston.transports.File({
62 level: 'info',
14 office 63 filename: path.resolve(
64 path.dirname(require.main.filename),
65 config.log.file
66 ),
1 office 67 handleExceptions: true,
68 json: false,
69 maxsize: 1048576, // 1MiB.
70 maxFiles: 10, // Ten rotations.
71 colorize: false,
14 office 72 timestamp: () => moment()
73 .format('YYYYMMDDTHHmmss')
1 office 74 }),
75 new winston.transports.Console({
76 level: 'info',
77 handleExceptions: true,
78 json: false,
79 colorize: true,
14 office 80 timestamp: () => moment()
81 .format('YYYYMMDDTHHmmss')
1 office 82 })
83 ],
84 exitOnError: false
85 });
86  
8 office 87 fs.realpath(argv.root, (error, root) => {
7 office 88 if (error) {
89 log.error('Could not find document root: ' + argv.root);
90 process.exit(1);
91 }
92  
93 // Start HTTP server.
94 http.createServer(
95 // authentication,
96 (request, response) =>
30 office 97 handler.process(config, request, response, root, (logging, result) => {
98  
29 office 99 // Log the message by severity.
30 office 100 log.log(logging.severity, logging.message);
101  
102 // If no result is to be processed, then terminate the request.
103 if (typeof result === 'undefined' || result === null)
29 office 104 return;
30 office 105  
106 // Set the response status.
107 response.status = result.status;
29 office 108 // Set the content type and send the data.
109 response.setHeader('Content-Type', result.type);
110 result.data
30 office 111 .on('readable', () => result.data.pipe(response))
112 .on('end', () => response.end());
8 office 113 })
7 office 114 ).listen(config.net.port, config.net.address, () => {
12 office 115 log.info('HTTP Server accessible at: http://' +
7 office 116 config.net.address +
12 office 117 ':' +
7 office 118 config.net.port +
13 office 119 ' and serving files from directory: ' +
8 office 120 root
7 office 121 );
122 });
123  
124 // Start HTTPs server if enabled.
8 office 125 if (config.ssl.enable) {
7 office 126 // Generate certificates for HTTPs.
10 office 127 certs.generate(
7 office 128 config.site.name,
129 config.net.address,
8 office 130 config.ssl.privateKeySize,
131 (certificates) => {
132 https.createServer(
133 // authentication,
134 {
135 key: certificates.privateKey,
136 cert: certificates.certificate,
137 },
138 (request, response) =>
29 office 139 handler.process(config, request, response, root, (result) => {
140 // Log the message by severity.
30 office 141 log.log(logging.severity, logging.message);
142  
143 // If no result is to be processed, then terminate the request.
144 if (typeof result === 'undefined' || result === null)
29 office 145 return;
30 office 146  
147 // Set the response status.
148 response.status = result.status;
29 office 149 // Set the content type and send the data.
150 response.setHeader('Content-Type', result.type);
151 result.data
30 office 152 .on('readable', () => result.data.pipe(response))
153 .on('end', () => response.end());
8 office 154 })
155 ).listen(config.ssl.port, config.ssl.address, () => {
12 office 156 log.info('HTTPs Server accessible at: https://' +
157 config.ssl.address +
158 ':' +
159 config.ssl.port +
13 office 160 ' and serving files from directory: ' +
8 office 161 root
162 );
163 })
164 }
7 office 165 );
8 office 166 }
1 office 167 });