/server.js |
@@ -14,30 +14,22 @@ |
const winston = require('winston'); |
const yargs = require('yargs'); |
const dns = require('dns'); |
const was = require("was.js"); |
const HttpCache = require("http-cache"); |
|
// Local imports. |
const GET = require( |
const Handler = require( |
path |
.resolve( |
path.dirname(require.main.filename), |
'src/methods', |
'get' |
'src', |
'handler' |
) |
); |
const POST = require( |
path |
.resolve( |
path.dirname(require.main.filename), |
'src/methods', |
'post' |
) |
); |
const certs = require( |
path |
.resolve( |
path.dirname(require.main.filename), |
'src/server', |
'src', |
'certs' |
) |
); |
@@ -99,17 +91,18 @@ |
process.exit(1); |
} |
|
// Create server-side cache. |
const httpcache = new HttpCache(); |
|
// Start HTTP server. |
http.createServer( |
// authentication, |
(request, response) => { |
// Grab connecting address. |
const address = request.socket.address(); |
|
// Configuration path requested, so send the server configuration if allowed. |
if(config.configuration.enable === true && |
url.parse(request.url, true).path === |
config.configuration.path) { |
const address = request.socket.address(); |
log.info('HTTP Server configuration requested by: ' + |
address.address + ':' + |
address.port |
@@ -119,46 +112,20 @@ |
return; |
} |
|
// Switch on HTTP method. |
was.lambda.switch( |
request.method, |
(o) => { |
log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
address.address + ':' + |
address.port |
); |
}, |
(o) => o === 'GET', |
(o) => { |
// Send the resource. |
new GET().process(config, request, response, root) |
.on('log', (data) => { |
log.log(data.severity, data.message); |
}) |
.on('data', (result) => { |
response.writeHead(result.status); |
result.data |
.on('readable', () => result.data.pipe(response)) |
.on('end', () => response.end()); |
}); |
return true; |
}, |
(o) => o === 'POST', |
(o) => { |
new POST().process(config, request, response, root) |
.on('log', (data) => { |
log.log(data.severity, data.message); |
}) |
.on('data', (result) => { |
response.setHeader('Content-Type', result.type); |
response.writeHead(result.status); |
result.data |
.on('readable', () => result.data.pipe(response)) |
.on('end', () => response.end()); |
}); |
return true; |
} |
); |
// Process and cache the resource. |
httpcache(request, response, () => { |
new Handler().process(config, request, response, root) |
.on('log', (data) => { |
log.log(data.severity, data.message); |
}) |
.on('data', (result) => { |
response.setHeader('Content-Type', result.type); |
response.writeHead(result.status); |
result.data |
.on('readable', () => result.data.pipe(response)) |
.on('end', () => response.end()); |
}); |
}); |
} |
).listen(config.net.port, config.net.address, () => { |
log.info('HTTP Server accessible at: http://' + |
@@ -185,13 +152,12 @@ |
cert: certificates.certificate, |
}, |
(request, response) => { |
// Grab connecting address. |
const address = request.socket.address(); |
|
// Configuration path requested, so send the server configuration if allowed. |
if(config.configuration.enable === true && |
url.parse(request.url, true).path === |
config.configuration.path) { |
const address = request.socket.address(); |
log.info('HTTP Server configuration requested by: ' + |
address.address + ':' + |
address.port |
@@ -201,47 +167,19 @@ |
return; |
} |
|
// Switch on HTTP method. |
was.lambda.switch( |
request.method, |
(o) => { |
log.info('Unsupported HTTP \'' + request.method + '\' method requested by: ' + |
address.address + ':' + |
address.port |
); |
}, |
(o) => o === 'GET', |
(o) => { |
// Send the resource. |
new GET().process(config, request, response, root) |
.on('log', (data) => { |
log.log(data.severity, data.message); |
}) |
.on('data', (result) => { |
response.setHeader('Content-Type', result.type); |
response.writeHead(result.status); |
result.data |
.on('readable', () => result.data.pipe(response)) |
.on('end', () => response.end()); |
}); |
return true; |
}, |
(o) => o === 'POST', |
(o) => { |
new POST().process(config, request, response, root) |
.on('log', (data) => { |
log.log(data.severity, data.message); |
}) |
.on('data', (result) => { |
response.setHeader('Content-Type', result.type); |
response.writeHead(result.status); |
result.data |
.on('readable', () => result.data.pipe(response)) |
.on('end', () => response.end()); |
}); |
return true; |
} |
); |
httpcache(request, response, () => { |
new Handler().process(config, request, response, root) |
.on('log', (data) => { |
log.log(data.severity, data.message); |
}) |
.on('data', (result) => { |
response.setHeader('Content-Type', result.type); |
response.writeHead(result.status); |
result.data |
.on('readable', () => result.data.pipe(response)) |
.on('end', () => response.end()); |
}); |
}); |
} |
).listen(config.ssl.port, config.ssl.address, () => { |
log.info('HTTPs Server accessible at: https://' + |