node-http-server – Diff between revs 37 and 38
?pathlinks?
Rev 37 | Rev 38 | |||
---|---|---|---|---|
Line 3... | Line 3... | |||
3 | /*************************************************************************/ |
3 | /*************************************************************************/ |
|
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
5 | /*************************************************************************/ |
5 | /*************************************************************************/ |
|
Line 6... | Line 6... | |||
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') |
|
Line 17... | Line 17... | |||
17 | const HttpCache = require("http-cache"); |
17 | const HttpCache = require("http-cache") |
|
18 | |
18 | |
|
19 | // Local imports. |
19 | // Local imports. |
|
20 | const Handler = require( |
20 | const Handler = require( |
|
21 | path |
21 | path |
|
22 | .resolve( |
22 | .resolve( |
|
23 | path.dirname(require.main.filename), |
23 | path.dirname(require.main.filename), |
|
24 | 'src', |
24 | 'src', |
|
25 | 'handler' |
25 | 'handler' |
|
- | 26 | ) |
||
26 | ) |
27 | ) |
|
27 | ); |
28 | |
|
28 | const certs = require( |
29 | const certs = require( |
|
29 | path |
30 | path |
|
30 | .resolve( |
31 | .resolve( |
|
31 | path.dirname(require.main.filename), |
32 | path.dirname(require.main.filename), |
|
32 | 'src', |
33 | 'src', |
|
33 | 'certs' |
34 | 'certs' |
|
Line 34... | Line 35... | |||
34 | ) |
35 | ) |
|
35 | ); |
36 | ) |
|
36 | |
37 | |
|
37 | // Load configuration file. |
38 | // Load configuration file. |
|
38 | const config = require( |
39 | const config = require( |
|
39 | path |
40 | path |
|
40 | .resolve( |
41 | .resolve( |
|
41 | path.dirname(require.main.filename), |
42 | path.dirname(require.main.filename), |
|
Line 42... | Line 43... | |||
42 | 'config' |
43 | 'config' |
|
43 | ) |
44 | ) |
|
44 | ); |
45 | ) |
|
45 | |
46 | |
|
Line 54... | Line 55... | |||
54 | .help() |
55 | .help() |
|
55 | .argv |
56 | .argv |
|
Line 56... | Line 57... | |||
56 | |
57 | |
|
57 | // Create various logging mechanisms. |
58 | // Create various logging mechanisms. |
|
58 | // RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } |
59 | // RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } |
|
59 | winston.setLevels(winston.config.syslog.levels); |
60 | winston.setLevels(winston.config.syslog.levels) |
|
60 | const log = new winston.Logger({ |
61 | const log = new winston.Logger({ |
|
61 | transports: [ |
62 | transports: [ |
|
62 | new winston.transports.File({ |
63 | new winston.transports.File({ |
|
63 | level: 'info', |
64 | level: 'info', |
|
Line 81... | Line 82... | |||
81 | timestamp: () => moment() |
82 | timestamp: () => moment() |
|
82 | .format('YYYYMMDDTHHmmss') |
83 | .format('YYYYMMDDTHHmmss') |
|
83 | }) |
84 | }) |
|
84 | ], |
85 | ], |
|
85 | exitOnError: false |
86 | exitOnError: false |
|
86 | }); |
87 | }) |
|
Line 87... | Line 88... | |||
87 | |
88 | |
|
88 | fs.realpath(argv.root, (error, root) => { |
89 | fs.realpath(argv.root, (error, root) => { |
|
89 | if (error) { |
90 | if (error) { |
|
90 | log.error('Could not find document root: ' + argv.root); |
91 | log.error('Could not find document root: ' + argv.root) |
|
91 | process.exit(1); |
92 | process.exit(1) |
|
Line 92... | Line 93... | |||
92 | } |
93 | } |
|
93 | |
94 | |
|
Line 94... | Line 95... | |||
94 | // Create server-side cache. |
95 | // Create server-side cache. |
|
95 | const httpcache = new HttpCache(); |
96 | const httpcache = new HttpCache() |
|
96 | |
97 | |
|
97 | // Start HTTP server. |
98 | // Start HTTP server. |
|
98 | http.createServer( |
99 | http.createServer( |
|
99 | // authentication, |
100 | // authentication, |
|
100 | (request, response) => { |
101 | (request, response) => { |
|
101 | // Configuration path requested, so send the server configuration if allowed. |
102 | // Configuration path requested, so send the server configuration if allowed. |
|
102 | if(config.configuration.enable === true && |
103 | if (config.configuration.enable === true && |
|
103 | url.parse(request.url, true).path === |
104 | url.parse(request.url, true).path === |
|
104 | config.configuration.path) { |
105 | config.configuration.path) { |
|
105 | const address = request.socket.address(); |
106 | const address = request.socket.address() |
|
106 | log.info('HTTP Server configuration requested by: ' + |
107 | log.info('HTTP Server configuration requested by: ' + |
|
107 | address.address + ':' + |
108 | address.address + ':' + |
|
108 | address.port |
109 | address.port |
|
109 | ); |
110 | ) |
|
110 | response.setHeader('Content-Type', 'application/json'); |
111 | response.setHeader('Content-Type', 'application/json') |
|
111 | response.end(JSON.stringify(config)); |
112 | response.end(JSON.stringify(config)) |
|
112 | return; |
113 | return |
|
113 | } |
114 | } |
|
114 | |
115 | |
|
115 | // Process and cache the resource. |
116 | // Process and cache the resource. |
|
116 | httpcache(request, response, () => { |
117 | httpcache(request, response, () => { |
|
117 | new Handler().process(config, request, response, root) |
118 | new Handler().process(config, request, response, root) |
|
118 | .on('log', (data) => { |
119 | .on('log', (data) => { |
|
119 | log.log(data.severity, data.message); |
120 | log.log(data.severity, data.message) |
|
120 | }) |
121 | }) |
|
121 | .on('data', (result) => { |
122 | .on('data', (result) => { |
|
122 | response.setHeader('Content-Type', result.type); |
- | ||
123 | response.writeHead(result.status); |
- | ||
124 | result.data |
123 | response.setHeader('Content-Type', result.type) |
|
125 | .on('readable', () => result.data.pipe(response)) |
124 | response.writeHead(result.status) |
|
126 | .on('end', () => response.end()); |
125 | result.data.pipe(response) |
|
127 | }); |
126 | }) |
|
128 | }); |
127 | }) |
|
129 | } |
128 | } |
|
130 | ).listen(config.net.port, config.net.address, () => { |
129 | ).listen(config.net.port, config.net.address, () => { |
|
131 | log.info('HTTP Server accessible at: http://' + |
130 | log.info('HTTP Server accessible at: http://' + |
|
132 | config.net.address + |
131 | config.net.address + |
|
133 | ':' + |
132 | ':' + |
|
134 | config.net.port + |
133 | config.net.port + |
|
135 | ' and serving files from directory: ' + |
134 | ' and serving files from directory: ' + |
|
Line 136... | Line 135... | |||
136 | root |
135 | root |
|
137 | ); |
136 | ) |
|
138 | }); |
137 | }) |
|
139 | |
138 | |
|
Line 150... | Line 149... | |||
150 | { |
149 | { |
|
151 | key: certificates.privateKey, |
150 | key: certificates.privateKey, |
|
152 | cert: certificates.certificate, |
151 | cert: certificates.certificate, |
|
153 | }, |
152 | }, |
|
154 | (request, response) => { |
153 | (request, response) => { |
|
155 | |
154 | |
|
156 | // Configuration path requested, so send the server configuration if allowed. |
155 | // Configuration path requested, so send the server configuration if allowed. |
|
157 | if(config.configuration.enable === true && |
156 | if (config.configuration.enable === true && |
|
158 | url.parse(request.url, true).path === |
157 | url.parse(request.url, true).path === |
|
159 | config.configuration.path) { |
158 | config.configuration.path) { |
|
160 | const address = request.socket.address(); |
159 | const address = request.socket.address() |
|
161 | log.info('HTTP Server configuration requested by: ' + |
160 | log.info('HTTP Server configuration requested by: ' + |
|
162 | address.address + ':' + |
161 | address.address + ':' + |
|
163 | address.port |
162 | address.port |
|
164 | ); |
163 | ) |
|
165 | response.setHeader('Content-Type', 'application/json'); |
164 | response.setHeader('Content-Type', 'application/json') |
|
166 | response.end(JSON.stringify(config)); |
165 | response.end(JSON.stringify(config)) |
|
167 | return; |
166 | return |
|
168 | } |
167 | } |
|
169 | |
168 | |
|
170 | httpcache(request, response, () => { |
169 | httpcache(request, response, () => { |
|
171 | new Handler().process(config, request, response, root) |
170 | new Handler().process(config, request, response, root) |
|
172 | .on('log', (data) => { |
171 | .on('log', (data) => { |
|
173 | log.log(data.severity, data.message); |
172 | log.log(data.severity, data.message) |
|
174 | }) |
173 | }) |
|
175 | .on('data', (result) => { |
174 | .on('data', (result) => { |
|
176 | response.setHeader('Content-Type', result.type); |
175 | response.setHeader('Content-Type', result.type) |
|
177 | response.writeHead(result.status); |
176 | response.writeHead(result.status) |
|
178 | result.data |
177 | result.data.pipe(response) |
|
179 | .on('readable', () => result.data.pipe(response)) |
- | ||
180 | .on('end', () => response.end()); |
- | ||
181 | }); |
178 | }) |
|
182 | }); |
179 | }) |
|
183 | } |
180 | } |
|
184 | ).listen(config.ssl.port, config.ssl.address, () => { |
181 | ).listen(config.ssl.port, config.ssl.address, () => { |
|
185 | log.info('HTTPs Server accessible at: https://' + |
182 | log.info('HTTPs Server accessible at: https://' + |
|
186 | config.ssl.address + |
183 | config.ssl.address + |
|
187 | ':' + |
184 | ':' + |
|
188 | config.ssl.port + |
185 | config.ssl.port + |
|
189 | ' and serving files from directory: ' + |
186 | ' and serving files from directory: ' + |
|
190 | root |
187 | root |
|
191 | ); |
188 | ) |
|
192 | }) |
189 | }) |
|
193 | } |
190 | } |
|
194 | ); |
191 | ) |
|
195 | } |
192 | } |
|
196 | }); |
193 | }) |