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 join = require('path').join
var defaultCommand = 'dev' var defaultCommand = 'dev'
var commands = new Set([ var commands = []
defaultCommand, try {
'init', commands = require('fs').readdirSync(__dirname)
'build', } catch (e) {
'start', commands = []
'generate' }
]) commands.push(defaultCommand, 'init')
commands = new Set(commands)
var cmd = process.argv[2] var cmd = process.argv[2]
if (commands.has(cmd)) { if (commands.has('nuxt-' + cmd)) {
process.argv.splice(2, 1) process.argv.splice(2, 1)
} else { } else {
cmd = defaultCommand 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 Nuxt = require('../')
var chokidar = require('chokidar') var chokidar = require('chokidar')
var resolve = require('path').resolve var resolve = require('path').resolve
var server = module.exports.server = require('http').createServer()
var rootDir = resolve(process.argv.slice(2)[0] || '.') var rootDir = resolve(process.argv.slice(2)[0] || '.')
var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js') var nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
var options = {} var options = {}
if (fs.existsSync(nuxtConfigFile)) { if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile) options = require(nuxtConfigFile)
module.exports.nuxtConfigFile = nuxtConfigFile
} }
if (typeof options.rootDir !== 'string') { if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir options.rootDir = rootDir
@ -26,16 +27,18 @@ options.dev = true // Add hot reloading and watching changes
var nuxt = new Nuxt(options) var nuxt = new Nuxt(options)
nuxt.build() nuxt.build()
.then(() => { .then(() => {
var server = new nuxt.Server(nuxt) var nuxtServer = new nuxt.Server(nuxt, server)
.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host) if (!process.env.NOTLISTEN) {
listenOnConfigChanges(nuxt, server) 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) => { .catch((err) => {
console.error(err) // eslint-disable-line no-console console.error(err) // eslint-disable-line no-console
process.exit(1) process.exit(1)
}) })
function listenOnConfigChanges (nuxt, server) { function listenOnConfigChanges (nuxt, nuxtServer) {
// Listen on nuxt.config.js changes // Listen on nuxt.config.js changes
var build = _.debounce(() => { var build = _.debounce(() => {
debug('[nuxt.config.js] changed') debug('[nuxt.config.js] changed')
@ -56,7 +59,7 @@ function listenOnConfigChanges (nuxt, server) {
return new Nuxt(options).build() return new Nuxt(options).build()
}) })
.then((nuxt) => { .then((nuxt) => {
server.nuxt = nuxt nuxtServer.nuxt = nuxt
}) })
.catch((error) => { .catch((error) => {
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console 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) var nuxt = new Nuxt(options)
new nuxt.Server(nuxt) var nuxtServer = new nuxt.Server(nuxt)
.listen( module.exports.server = nuxtServer.server
process.env.PORT || process.env.npm_package_config_nuxt_port, module.exports.nuxtConfigFile = nuxtConfigFile
process.env.HOST || process.env.npm_package_config_nuxt_host
) 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' 'use strict'
const http = require('http') const http = require('http')
const https = require('https')
class Server { class Server {
constructor (nuxt, server, isUserListen) {
constructor (nuxt) {
this.nuxt = nuxt 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 return this
} }
@ -16,11 +23,16 @@ class Server {
} }
listen (port, host) { listen (port, host) {
host = host || 'localhost' if (this.isUserListen !== true) {
port = port || 3000 host = host || 'localhost'
this.server.listen(port, host, () => { port = port || 3000
console.log('Ready on http://%s:%s', host, port) // eslint-disable-line no-console 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 return this
} }

View File

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