refactor: Extract createHttpServer Function and Replace Temp with Query

Signed-off-by: BoHong Li <a60814billy@gmail.com>
This commit is contained in:
BoHong Li 2019-04-20 01:30:26 +08:00
parent 8f9cfeec77
commit 0bed306ba9
No known key found for this signature in database
GPG Key ID: 9696D5590D58290F
1 changed files with 25 additions and 22 deletions

47
app.js
View File

@ -26,30 +26,33 @@ var response = require('./lib/response')
var models = require('./lib/models') var models = require('./lib/models')
var csp = require('./lib/csp') var csp = require('./lib/csp')
function createHttpServer () {
if (config.useSSL) {
const ca = (function () {
let i, len, results
results = []
for (i = 0, len = config.sslCAPath.length; i < len; i++) {
results.push(fs.readFileSync(config.sslCAPath[i], 'utf8'))
}
return results
})()
const options = {
key: fs.readFileSync(config.sslKeyPath, 'utf8'),
cert: fs.readFileSync(config.sslCertPath, 'utf8'),
ca: ca,
dhparam: fs.readFileSync(config.dhParamPath, 'utf8'),
requestCert: false,
rejectUnauthorized: false
}
return require('https').createServer(options, app)
} else {
return require('http').createServer(app)
}
}
// server setup // server setup
var app = express() var app = express()
var server = null var server = createHttpServer()
if (config.useSSL) {
var ca = (function () {
var i, len, results
results = []
for (i = 0, len = config.sslCAPath.length; i < len; i++) {
results.push(fs.readFileSync(config.sslCAPath[i], 'utf8'))
}
return results
})()
var options = {
key: fs.readFileSync(config.sslKeyPath, 'utf8'),
cert: fs.readFileSync(config.sslCertPath, 'utf8'),
ca: ca,
dhparam: fs.readFileSync(config.dhParamPath, 'utf8'),
requestCert: false,
rejectUnauthorized: false
}
server = require('https').createServer(options, app)
} else {
server = require('http').createServer(app)
}
// logger // logger
app.use(morgan('combined', { app.use(morgan('combined', {