Add ddv virtual host management worker support

Add ddv virtual host management worker support
This commit is contained in:
yuchonghua 2017-01-24 11:30:52 +08:00
parent e9740e4e27
commit 0a2abc460b
7 changed files with 106 additions and 28 deletions

View File

@ -3,17 +3,18 @@
var join = require('path').join
var defaultCommand = 'dev'
var commands = new Set([
defaultCommand,
'init',
'build',
'start',
'generate'
])
var commands = []
try {
commands = require('fs').readdirSync(__dirname)
} catch (e) {
commands = []
}
commands.push(defaultCommand, 'init')
commands = new Set(commands)
var cmd = process.argv[2]
if (commands.has(cmd)) {
if (commands.has('nuxt-' + cmd)) {
process.argv.splice(2, 1)
} else {
cmd = defaultCommand

28
bin/nuxt-ddvdev Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env node
process.env.NOTLISTEN = true
const fs = require('fs')
const worker = require('ddv-worker')
const nuxtDev = require('./nuxt-dev')
worker.server = nuxtDev.server
var options = {}
if (fs.existsSync(nuxtDev.nuxtConfigFile)) {
options = require(nuxtDev.nuxtConfigFile)
}
worker.DEBUG = true // Force production mode (no webpack middlewares called)
worker.updateServerConf({
defaultListen: options.defaultListen,
listen: options.listen,
cpuLen: options.cpuLen
}, (e) => {
if (e) {
console.error('service config update fail') // eslint-disable-line no-console
console.error(e) // eslint-disable-line no-console
} else {
console.log('service config update successful') // eslint-disable-line no-console
}
})

28
bin/nuxt-ddvstart Normal file
View File

@ -0,0 +1,28 @@
#!/usr/bin/env node
process.env.NOTLISTEN = true
const fs = require('fs')
const worker = require('ddv-worker')
const nuxtStart = require('./nuxt-start')
worker.server = nuxtStart.server
var options = {}
if (fs.existsSync(nuxtStart.nuxtConfigFile)) {
options = require(nuxtStart.nuxtConfigFile)
}
worker.DEBUG = false // Force production mode (no webpack middlewares called)
worker.updateServerConf({
defaultListen: options.defaultListen,
listen: options.listen,
cpuLen: options.cpuLen
}, (e) => {
if (e) {
console.error('service config update fail') // eslint-disable-line no-console
console.error(e) // eslint-disable-line no-console
} else {
console.log('service config update successful') // eslint-disable-line no-console
}
})

View File

@ -10,13 +10,14 @@ var fs = require('fs')
var Nuxt = require('../')
var chokidar = require('chokidar')
var resolve = require('path').resolve
var server = module.exports.server = require('http').createServer()
var rootDir = resolve(process.argv.slice(2)[0] || '.')
var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
var options = {}
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
module.exports.nuxtConfigFile = nuxtConfigFile
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
@ -26,16 +27,18 @@ options.dev = true // Add hot reloading and watching changes
var nuxt = new Nuxt(options)
nuxt.build()
.then(() => {
var server = new nuxt.Server(nuxt)
.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host)
listenOnConfigChanges(nuxt, server)
var nuxtServer = new nuxt.Server(nuxt, server)
if (!process.env.NOTLISTEN) {
nuxtServer.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host)
}
listenOnConfigChanges(nuxt, nuxtServer)
})
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
function listenOnConfigChanges (nuxt, server) {
function listenOnConfigChanges (nuxt, nuxtServer) {
// Listen on nuxt.config.js changes
var build = _.debounce(() => {
debug('[nuxt.config.js] changed')
@ -56,7 +59,7 @@ function listenOnConfigChanges (nuxt, server) {
return new Nuxt(options).build()
})
.then((nuxt) => {
server.nuxt = nuxt
nuxtServer.nuxt = nuxt
})
.catch((error) => {
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console

View File

@ -18,8 +18,13 @@ options.dev = false // Force production mode (no webpack middlewares called)
var nuxt = new Nuxt(options)
new nuxt.Server(nuxt)
.listen(
process.env.PORT || process.env.npm_package_config_nuxt_port,
process.env.HOST || process.env.npm_package_config_nuxt_host
)
var nuxtServer = new nuxt.Server(nuxt)
module.exports.server = nuxtServer.server
module.exports.nuxtConfigFile = nuxtConfigFile
if (!process.env.NOTLISTEN) {
nuxtServer.listen(
process.env.PORT || process.env.npm_package_config_nuxt_port,
process.env.HOST || process.env.npm_package_config_nuxt_host
)
}

View File

@ -1,12 +1,19 @@
'use strict'
const http = require('http')
const https = require('https')
class Server {
constructor (nuxt) {
constructor (nuxt, server, isUserListen) {
this.nuxt = nuxt
this.server = http.createServer(this.render.bind(this))
if (server instanceof http.Server && server instanceof https.Server) {
this.isUserListen = isUserListen === void 0 ? true : isUserListen
this.server = server
} else {
this.isUserListen = false
this.server = http.createServer()
}
this.server.on('request', this.render.bind(this))
return this
}
@ -16,11 +23,16 @@ class Server {
}
listen (port, host) {
host = host || 'localhost'
port = port || 3000
this.server.listen(port, host, () => {
console.log('Ready on http://%s:%s', host, port) // eslint-disable-line no-console
})
if (this.isUserListen !== true) {
host = host || 'localhost'
port = port || 3000
this.server.listen(port, host, () => {
console.log('Ready on http://%s:%s', host, port) // eslint-disable-line no-console
})
} else {
console.log('Please use the incoming server to establish listen') // eslint-disable-line no-console
}
return this
}

View File

@ -82,7 +82,8 @@
"webpack": "^2.2.0",
"webpack-bundle-analyzer": "^2.2.1",
"webpack-dev-middleware": "^1.9.0",
"webpack-hot-middleware": "^2.15.0"
"webpack-hot-middleware": "^2.15.0",
"ddv-worker": "latest"
},
"devDependencies": {
"ava": "^0.17.0",