node-http-server – Diff between revs 15 and 29
?pathlinks?
Rev 15 | Rev 29 | |||
---|---|---|---|---|
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 } |
||
- | 58 | winston.setLevels(winston.config.syslog.levels); |
||
57 | const log = new winston.Logger({ |
59 | const log = new winston.Logger({ |
|
58 | transports: [ |
60 | transports: [ |
|
59 | new winston.transports.File({ |
61 | new winston.transports.File({ |
|
60 | level: 'info', |
62 | level: 'info', |
|
61 | filename: path.resolve( |
63 | filename: path.resolve( |
|
62 | path.dirname(require.main.filename), |
64 | path.dirname(require.main.filename), |
|
63 | config.log.file |
65 | config.log.file |
|
64 | ), |
66 | ), |
|
65 | handleExceptions: true, |
67 | handleExceptions: true, |
|
66 | json: false, |
68 | json: false, |
|
67 | maxsize: 1048576, // 1MiB. |
69 | maxsize: 1048576, // 1MiB. |
|
68 | maxFiles: 10, // Ten rotations. |
70 | maxFiles: 10, // Ten rotations. |
|
69 | colorize: false, |
71 | colorize: false, |
|
70 | timestamp: () => moment() |
72 | timestamp: () => moment() |
|
71 | .format('YYYYMMDDTHHmmss') |
73 | .format('YYYYMMDDTHHmmss') |
|
72 | }), |
74 | }), |
|
73 | new winston.transports.Console({ |
75 | new winston.transports.Console({ |
|
74 | level: 'info', |
76 | level: 'info', |
|
75 | handleExceptions: true, |
77 | handleExceptions: true, |
|
76 | json: false, |
78 | json: false, |
|
77 | colorize: true, |
79 | colorize: true, |
|
78 | timestamp: () => moment() |
80 | timestamp: () => moment() |
|
79 | .format('YYYYMMDDTHHmmss') |
81 | .format('YYYYMMDDTHHmmss') |
|
80 | }) |
82 | }) |
|
81 | ], |
83 | ], |
|
82 | exitOnError: false |
84 | exitOnError: false |
|
83 | }); |
85 | }); |
|
84 | |
86 | |
|
85 | fs.realpath(argv.root, (error, root) => { |
87 | fs.realpath(argv.root, (error, root) => { |
|
86 | if (error) { |
88 | if (error) { |
|
87 | log.error('Could not find document root: ' + argv.root); |
89 | log.error('Could not find document root: ' + argv.root); |
|
88 | process.exit(1); |
90 | process.exit(1); |
|
89 | } |
91 | } |
|
90 | |
92 | |
|
91 | // Start HTTP server. |
93 | // Start HTTP server. |
|
92 | http.createServer( |
94 | http.createServer( |
|
93 | // authentication, |
95 | // authentication, |
|
94 | (request, response) => |
96 | (request, response) => |
|
95 | handler.process(config, request, response, root, (error, level) => { |
97 | handler.process(config, request, response, root, (result) => { |
|
- | 98 | |
||
96 | switch (level) { |
99 | // Log the message by severity. |
|
97 | case handler.error.level.INFO: |
100 | log.log(result.severity, result.message); |
|
98 | log.info(error); |
101 | if(result.status) |
|
99 | break; |
102 | response.status = result.status; |
|
- | 103 | |
||
100 | case handler.error.level.WARN: |
104 | // If no data stream is made available or the content type is not |
|
101 | log.warn(error); |
105 | // set then there is no further reason to bother with the request. |
|
- | 106 | if(typeof result.data === 'undefined' || result.data === null || |
||
- | 107 | typeof result.type === 'undefined' || result.type === null) |
||
102 | break; |
108 | return; |
|
- | 109 | |
||
103 | case handler.error.level.ERROR: |
110 | // Set the content type and send the data. |
|
104 | log.error(error); |
111 | response.setHeader('Content-Type', result.type); |
|
105 | break; |
112 | result.data |
|
106 | } |
113 | .on('open', () => result.data.pipe(response)); |
|
- | 114 | |
||
107 | }) |
115 | }) |
|
108 | ).listen(config.net.port, config.net.address, () => { |
116 | ).listen(config.net.port, config.net.address, () => { |
|
109 | log.info('HTTP Server accessible at: http://' + |
117 | log.info('HTTP Server accessible at: http://' + |
|
110 | config.net.address + |
118 | config.net.address + |
|
111 | ':' + |
119 | ':' + |
|
112 | config.net.port + |
120 | config.net.port + |
|
113 | ' and serving files from directory: ' + |
121 | ' and serving files from directory: ' + |
|
114 | root |
122 | root |
|
115 | ); |
123 | ); |
|
116 | }); |
124 | }); |
|
117 | |
125 | |
|
118 | // Start HTTPs server if enabled. |
126 | // Start HTTPs server if enabled. |
|
119 | if (config.ssl.enable) { |
127 | if (config.ssl.enable) { |
|
120 | // Generate certificates for HTTPs. |
128 | // Generate certificates for HTTPs. |
|
121 | certs.generate( |
129 | certs.generate( |
|
122 | config.site.name, |
130 | config.site.name, |
|
123 | config.net.address, |
131 | config.net.address, |
|
124 | config.ssl.privateKeySize, |
132 | config.ssl.privateKeySize, |
|
125 | (certificates) => { |
133 | (certificates) => { |
|
126 | https.createServer( |
134 | https.createServer( |
|
127 | // authentication, |
135 | // authentication, |
|
128 | { |
136 | { |
|
129 | key: certificates.privateKey, |
137 | key: certificates.privateKey, |
|
130 | cert: certificates.certificate, |
138 | cert: certificates.certificate, |
|
131 | }, |
139 | }, |
|
132 | (request, response) => |
140 | (request, response) => |
|
133 | handler.process(config, request, response, root, (error, level) => { |
141 | handler.process(config, request, response, root, (result) => { |
|
134 | switch (level) { |
142 | // Log the message by severity. |
|
135 | case handler.error.level.INFO: |
143 | log.log(result.severity, result.message); |
|
136 | log.info(error); |
144 | if(result.status) |
|
137 | break; |
145 | response.status = result.status; |
|
- | 146 | |
||
138 | case handler.error.level.WARN: |
147 | // If no data stream is made available or the content type is not |
|
139 | log.warn(error); |
148 | // set then there is no further reason to bother with the request. |
|
- | 149 | if(typeof result.data === 'undefined' || result.data === null || |
||
- | 150 | typeof result.type === 'undefined' || result.type === null) |
||
140 | break; |
151 | return; |
|
- | 152 | |
||
141 | case handler.error.level.ERROR: |
153 | // Set the content type and send the data. |
|
142 | log.error(error); |
154 | response.setHeader('Content-Type', result.type); |
|
143 | break; |
155 | result.data |
|
144 | } |
156 | .on('open', () => result.data.pipe(response)); |
|
145 | }) |
157 | }) |
|
146 | ).listen(config.ssl.port, config.ssl.address, () => { |
158 | ).listen(config.ssl.port, config.ssl.address, () => { |
|
147 | log.info('HTTPs Server accessible at: https://' + |
159 | log.info('HTTPs Server accessible at: https://' + |
|
148 | config.ssl.address + |
160 | config.ssl.address + |
|
149 | ':' + |
161 | ':' + |
|
150 | config.ssl.port + |
162 | config.ssl.port + |
|
151 | ' and serving files from directory: ' + |
163 | ' and serving files from directory: ' + |
|
152 | root |
164 | root |
|
153 | ); |
165 | ); |
|
154 | }) |
166 | }) |
|
155 | } |
167 | } |
|
156 | ); |
168 | ); |
|
157 | } |
169 | } |
|
158 | }); |
170 | }); |
|
159 | |
171 | |
|
160 | |
172 | |
|
161 |
|
173 |
|
|
162 | |
174 | |
|
163 | |
175 | |
|
164 | |
176 | |