/server.js |
@@ -5,43 +5,44 @@ |
/*************************************************************************/ |
|
// Import packages. |
const https = require('https'); |
const http = require('http'); |
const path = require('path'); |
const fs = require('fs'); |
const url = require('url'); |
const moment = require('moment'); |
const winston = require('winston'); |
const yargs = require('yargs'); |
const dns = require('dns'); |
const HttpCache = require("http-cache"); |
const https = require('https') |
const http = require('http') |
const path = require('path') |
const fs = require('fs') |
const url = require('url') |
const moment = require('moment') |
const winston = require('winston') |
const yargs = require('yargs') |
const dns = require('dns') |
const HttpCache = require("http-cache") |
|
// Local imports. |
const Handler = require( |
path |
.resolve( |
path.dirname(require.main.filename), |
'src', |
'handler' |
) |
); |
.resolve( |
path.dirname(require.main.filename), |
'src', |
'handler' |
) |
) |
|
const certs = require( |
path |
.resolve( |
path.dirname(require.main.filename), |
'src', |
'certs' |
) |
); |
.resolve( |
path.dirname(require.main.filename), |
'src', |
'certs' |
) |
) |
|
// Load configuration file. |
const config = require( |
path |
.resolve( |
path.dirname(require.main.filename), |
'config' |
) |
); |
.resolve( |
path.dirname(require.main.filename), |
'config' |
) |
) |
|
// Get command-line arguments. |
const argv = yargs |
@@ -56,7 +57,7 @@ |
|
// Create various logging mechanisms. |
// RFC5424 - { emerg: 0, alert: 1, crit: 2, error: 3, warning: 4, notice: 5, info: 6, debug: 7 } |
winston.setLevels(winston.config.syslog.levels); |
winston.setLevels(winston.config.syslog.levels) |
const log = new winston.Logger({ |
transports: [ |
new winston.transports.File({ |
@@ -83,16 +84,16 @@ |
}) |
], |
exitOnError: false |
}); |
}) |
|
fs.realpath(argv.root, (error, root) => { |
if (error) { |
log.error('Could not find document root: ' + argv.root); |
process.exit(1); |
log.error('Could not find document root: ' + argv.root) |
process.exit(1) |
} |
|
// Create server-side cache. |
const httpcache = new HttpCache(); |
const httpcache = new HttpCache() |
|
// Start HTTP server. |
http.createServer( |
@@ -99,33 +100,31 @@ |
// authentication, |
(request, response) => { |
// Configuration path requested, so send the server configuration if allowed. |
if(config.configuration.enable === true && |
url.parse(request.url, true).path === |
if (config.configuration.enable === true && |
url.parse(request.url, true).path === |
config.configuration.path) { |
const address = request.socket.address(); |
const address = request.socket.address() |
log.info('HTTP Server configuration requested by: ' + |
address.address + ':' + |
address.port |
); |
response.setHeader('Content-Type', 'application/json'); |
response.end(JSON.stringify(config)); |
return; |
) |
response.setHeader('Content-Type', 'application/json') |
response.end(JSON.stringify(config)) |
return |
} |
|
|
// Process and cache the resource. |
httpcache(request, response, () => { |
new Handler().process(config, request, response, root) |
.on('log', (data) => { |
log.log(data.severity, data.message); |
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()); |
}); |
}); |
response.setHeader('Content-Type', result.type) |
response.writeHead(result.status) |
result.data.pipe(response) |
}) |
}) |
} |
).listen(config.net.port, config.net.address, () => { |
log.info('HTTP Server accessible at: http://' + |
@@ -134,8 +133,8 @@ |
config.net.port + |
' and serving files from directory: ' + |
root |
); |
}); |
) |
}) |
|
// Start HTTPs server if enabled. |
if (config.ssl.enable) { |
@@ -152,34 +151,32 @@ |
cert: certificates.certificate, |
}, |
(request, response) => { |
|
|
// Configuration path requested, so send the server configuration if allowed. |
if(config.configuration.enable === true && |
url.parse(request.url, true).path === |
if (config.configuration.enable === true && |
url.parse(request.url, true).path === |
config.configuration.path) { |
const address = request.socket.address(); |
const address = request.socket.address() |
log.info('HTTP Server configuration requested by: ' + |
address.address + ':' + |
address.port |
); |
response.setHeader('Content-Type', 'application/json'); |
response.end(JSON.stringify(config)); |
return; |
) |
response.setHeader('Content-Type', 'application/json') |
response.end(JSON.stringify(config)) |
return |
} |
|
|
httpcache(request, response, () => { |
new Handler().process(config, request, response, root) |
.on('log', (data) => { |
log.log(data.severity, data.message); |
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()); |
}); |
}); |
response.setHeader('Content-Type', result.type) |
response.writeHead(result.status) |
result.data.pipe(response) |
}) |
}) |
} |
).listen(config.ssl.port, config.ssl.address, () => { |
log.info('HTTPs Server accessible at: https://' + |
@@ -188,9 +185,9 @@ |
config.ssl.port + |
' and serving files from directory: ' + |
root |
); |
) |
}) |
} |
); |
) |
} |
}); |
}) |