rename to base-envar-config

This commit is contained in:
陆柯 2022-06-04 09:01:15 +08:00
parent 19f93f302f
commit dc7081a6c9
2 changed files with 74 additions and 85 deletions

View File

@ -23,7 +23,7 @@
"method-override": "^2.3.10", "method-override": "^2.3.10",
"morgan": "^1.9.0", "morgan": "^1.9.0",
"serve-favicon": "^2.5.0", "serve-favicon": "^2.5.0",
"base.enviconfig": "git+https://git.faronear.org/npm/base.enviconfig", "base-envar-config": "git+https://git.faronear.org/npm/base-envar-config",
"vhost": "^3.0.2" "vhost": "^3.0.2"
}, },
"devDependencies": { "devDependencies": {

View File

@ -3,7 +3,8 @@ const path = require('path')
const express = require('express') const express = require('express')
const wo = (global.wo = { const wo = (global.wo = {
envar : require('base.enviconfig').mergeEnvar({rawEnvar: { envar: require('base-envar-config').merge_envar({
rawEnvar: {
Commander_Option_List: [ Commander_Option_List: [
// 命令行里可以接受的参数。将传给 config.js 里的 commander。每个参数的定义格式是 [参数名,参数键,描述]后两者用于传给commander取值后覆盖掉Config里的同名变量。 // 命令行里可以接受的参数。将传给 config.js 里的 commander。每个参数的定义格式是 [参数名,参数键,描述]后两者用于传给commander取值后覆盖掉Config里的同名变量。
['Web_Protocol', '-P, --Web_Protocol <string>', 'Web protocol: http|https|httpall.'], ['Web_Protocol', '-P, --Web_Protocol <string>', 'Web protocol: http|https|httpall.'],
@ -23,7 +24,8 @@ const wo = (global.wo = {
{ Web_Root: 'dist', Web_Index: 'index.html', domainList: ['']} { Web_Root: 'dist', Web_Index: 'index.html', domainList: ['']}
], ],
*/ */
}}) },
}),
}) })
if (typeof wo.envar.Web_Ssl === 'string') { if (typeof wo.envar.Web_Ssl === 'string') {
wo.envar.Web_Ssl = eval(`(${wo.envar.Web_Ssl})`) wo.envar.Web_Ssl = eval(`(${wo.envar.Web_Ssl})`)
@ -91,9 +93,7 @@ if (typeof wo.envar.Web_Ssl === 'string') {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
console.log({time: new Date().toJSON()}, console.log({ time: new Date().toJSON() }, `Server listening on ${wo.envar.Web_Protocol}://${wo.envar.Web_Hostname}:${portHttp}`)
`Server listening on ${wo.envar.Web_Protocol}://${wo.envar.Web_Hostname}:${portHttp}`
)
} }
}) })
} else if ('https' === wo.envar.Web_Protocol) { } else if ('https' === wo.envar.Web_Protocol) {
@ -112,9 +112,7 @@ if (typeof wo.envar.Web_Ssl === 'string') {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
console.log({time: new Date().toJSON()}, console.log({ time: new Date().toJSON() }, `Server listening on ${wo.envar.Web_Protocol}://${wo.envar.Web_Hostname}:${portHttps}`)
`Server listening on ${wo.envar.Web_Protocol}://${wo.envar.Web_Hostname}:${portHttps}`
)
} }
}) })
} else if ('httpall' === wo.envar.Web_Protocol) { } else if ('httpall' === wo.envar.Web_Protocol) {
@ -130,15 +128,12 @@ if (typeof wo.envar.Web_Ssl === 'string') {
.createServer(function (ask, reply) { .createServer(function (ask, reply) {
reply.writeHead(301, { Location: `https://${ask.headers.host.replace(`:${portHttp}`, `:${portHttps}`)}${ask.url}` }) reply.writeHead(301, { Location: `https://${ask.headers.host.replace(`:${portHttp}`, `:${portHttps}`)}${ask.url}` })
reply.end() reply.end()
} })
)
.listen(portHttp, function (err) { .listen(portHttp, function (err) {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
console.log({time: new Date().toJSON()}, console.log({ time: new Date().toJSON() }, `Server redirecting from http://${wo.envar.Web_Hostname}:${portHttp}`)
`Server redirecting from http://${wo.envar.Web_Hostname}:${portHttp}`
)
} }
}) })
webServer = require('https') webServer = require('https')
@ -154,25 +149,22 @@ if (typeof wo.envar.Web_Ssl === 'string') {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
console.log({time: new Date().toJSON()}, console.log({ time: new Date().toJSON() }, `Server listening on https://${wo.envar.Web_Hostname}:${portHttps}`)
`Server listening on https://${wo.envar.Web_Hostname}:${portHttps}`
)
} }
}) })
// } // }
} else if ('redirectHttp2Https' === wo.envar.Web_Protocol) { } else if ('redirectHttp2Https' === wo.envar.Web_Protocol) {
let portHttp = wo.envar.Web_Port?.portHttp || 80 let portHttp = wo.envar.Web_Port?.portHttp || 80
let portHttps = wo.envar.Web_Port?.portHttps || 443 let portHttps = wo.envar.Web_Port?.portHttps || 443
webServer = server.all('*', function (ask, reply) { webServer = server
.all('*', function (ask, reply) {
reply.redirect(301, `https://${ask.headers.host.replace(`:${portHttp}`, `:${portHttps}`)}${ask.url}`) reply.redirect(301, `https://${ask.headers.host.replace(`:${portHttp}`, `:${portHttps}`)}${ask.url}`)
}) })
.listen(portHttp, function (err) { .listen(portHttp, function (err) {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
console.log({time: new Date().toJSON()}, console.log({ time: new Date().toJSON() }, `Server listening on ${wo.envar.Web_Protocol}://${wo.envar.Web_Hostname}:${portHttp}`)
`Server listening on ${wo.envar.Web_Protocol}://${wo.envar.Web_Hostname}:${portHttp}`
)
} }
}) })
} else if ('proxyHttps2Http' === wo.envar.Web_Protocol) { } else if ('proxyHttps2Http' === wo.envar.Web_Protocol) {
@ -186,7 +178,7 @@ if (typeof wo.envar.Web_Ssl === 'string') {
}, },
target: `http://127.0.0.1:${portHttp}`, // iOS 的 AppStore 要求支持IPv6只能用国外的vultr.com服务器因此再代理回国内的solet主机。 target: `http://127.0.0.1:${portHttp}`, // iOS 的 AppStore 要求支持IPv6只能用国外的vultr.com服务器因此再代理回国内的solet主机。
// secure: true, // proxying https to https // secure: true, // proxying https to https
ws: true // proxying websockets ws: true, // proxying websockets
}) })
proxy.on('error', function (err, req, res) { proxy.on('error', function (err, req, res) {
res.writeHead(500, { 'Content-Type': 'text/plain' }) res.writeHead(500, { 'Content-Type': 'text/plain' })
@ -196,13 +188,10 @@ if (typeof wo.envar.Web_Ssl === 'string') {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
console.log({time: new Date().toJSON()}, console.log({ time: new Date().toJSON() }, `Server listening on ${wo.envar.Web_Protocol}://${wo.envar.Web_Hostname}:${portHttps}`)
`Server listening on ${wo.envar.Web_Protocol}://${wo.envar.Web_Hostname}:${portHttps}`
)
} }
}) })
} }
return webServer return webServer
})() })()