From dd92a26e6cb6df4be8632163db621688d04834fa Mon Sep 17 00:00:00 2001 From: Aritro37 <85866469+Aritro37@users.noreply.github.com> Date: Mon, 17 Mar 2025 22:01:38 +0800 Subject: [PATCH] =?UTF-8?q?Perf:=20=E6=8F=90=E5=89=8D=E5=8A=A0=E8=BD=BD=20?= =?UTF-8?q?.env;=E5=90=8E=E7=AB=AF=E5=A4=8D=E7=94=A8=E5=89=8D=E7=AB=AF=20P?= =?UTF-8?q?ath?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/main.js | 6 +++++- backend/src/restful/index.js | 36 +++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/backend/src/main.js b/backend/src/main.js index 03737d8..3ba7bf1 100644 --- a/backend/src/main.js +++ b/backend/src/main.js @@ -11,6 +11,7 @@ * @documentation: https://www.notion.so/Sub-Store-6259586994d34c11a4ced5c406264b46 */ import { version } from '../package.json'; +import $ from '@/core/app'; console.log( ` ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ @@ -18,7 +19,10 @@ console.log( ┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅┅ `, ); - +if ($.env.isNode) { + const dotenv = eval(`require("dotenv")`); + dotenv.config(); +} import migrate from '@/utils/migration'; import serve from '@/restful'; diff --git a/backend/src/restful/index.js b/backend/src/restful/index.js index 385c53d..fb5ea61 100644 --- a/backend/src/restful/index.js +++ b/backend/src/restful/index.js @@ -24,30 +24,31 @@ import registerParserRoutes from './parser'; export default function serve() { let port; let host; - let prefix; if ($.env.isNode) { - const dotenv = eval(`require("dotenv")`); - dotenv.config(); port = eval('process.env.SUB_STORE_BACKEND_API_PORT') || 3000; host = eval('process.env.SUB_STORE_BACKEND_API_HOST') || '::'; - prefix = eval('process.env.SUB_STORE_BACKEND_PATH_PREFIX'); - if (prefix && !prefix.startsWith("/")){ - prefix = '/${prefix}' - } } const $app = express({ substore: $, port, host }); - if (prefix && prefix.length >= 2){ - $app.use((req, res, next) => { - if (req.path.startsWith(prefix)) { - const newPath = req.url.replace(prefix, '') || '/'; - req.url = newPath; - next(); - } else { - res.status(403).send(); + if ($.env.isNode) { + const be_prefix = eval('process.env.SUB_STORE_BACKEND_PREFIX'); + const fe_be_path = eval('process.env.SUB_STORE_FRONTEND_BACKEND_PATH'); + if (be_prefix) { + if(!fe_be_path.startsWith('/')){ + throw new Error( + 'SUB_STORE_FRONTEND_BACKEND_PATH should start with /', + ); } - }); + $app.use((req, res, next) => { + if (req.path.startsWith(fe_be_path)) { + const newPath = req.url.replace(fe_be_path, '') || '/'; + req.url = newPath; + next(); + } else { + res.status(403).send(); + } + }); + } } - // register routes registerCollectionRoutes($app); registerSubscriptionRoutes($app); @@ -219,6 +220,7 @@ export default function serve() { let be_download_rewrite = ''; let be_api_rewrite = ''; let be_share_rewrite = `${be_share}:type/:name`; + let prefix = eval('process.env.SUB_STORE_BACKEND_PREFIX') ? fe_be_path : ''; if (fe_be_path) { if (!fe_be_path.startsWith('/')) { throw new Error(