From 7a793b954e7d6b446c36bdb104d2e9717970f4b4 Mon Sep 17 00:00:00 2001 From: xream Date: Sun, 10 Dec 2023 13:07:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Node=20=E7=89=88=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=82=E8=BD=BD=E5=89=8D=E7=AB=AF=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9,=20=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=20`S?= =?UTF-8?q?UB=5FSTORE=5FFRONTEND=5FPATH`,=20`SUB=5FSTORE=5FFRONTEND=5FHOST?= =?UTF-8?q?`,=20`SUB=5FSTORE=5FFRONTEND=5FPORT`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 3 ++- backend/pnpm-lock.yaml | 10 +++++++++ backend/src/restful/index.js | 42 +++++++++++++++++++++++++++++++++++ backend/src/vendor/express.js | 2 +- package.json | 5 +++++ pnpm-lock.yaml | 19 ++++++++++++++++ 6 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 package.json create mode 100644 pnpm-lock.yaml diff --git a/backend/package.json b/backend/package.json index 8847a33..5cf0fe7 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.118", + "version": "2.14.119", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { @@ -16,6 +16,7 @@ "dependencies": { "automerge": "1.0.1-preview.7", "body-parser": "^1.19.0", + "connect-history-api-fallback": "^2.0.0", "express": "^4.17.1", "js-base64": "^3.7.2", "lodash": "^4.17.21", diff --git a/backend/pnpm-lock.yaml b/backend/pnpm-lock.yaml index 99a02c4..e35563f 100644 --- a/backend/pnpm-lock.yaml +++ b/backend/pnpm-lock.yaml @@ -11,6 +11,9 @@ dependencies: body-parser: specifier: ^1.19.0 version: registry.npmmirror.com/body-parser@1.19.0 + connect-history-api-fallback: + specifier: ^2.0.0 + version: registry.npmmirror.com/connect-history-api-fallback@2.0.0 express: specifier: ^4.17.1 version: registry.npmmirror.com/express@4.17.1 @@ -3543,6 +3546,13 @@ packages: xdg-basedir: registry.npmmirror.com/xdg-basedir@4.0.0 dev: true + registry.npmmirror.com/connect-history-api-fallback@2.0.0: + resolution: {integrity: sha512-U73+6lQFmfiNPrYbXqr6kZ1i1wiRqXnp2nhMsINseWXO8lDau0LGEffJ8kQi4EjLZympVgRdvqjAgiZ1tgzDDA==, registry: http://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/connect-history-api-fallback/-/connect-history-api-fallback-2.0.0.tgz} + name: connect-history-api-fallback + version: 2.0.0 + engines: {node: '>=0.8'} + dev: false + registry.npmmirror.com/console-browserify@1.2.0: resolution: {integrity: sha512-ZMkYO/LkF17QvCPqM0gxw8yUzigAOZOSWSHg91FH6orS7vcEj5dVZTidN2fQ14yBSdg97RqhSNwLUXInd52OTA==, registry: http://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/console-browserify/-/console-browserify-1.2.0.tgz} name: console-browserify diff --git a/backend/src/restful/index.js b/backend/src/restful/index.js index f8f220c..86c2c97 100644 --- a/backend/src/restful/index.js +++ b/backend/src/restful/index.js @@ -37,4 +37,46 @@ export default function serve() { registerMiscRoutes($app); $app.start(); + + if ($.env.isNode) { + const path = eval(`require("path")`); + const fs = eval(`require("fs")`); + const fe_port = eval('process.env.SUB_STORE_FRONTEND_PORT') || 3001; + const fe_host = + eval('process.env.SUB_STORE_FRONTEND_HOST') || host || '::'; + const fe_path = eval('process.env.SUB_STORE_FRONTEND_PATH'); + const fe_abs_path = path.resolve( + fe_path || path.join(__dirname, 'frontend'), + ); + if (fe_path) { + try { + fs.accessSync(path.join(fe_abs_path, 'index.html')); + } catch (e) { + throw new Error( + `[FRONTEND] index.html file not found in ${fe_abs_path}`, + ); + } + + const express_ = eval(`require("express")`); + const history = eval(`require("connect-history-api-fallback")`); + + const app = express_(); + + const staticFileMiddleware = express_.static(fe_path); + + app.use(staticFileMiddleware); + app.use( + history({ + disableDotRule: true, + verbose: true, + }), + ); + app.use(staticFileMiddleware); + + const listener = app.listen(fe_port, fe_host, () => { + const { address, port } = listener.address(); + $.info(`[FRONTEND] ${address}:${port}`); + }); + } + } } diff --git a/backend/src/vendor/express.js b/backend/src/vendor/express.js index e3eb5ef..ce85558 100644 --- a/backend/src/vendor/express.js +++ b/backend/src/vendor/express.js @@ -32,7 +32,7 @@ export default function express({ substore: $, port, host }) { app.start = () => { const listener = app.listen(port, host, () => { const { address, port } = listener.address(); - $.info(`Express started on ${address}:${port}`); + $.info(`[BACKEND] ${address}:${port}`); }); }; return app; diff --git a/package.json b/package.json new file mode 100644 index 0000000..498e503 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "connect-history-api-fallback": "^2.0.0" + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml new file mode 100644 index 0000000..aaaee5a --- /dev/null +++ b/pnpm-lock.yaml @@ -0,0 +1,19 @@ +lockfileVersion: '6.0' + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + +dependencies: + connect-history-api-fallback: + specifier: ^2.0.0 + version: registry.npmmirror.com/connect-history-api-fallback@2.0.0 + +packages: + + registry.npmmirror.com/connect-history-api-fallback@2.0.0: + resolution: {integrity: sha512-U73+6lQFmfiNPrYbXqr6kZ1i1wiRqXnp2nhMsINseWXO8lDau0LGEffJ8kQi4EjLZympVgRdvqjAgiZ1tgzDDA==, registry: http://registry.npm.taobao.org/, tarball: https://registry.npmmirror.com/connect-history-api-fallback/-/connect-history-api-fallback-2.0.0.tgz} + name: connect-history-api-fallback + version: 2.0.0 + engines: {node: '>=0.8'} + dev: false