node-http-server – Diff between revs 30 and 31

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