Perf: 提前加载 .env;后端复用前端 Path

This commit is contained in:
Aritro37 2025-03-17 22:01:38 +08:00 committed by GitHub
parent bb5c9d43d0
commit dd92a26e6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 18 deletions

View File

@ -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';

View File

@ -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(