201 lines
7.6 KiB
JavaScript
201 lines
7.6 KiB
JavaScript
const fs = require('fs')
|
||
const path = require('path')
|
||
const nettool = require('sol.nettool')
|
||
const express = require('express')
|
||
|
||
const wo = (global.wo = {
|
||
envi : require('sol.enviconfig')({
|
||
commanderOptions: [
|
||
// 命令行里可以接受的参数。将传给 config.js 里的 commander。每个参数的定义格式是 [参数名,参数键,描述],后两者用于传给commander,取值后覆盖掉Config里的同名变量。
|
||
['protocol', '-P, --protocol <string>', 'Web Server protocol: http|https|httpall.'],
|
||
['host', '-H, --host <string>', 'Host IP or domain name, default to localhost.'],
|
||
['port', '-p, --port <number>', 'HTTP port number.'],
|
||
['from', '-f, --from <string>', 'Path to serve as website'],
|
||
['ssl', '--ssl <string>', 'SSL options in JSON string.'],
|
||
],
|
||
// 最基础的必须的默认配置,如果用户什么也没有提供
|
||
protocol: 'http',
|
||
host: 'localhost',
|
||
from: './dist', // local path to serve as webroot
|
||
// 如果使用虚拟主机
|
||
/*
|
||
vhosts: [
|
||
{ webroot: 'dist', webindex: 'index.html', domainList: ['']}
|
||
],
|
||
*/
|
||
})
|
||
})
|
||
if (typeof wo.envi.ssl === 'string') {
|
||
wo.envi.ssl = eval(`(${wo.envi.ssl})`)
|
||
}
|
||
|
||
;(function serve() {
|
||
console.log('★★★★★★★★ Starting Server ★★★★★★★★')
|
||
|
||
const server = express()
|
||
|
||
// const greenlock = require('greenlock-express').create({
|
||
// version: 'draft-11',
|
||
// server: 'https://acme-v02.api.letsencrypt.org/directory', // for test: acme-staging-v02
|
||
// agreeTos: true,
|
||
// communityMember: false,
|
||
// store: require('greenlock-store-fs'),
|
||
// email: 'ssl@faronear.org',
|
||
// approvedDomains: wo.wo.envi.sslDomains,
|
||
// configDir: path.resolve(__dirname, 'ssl'),
|
||
// app: server,
|
||
// })
|
||
|
||
/*** 通用中间件 ***/
|
||
server.use(require('morgan')('development' === wo.envi.prodev ? 'dev' : 'combined'))
|
||
server.use(require('body-parser').json())
|
||
server.use(require('body-parser').urlencoded({ extended: false }))
|
||
server.use(require('cookie-parser')())
|
||
server.use(require('compression')())
|
||
|
||
/*** 路由 ***/
|
||
// vhost 匹配了域名,就执行;不匹配,就next()
|
||
// express.static 找到了具体文件,就返回;找不到,就next()
|
||
// 所以,如果 vhost匹配了域名,且static找到了文件,就结束了。如果 vhost 匹配了域名,但static找不到文件,就继续往下。
|
||
if (!wo.envi.vhosts) {
|
||
server.use(
|
||
express.static(path.join(process.cwd(), wo.envi.from), {
|
||
index: 'index.html',
|
||
})
|
||
)
|
||
// server.use(require('serve-favicon')(path.join(process.cwd(), 'public', 'favicon.ico'))) // uncomment after placing your favicon in /public
|
||
} else {
|
||
let vhost = require('vhost')
|
||
for (let h of wo.envi.vhosts) {
|
||
for (let domain of h.domainList) {
|
||
server.use(
|
||
vhost(
|
||
domain,
|
||
express.static(path.join(process.cwd(), h.webroot), {
|
||
index: h.webindex,
|
||
})
|
||
)
|
||
)
|
||
}
|
||
}
|
||
}
|
||
|
||
/*** 路由 ***/
|
||
//var router = express.Router()
|
||
//router.get('/path', function(req,res) { res.redirect('target') })
|
||
//server.use(router)
|
||
|
||
/*** 启动 Web 服务 ***/
|
||
let webServer
|
||
let ipv4 = nettool.getMyIp()
|
||
let portHttp = wo.envi.port || 80
|
||
let portHttps = wo.envi.port || 443
|
||
if ('http' === wo.envi.protocol) {
|
||
webServer = require('http')
|
||
.createServer(server)
|
||
.listen(portHttp, function (err) {
|
||
if (err) console.log(err)
|
||
else
|
||
console.log(
|
||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
||
)
|
||
})
|
||
} else if ('https' === wo.envi.protocol) {
|
||
webServer = require('https')
|
||
.createServer(
|
||
// wo.wo.envi.ssl.type==='greenlock' ? greenlock.httpsOptions :
|
||
{
|
||
key: fs.readFileSync(wo.envi.ssl.file.key),
|
||
cert: fs.readFileSync(wo.envi.ssl.file.cert),
|
||
// ca: [ fs.readFileSync(wo.envi.ssl.file.ca) ] // only for self-signed certificate: https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
|
||
},
|
||
server
|
||
)
|
||
.listen(portHttps, function (err) {
|
||
if (err) console.log(err)
|
||
else
|
||
console.log(
|
||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
||
)
|
||
})
|
||
} else if ('httpall' === wo.envi.protocol) {
|
||
portHttp = 80
|
||
// if (wo.wo.envi.ssl.type==='greenlock') {
|
||
// greenlock.listen(portHttp, portHttps, function (err) {
|
||
// if (err) console.log(err)
|
||
// else console.log(`Server listening on [${wo.wo.envi.protocol}] http=>https://${wo.wo.envi.host}:${portHttp}=>${portHttps} for ${server.settings.env} environment`)
|
||
// })
|
||
// }else {
|
||
require('http')
|
||
.createServer(
|
||
server.all('*', function (ask, reply) {
|
||
reply.redirect(`https://${wo.envi.host}:${portHttps}`)
|
||
})
|
||
)
|
||
.listen(portHttp, function (err) {
|
||
if (err) console.log(err)
|
||
else
|
||
console.log(
|
||
`[${new Date().toJSON()}] Server redirecting from [${wo.envi.protocol}] http://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${
|
||
wo.envi.prodev
|
||
} environment`
|
||
)
|
||
})
|
||
webServer = require('https')
|
||
.createServer(
|
||
{
|
||
key: fs.readFileSync(wo.envi.ssl.file.key),
|
||
cert: fs.readFileSync(wo.envi.ssl.file.cert),
|
||
// ca: [ fs.readFileSync(wo.envi.ssl.file.ca) ] // only for self-signed certificate: https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
|
||
},
|
||
server
|
||
)
|
||
.listen(portHttps, function (err) {
|
||
if (err) console.log(err)
|
||
else
|
||
console.log(
|
||
`[${new Date().toJSON()}] Server listening on [${wo.envi.protocol}] https://${wo.envi.host}:${portHttps} with IPv4=${ipv4} for ${
|
||
wo.envi.prodev
|
||
} environment`
|
||
)
|
||
})
|
||
// }
|
||
} else if ('http2https' === wo.envi.protocol) {
|
||
webServer = require('http')
|
||
.createServer(
|
||
server.all('*', function (ask, reply) {
|
||
reply.redirect(`https://${wo.envi.host}`)
|
||
})
|
||
// 或者 require('redirect-https')() // https://www.npmjs.com/package/redirect-https
|
||
)
|
||
.listen(portHttp, function (err) {
|
||
if (err) console.log(err)
|
||
else
|
||
console.log(
|
||
`[${new Date().toJSON()}] Server listening on ${wo.envi.protocol}://${wo.envi.host}:${portHttp} with IPv4=${ipv4} for ${wo.envi.prodev} environment`
|
||
)
|
||
})
|
||
}else if ('proxy' === wo.envi.protocol) {
|
||
var proxy=require('http-proxy').createProxyServer({
|
||
ssl: {
|
||
key: fs.readFileSync(wo.envi.ssl.file.key),
|
||
cert: fs.readFileSync(wo.envi.ssl.file.cert),
|
||
// ca: [ fs.readFileSync(wo.envi.sslCA) ] // https://nodejs.org/api/tls.html#tls_tls_createserver_options_secureconnectionlistener
|
||
},
|
||
target: `http://127.0.0.1:${wo.envi.proxyPort}`, // iOS 的 AppStore 要求支持IPv6,只能用国外的vultr.com服务器,因此再代理回国内的solet主机。
|
||
// secure: true, // proxying https to https
|
||
ws: true // proxying websockets
|
||
})
|
||
proxy.on('error', function (err, req, res) {
|
||
res.writeHead(500, { 'Content-Type': 'text/plain' })
|
||
res.end('Proxy Error.')
|
||
})
|
||
wo.envi.port=443
|
||
proxy.listen(wo.envi.port)
|
||
console.info('server listening on %s://%s:%d as proxy', wo.envi.protocol, wo.envi.host, wo.envi.port)
|
||
}
|
||
|
||
|
||
return webServer
|
||
})()
|