fluffy – Diff between revs 2 and 3
?pathlinks?
Rev 2 | Rev 3 | |||
---|---|---|---|---|
Line 12... | Line 12... | |||
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 HttpCache = require("http-cache"); |
17 | const was = require("was.js"); |
|
Line 18... | Line 18... | |||
18 | |
18 | |
|
19 | // Local imports. |
19 | // Local imports. |
|
20 | const Handler = 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', |
24 | 'src', |
|
25 | 'handler' |
25 | 'get' |
|
26 | ) |
26 | ) |
|
27 | ); |
27 | ); |
|
28 | const certs = require( |
28 | const certs = require( |
|
29 | path |
29 | path |
|
Line 89... | Line 89... | |||
89 | if (error) { |
89 | if (error) { |
|
90 | log.error('Could not find document root: ' + argv.root); |
90 | log.error('Could not find document root: ' + argv.root); |
|
91 | process.exit(1); |
91 | process.exit(1); |
|
92 | } |
92 | } |
|
Line 93... | Line -... | |||
93 | |
- | ||
94 | // Create server-side cache. |
- | ||
95 | const httpcache = new HttpCache(); |
- | ||
96 | |
93 | |
|
97 | // Start HTTP server. |
94 | // Start HTTP server. |
|
98 | http.createServer( |
95 | http.createServer( |
|
99 | // authentication, |
96 | // authentication, |
|
- | 97 | (request, response) => { |
||
- | 98 | // Grab connecting address. |
||
- | 99 | const address = request.socket.address(); |
||
100 | (request, response) => { |
100 | |
|
101 | // Configuration path requested, so send the server configuration if allowed. |
101 | // Configuration path requested, so send the server configuration if allowed. |
|
102 | if(config.configuration.enable === true && |
102 | if(config.configuration.enable === true && |
|
103 | url.parse(request.url, true).path === |
103 | url.parse(request.url, true).path === |
|
104 | config.configuration.path) { |
- | ||
105 | const address = request.socket.address(); |
104 | config.configuration.path) { |
|
106 | log.info('HTTP Server configuration requested by: ' + |
105 | log.info('HTTP Server configuration requested by: ' + |
|
107 | address.address + ':' + |
106 | address.address + ':' + |
|
108 | address.port |
107 | address.port |
|
109 | ); |
108 | ); |
|
110 | response.setHeader('Content-Type', 'application/json'); |
109 | response.setHeader('Content-Type', 'application/json'); |
|
111 | response.end(JSON.stringify(config)); |
110 | response.end(JSON.stringify(config)); |
|
112 | return; |
111 | return; |
|
Line 113... | Line 112... | |||
113 | } |
112 | } |
|
- | 113 | |
||
114 | |
114 | // Switch on HTTP method. |
|
- | 115 | was.lambda.switch( |
||
- | 116 | request.method, |
||
- | 117 | (o) => { |
||
- | 118 | log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
||
- | 119 | address.address + ':' + |
||
- | 120 | address.port |
||
- | 121 | ); |
||
- | 122 | }, |
||
- | 123 | (o) => o === 'GET', |
||
115 | // Process and cache the resource. |
124 | (o) => { |
|
116 | httpcache(request, response, () => { |
125 | // Send the resource. |
|
117 | new Handler().process(config, request, response, root) |
126 | new GET().process(config, request, response, root) |
|
118 | .on('log', (data) => { |
127 | .on('log', (data) => { |
|
119 | log.log(data.severity, data.message); |
128 | log.log(data.severity, data.message); |
|
120 | }) |
- | ||
121 | .on('data', (result) => { |
129 | }) |
|
122 | response.setHeader('Content-Type', result.type); |
130 | .on('data', (result) => { |
|
123 | response.writeHead(result.status); |
131 | response.writeHead(result.status); |
|
124 | result.data |
132 | result.data |
|
125 | .on('readable', () => result.data.pipe(response)) |
133 | .on('readable', () => result.data.pipe(response)) |
|
- | 134 | .on('end', () => response.end()); |
||
- | 135 | }); |
||
- | 136 | return true; |
||
- | 137 | }, |
||
- | 138 | (o) => o === 'PUT', |
||
- | 139 | (o) => { |
||
- | 140 | // TODO: implement PUT |
||
126 | .on('end', () => response.end()); |
141 | return true; |
|
127 | }); |
142 | } |
|
128 | }); |
143 | ); |
|
129 | } |
144 | } |
|
130 | ).listen(config.net.port, config.net.address, () => { |
145 | ).listen(config.net.port, config.net.address, () => { |
|
131 | log.info('HTTP Server accessible at: http://' + |
146 | log.info('HTTP Server accessible at: http://' + |
|
Line 150... | Line 165... | |||
150 | { |
165 | { |
|
151 | key: certificates.privateKey, |
166 | key: certificates.privateKey, |
|
152 | cert: certificates.certificate, |
167 | cert: certificates.certificate, |
|
153 | }, |
168 | }, |
|
154 | (request, response) => { |
169 | (request, response) => { |
|
- | 170 | // Grab connecting address. |
||
- | 171 | const address = request.socket.address(); |
||
- | 172 | |
||
155 | // Configuration path requested, so send the server configuration if allowed. |
173 | // Configuration path requested, so send the server configuration if allowed. |
|
156 | if(config.configuration.enable === true && |
174 | if(config.configuration.enable === true && |
|
157 | url.parse(request.url, true).path === |
175 | url.parse(request.url, true).path === |
|
158 | config.configuration.path) { |
176 | config.configuration.path) { |
|
159 | const address = request.socket.address(); |
- | ||
160 | log.info('HTTP Server configuration requested by: ' + |
177 | log.info('HTTP Server configuration requested by: ' + |
|
161 | address.address + ':' + |
178 | address.address + ':' + |
|
162 | address.port |
179 | address.port |
|
163 | ); |
180 | ); |
|
164 | response.setHeader('Content-Type', 'application/json'); |
181 | response.setHeader('Content-Type', 'application/json'); |
|
165 | response.end(JSON.stringify(config)); |
182 | response.end(JSON.stringify(config)); |
|
166 | return; |
183 | return; |
|
167 | } |
184 | } |
|
Line 168... | Line 185... | |||
168 | |
185 | |
|
- | 186 | // Switch on HTTP method. |
||
169 | // Process and cache the resource. |
187 | was.lambda.switch( |
|
- | 188 | request.method, |
||
- | 189 | (o) => { |
||
- | 190 | log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
||
- | 191 | address.address + ':' + |
||
- | 192 | address.port |
||
- | 193 | ); |
||
- | 194 | }, |
||
- | 195 | (o) => o === 'GET', |
||
- | 196 | (o) => { |
||
170 | httpcache(request, response, () => { |
197 | // Send the resource. |
|
171 | new Handler().process(config, request, response, root) |
198 | new GET().process(config, request, response, root) |
|
172 | .on('log', (data) => { |
199 | .on('log', (data) => { |
|
173 | log.log(data.severity, data.message); |
200 | log.log(data.severity, data.message); |
|
174 | }) |
201 | }) |
|
175 | .on('data', (result) => { |
202 | .on('data', (result) => { |
|
176 | response.setHeader('Content-Type', result.type); |
203 | response.setHeader('Content-Type', result.type); |
|
177 | response.writeHead(result.status); |
204 | response.writeHead(result.status); |
|
178 | result.data |
205 | result.data |
|
179 | .on('readable', () => result.data.pipe(response)) |
206 | .on('readable', () => result.data.pipe(response)) |
|
180 | .on('end', () => response.end()); |
207 | .on('end', () => response.end()); |
|
- | 208 | }); |
||
- | 209 | return true; |
||
- | 210 | }, |
||
- | 211 | (o) => o === 'PUT', |
||
- | 212 | (o) => { |
||
- | 213 | // TODO: implement PUT |
||
- | 214 | return true; |
||
181 | }); |
215 | } |
|
182 | }); |
216 | ); |
|
183 | } |
217 | } |
|
184 | ).listen(config.ssl.port, config.ssl.address, () => { |
218 | ).listen(config.ssl.port, config.ssl.address, () => { |
|
185 | log.info('HTTPs Server accessible at: https://' + |
219 | log.info('HTTPs Server accessible at: https://' + |
|
186 | config.ssl.address + |
220 | config.ssl.address + |