This commit is contained in:
Lloyd Brookes
2016-06-15 21:02:52 +01:00
parent 88e441dba4
commit 789b82160d
4 changed files with 57 additions and 69 deletions

View File

@ -8,57 +8,47 @@ const Tool = require('command-line-tool')
const tool = new Tool()
class Cli {
constructor (options) {
this.options = null
constructor () {
this.options = collectOptions()
this.app = null
this.middleware = null
options = collectOptions()
this.options = options
const options = this.options
if (options.misc.config) {
tool.stop(JSON.stringify(options.server, null, ' '), 0)
} else {
const Koa = require('koa')
const app = new Koa()
this.app = app
const MiddlewareStack = require('./middleware-stack')
this.middleware = new MiddlewareStack(options)
const a = {
compress: options.server.compress,
mime: options.server.mime,
forbid: options.server.forbid,
spa: options.server.spa,
'no-cache': options.server['no-cache'],
rewrite: options.server.rewrite,
verbose: options.server.verbose,
mocks: options.server.mocks
}
app.on('error', err => {
if (options.server['log-format']) {
console.error(ansi.format(err.message, 'red'))
}
})
this.app = app
const MiddlewareStack = require('./middleware-stack')
this.middleware = new MiddlewareStack(options)
}
}
listen () {
this.app.use(this.middleware.getMiddleware())
this.app.use(this.middleware.compose())
const options = this.options
const key = this.options.server.key
const cert = this.options.server.cert
if (options.server.https) {
options.server.key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
options.server.cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
}
if (options.server.key && options.server.cert) {
if (key && cert) {
const https = require('https')
const fs = require('fs')
const serverOptions = {
key: fs.readFileSync(options.server.key),
cert: fs.readFileSync(options.server.cert)
key: fs.readFileSync(key),
cert: fs.readFileSync(cert)
}
const server = https.createServer(serverOptions, this.app.callback())