fluffy – Diff between revs 5 and 6
?pathlinks?
Rev 5 | Rev 6 | |||
---|---|---|---|---|
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 | const was = require("was.js"); |
17 | const was = require("was.js"); |
|
18 | |
18 | |
|
19 | // Local imports. |
19 | // Local imports. |
|
20 | const GET = require( |
20 | const GET = require( |
|
21 | path |
21 | path |
|
22 | .resolve( |
22 | .resolve( |
|
23 | path.dirname(require.main.filename), |
23 | path.dirname(require.main.filename), |
|
24 | 'src/methods', |
24 | 'src/methods', |
|
25 | 'get' |
25 | 'get' |
|
26 | ) |
26 | ) |
|
27 | ); |
27 | ); |
|
- | 28 | const POST = require( |
||
- | 29 | path |
||
- | 30 | .resolve( |
||
- | 31 | path.dirname(require.main.filename), |
||
- | 32 | 'src/methods', |
||
- | 33 | 'post' |
||
- | 34 | ) |
||
- | 35 | ); |
||
28 | const certs = require( |
36 | const certs = require( |
|
29 | path |
37 | path |
|
30 | .resolve( |
38 | .resolve( |
|
31 | path.dirname(require.main.filename), |
39 | path.dirname(require.main.filename), |
|
32 | 'src/server', |
40 | 'src/server', |
|
33 | 'certs' |
41 | 'certs' |
|
34 | ) |
42 | ) |
|
35 | ); |
43 | ); |
|
36 | |
44 | |
|
37 | // Load configuration file. |
45 | // Load configuration file. |
|
38 | const config = require( |
46 | const config = require( |
|
39 | path |
47 | path |
|
40 | .resolve( |
48 | .resolve( |
|
41 | path.dirname(require.main.filename), |
49 | path.dirname(require.main.filename), |
|
42 | 'config' |
50 | 'config' |
|
43 | ) |
51 | ) |
|
44 | ); |
52 | ); |
|
45 | |
53 | |
|
46 | // Get command-line arguments. |
54 | // Get command-line arguments. |
|
47 | const argv = yargs |
55 | const argv = yargs |
|
48 | .version() |
56 | .version() |
|
49 | .option('root', { |
57 | .option('root', { |
|
50 | alias: 'd', |
58 | alias: 'd', |
|
51 | describe: 'Path to the document root', |
59 | describe: 'Path to the document root', |
|
52 | demandOption: true |
60 | demandOption: true |
|
53 | }) |
61 | }) |
|
54 | .help() |
62 | .help() |
|
55 | .argv |
63 | .argv |
|
56 | |
64 | |
|
57 | // Create various logging mechanisms. |
65 | // Create various logging mechanisms. |
|
58 | // RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } |
66 | // RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } |
|
59 | winston.setLevels(winston.config.syslog.levels); |
67 | winston.setLevels(winston.config.syslog.levels); |
|
60 | const log = new winston.Logger({ |
68 | const log = new winston.Logger({ |
|
61 | transports: [ |
69 | transports: [ |
|
62 | new winston.transports.File({ |
70 | new winston.transports.File({ |
|
63 | level: 'info', |
71 | level: 'info', |
|
64 | filename: path.resolve( |
72 | filename: path.resolve( |
|
65 | path.dirname(require.main.filename), |
73 | path.dirname(require.main.filename), |
|
66 | config.log.file |
74 | config.log.file |
|
67 | ), |
75 | ), |
|
68 | handleExceptions: true, |
76 | handleExceptions: true, |
|
69 | json: false, |
77 | json: false, |
|
70 | maxsize: 1048576, // 1MiB. |
78 | maxsize: 1048576, // 1MiB. |
|
71 | maxFiles: 10, // Ten rotations. |
79 | maxFiles: 10, // Ten rotations. |
|
72 | colorize: false, |
80 | colorize: false, |
|
73 | timestamp: () => moment() |
81 | timestamp: () => moment() |
|
74 | .format('YYYYMMDDTHHmmss') |
82 | .format('YYYYMMDDTHHmmss') |
|
75 | }), |
83 | }), |
|
76 | new winston.transports.Console({ |
84 | new winston.transports.Console({ |
|
77 | level: 'info', |
85 | level: 'info', |
|
78 | handleExceptions: true, |
86 | handleExceptions: true, |
|
79 | json: false, |
87 | json: false, |
|
80 | colorize: true, |
88 | colorize: true, |
|
81 | timestamp: () => moment() |
89 | timestamp: () => moment() |
|
82 | .format('YYYYMMDDTHHmmss') |
90 | .format('YYYYMMDDTHHmmss') |
|
83 | }) |
91 | }) |
|
84 | ], |
92 | ], |
|
85 | exitOnError: false |
93 | exitOnError: false |
|
86 | }); |
94 | }); |
|
87 | |
95 | |
|
88 | fs.realpath(argv.root, (error, root) => { |
96 | fs.realpath(argv.root, (error, root) => { |
|
89 | if (error) { |
97 | if (error) { |
|
90 | log.error('Could not find document root: ' + argv.root); |
98 | log.error('Could not find document root: ' + argv.root); |
|
91 | process.exit(1); |
99 | process.exit(1); |
|
92 | } |
100 | } |
|
93 | |
101 | |
|
94 | // Start HTTP server. |
102 | // Start HTTP server. |
|
95 | http.createServer( |
103 | http.createServer( |
|
96 | // authentication, |
104 | // authentication, |
|
97 | (request, response) => { |
105 | (request, response) => { |
|
98 | // Grab connecting address. |
106 | // Grab connecting address. |
|
99 | const address = request.socket.address(); |
107 | const address = request.socket.address(); |
|
100 | |
108 | |
|
101 | // Configuration path requested, so send the server configuration if allowed. |
109 | // Configuration path requested, so send the server configuration if allowed. |
|
102 | if(config.configuration.enable === true && |
110 | if(config.configuration.enable === true && |
|
103 | url.parse(request.url, true).path === |
111 | url.parse(request.url, true).path === |
|
104 | config.configuration.path) { |
112 | config.configuration.path) { |
|
105 | log.info('HTTP Server configuration requested by: ' + |
113 | log.info('HTTP Server configuration requested by: ' + |
|
106 | address.address + ':' + |
114 | address.address + ':' + |
|
107 | address.port |
115 | address.port |
|
108 | ); |
116 | ); |
|
109 | response.setHeader('Content-Type', 'application/json'); |
117 | response.setHeader('Content-Type', 'application/json'); |
|
110 | response.end(JSON.stringify(config)); |
118 | response.end(JSON.stringify(config)); |
|
111 | return; |
119 | return; |
|
112 | } |
120 | } |
|
113 | |
121 | |
|
114 | // Switch on HTTP method. |
122 | // Switch on HTTP method. |
|
115 | was.lambda.switch( |
123 | was.lambda.switch( |
|
116 | request.method, |
124 | request.method, |
|
117 | (o) => { |
125 | (o) => { |
|
118 | log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
126 | log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
|
119 | address.address + ':' + |
127 | address.address + ':' + |
|
120 | address.port |
128 | address.port |
|
121 | ); |
129 | ); |
|
122 | }, |
130 | }, |
|
123 | (o) => o === 'GET', |
131 | (o) => o === 'GET', |
|
124 | (o) => { |
132 | (o) => { |
|
125 | // Send the resource. |
133 | // Send the resource. |
|
126 | new GET().process(config, request, response, root) |
134 | new GET().process(config, request, response, root) |
|
127 | .on('log', (data) => { |
135 | .on('log', (data) => { |
|
128 | log.log(data.severity, data.message); |
136 | log.log(data.severity, data.message); |
|
129 | }) |
137 | }) |
|
130 | .on('data', (result) => { |
138 | .on('data', (result) => { |
|
131 | response.writeHead(result.status); |
139 | response.writeHead(result.status); |
|
132 | result.data |
140 | result.data |
|
133 | .on('readable', () => result.data.pipe(response)) |
141 | .on('readable', () => result.data.pipe(response)) |
|
134 | .on('end', () => response.end()); |
142 | .on('end', () => response.end()); |
|
135 | }); |
143 | }); |
|
136 | return true; |
144 | return true; |
|
137 | }, |
145 | }, |
|
138 | (o) => o === 'PUT', |
146 | (o) => o === 'POST', |
|
139 | (o) => { |
147 | (o) => { |
|
- | 148 | new POST().process(config, request, response, root) |
||
- | 149 | .on('log', (data) => { |
||
- | 150 | log.log(data.severity, data.message); |
||
- | 151 | }) |
||
- | 152 | .on('data', (result) => { |
||
- | 153 | response.setHeader('Content-Type', result.type); |
||
- | 154 | response.writeHead(result.status); |
||
140 | // TODO: implement PUT |
155 | result.data |
|
- | 156 | .on('readable', () => result.data.pipe(response)) |
||
- | 157 | .on('end', () => response.end()); |
||
- | 158 | }); |
||
141 | return true; |
159 | return true; |
|
142 | } |
160 | } |
|
143 | ); |
161 | ); |
|
144 | } |
162 | } |
|
145 | ).listen(config.net.port, config.net.address, () => { |
163 | ).listen(config.net.port, config.net.address, () => { |
|
146 | log.info('HTTP Server accessible at: http://' + |
164 | log.info('HTTP Server accessible at: http://' + |
|
147 | config.net.address + |
165 | config.net.address + |
|
148 | ':' + |
166 | ':' + |
|
149 | config.net.port + |
167 | config.net.port + |
|
150 | ' and serving files from directory: ' + |
168 | ' and serving files from directory: ' + |
|
151 | root |
169 | root |
|
152 | ); |
170 | ); |
|
153 | }); |
171 | }); |
|
154 | |
172 | |
|
155 | // Start HTTPs server if enabled. |
173 | // Start HTTPs server if enabled. |
|
156 | if (config.ssl.enable) { |
174 | if (config.ssl.enable) { |
|
157 | // Generate certificates for HTTPs. |
175 | // Generate certificates for HTTPs. |
|
158 | certs.generate( |
176 | certs.generate( |
|
159 | config.site.name, |
177 | config.site.name, |
|
160 | config.net.address, |
178 | config.net.address, |
|
161 | config.ssl.privateKeySize, |
179 | config.ssl.privateKeySize, |
|
162 | (certificates) => { |
180 | (certificates) => { |
|
163 | https.createServer( |
181 | https.createServer( |
|
164 | // authentication, |
182 | // authentication, |
|
165 | { |
183 | { |
|
166 | key: certificates.privateKey, |
184 | key: certificates.privateKey, |
|
167 | cert: certificates.certificate, |
185 | cert: certificates.certificate, |
|
168 | }, |
186 | }, |
|
169 | (request, response) => { |
187 | (request, response) => { |
|
170 | // Grab connecting address. |
188 | // Grab connecting address. |
|
171 | const address = request.socket.address(); |
189 | const address = request.socket.address(); |
|
172 | |
190 | |
|
173 | // Configuration path requested, so send the server configuration if allowed. |
191 | // Configuration path requested, so send the server configuration if allowed. |
|
174 | if(config.configuration.enable === true && |
192 | if(config.configuration.enable === true && |
|
175 | url.parse(request.url, true).path === |
193 | url.parse(request.url, true).path === |
|
176 | config.configuration.path) { |
194 | config.configuration.path) { |
|
177 | log.info('HTTP Server configuration requested by: ' + |
195 | log.info('HTTP Server configuration requested by: ' + |
|
178 | address.address + ':' + |
196 | address.address + ':' + |
|
179 | address.port |
197 | address.port |
|
180 | ); |
198 | ); |
|
181 | response.setHeader('Content-Type', 'application/json'); |
199 | response.setHeader('Content-Type', 'application/json'); |
|
182 | response.end(JSON.stringify(config)); |
200 | response.end(JSON.stringify(config)); |
|
183 | return; |
201 | return; |
|
184 | } |
202 | } |
|
185 | |
203 | |
|
186 | // Switch on HTTP method. |
204 | // Switch on HTTP method. |
|
187 | was.lambda.switch( |
205 | was.lambda.switch( |
|
188 | request.method, |
206 | request.method, |
|
189 | (o) => { |
207 | (o) => { |
|
190 | log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
208 | log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
|
191 | address.address + ':' + |
209 | address.address + ':' + |
|
192 | address.port |
210 | address.port |
|
193 | ); |
211 | ); |
|
194 | }, |
212 | }, |
|
195 | (o) => o === 'GET', |
213 | (o) => o === 'GET', |
|
196 | (o) => { |
214 | (o) => { |
|
197 | // Send the resource. |
215 | // Send the resource. |
|
198 | new GET().process(config, request, response, root) |
216 | new GET().process(config, request, response, root) |
|
199 | .on('log', (data) => { |
217 | .on('log', (data) => { |
|
200 | log.log(data.severity, data.message); |
218 | log.log(data.severity, data.message); |
|
201 | }) |
219 | }) |
|
202 | .on('data', (result) => { |
220 | .on('data', (result) => { |
|
203 | response.setHeader('Content-Type', result.type); |
221 | response.setHeader('Content-Type', result.type); |
|
204 | response.writeHead(result.status); |
222 | response.writeHead(result.status); |
|
205 | result.data |
223 | result.data |
|
206 | .on('readable', () => result.data.pipe(response)) |
224 | .on('readable', () => result.data.pipe(response)) |
|
207 | .on('end', () => response.end()); |
225 | .on('end', () => response.end()); |
|
208 | }); |
226 | }); |
|
209 | return true; |
227 | return true; |
|
210 | }, |
228 | }, |
|
211 | (o) => o === 'PUT', |
229 | (o) => o === 'POST', |
|
212 | (o) => { |
230 | (o) => { |
|
- | 231 | new POST().process(config, request, response, root) |
||
- | 232 | .on('log', (data) => { |
||
- | 233 | log.log(data.severity, data.message); |
||
- | 234 | }) |
||
- | 235 | .on('data', (result) => { |
||
- | 236 | response.setHeader('Content-Type', result.type); |
||
- | 237 | response.writeHead(result.status); |
||
213 | // TODO: implement PUT |
238 | result.data |
|
- | 239 | .on('readable', () => result.data.pipe(response)) |
||
- | 240 | .on('end', () => response.end()); |
||
- | 241 | }); |
||
214 | return true; |
242 | return true; |
|
215 | } |
243 | } |
|
216 | ); |
244 | ); |
|
217 | } |
245 | } |
|
218 | ).listen(config.ssl.port, config.ssl.address, () => { |
246 | ).listen(config.ssl.port, config.ssl.address, () => { |
|
219 | log.info('HTTPs Server accessible at: https://' + |
247 | log.info('HTTPs Server accessible at: https://' + |
|
220 | config.ssl.address + |
248 | config.ssl.address + |
|
221 | ':' + |
249 | ':' + |
|
222 | config.ssl.port + |
250 | config.ssl.port + |
|
223 | ' and serving files from directory: ' + |
251 | ' and serving files from directory: ' + |
|
224 | root |
252 | root |
|
225 | ); |
253 | ); |
|
226 | }) |
254 | }) |
|
227 | } |
255 | } |
|
228 | ); |
256 | ); |
|
229 | } |
257 | } |
|
230 | }); |
258 | }); |
|
231 | |
259 | |