Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持通过.env配置环境变量,后端支持设置前置路由 #430

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"automerge": "1.0.1-preview.7",
"body-parser": "^1.19.0",
"buffer": "^6.0.3",
"dotenv": "^16.4.7",
"connect-history-api-fallback": "^2.0.0",
"cron": "^3.1.6",
"dns-packet": "^5.6.1",
Expand Down Expand Up @@ -70,4 +71,4 @@
"prettier-plugin-sort-imports": "^1.6.1",
"tinyify": "^3.0.0"
}
}
}
29 changes: 24 additions & 5 deletions backend/src/restful/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,30 @@ 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();
}
});
}

// register routes
registerCollectionRoutes($app);
registerSubscriptionRoutes($app);
Expand Down Expand Up @@ -216,7 +235,7 @@ export default function serve() {
app.use(
be_share_rewrite,
createProxyMiddleware({
target: `http://127.0.0.1:${port}`,
target: `http://127.0.0.1:${port}${prefix}`,
changeOrigin: true,
pathRewrite: async (path, req) => {
if (req.method.toLowerCase() !== 'get')
Expand All @@ -237,7 +256,7 @@ export default function serve() {
app.use(
be_api_rewrite,
createProxyMiddleware({
target: `http://127.0.0.1:${port}${be_api}`,
target: `http://127.0.0.1:${port}${prefix}${be_api}`,
pathRewrite: async (path) => {
return path.includes('?')
? `${path}&share=true`
Expand All @@ -248,7 +267,7 @@ export default function serve() {
app.use(
be_download_rewrite,
createProxyMiddleware({
target: `http://127.0.0.1:${port}${be_download}`,
target: `http://127.0.0.1:${port}${prefix}${be_download}`,
changeOrigin: true,
}),
);
Expand All @@ -269,10 +288,10 @@ export default function serve() {
$.info(`[FRONTEND] ${fe_address}:${fe_port}`);
if (fe_be_path) {
$.info(
`[FRONTEND -> BACKEND] ${fe_address}:${fe_port}${be_api_rewrite} -> http://127.0.0.1:${port}${be_api}`,
`[FRONTEND -> BACKEND] ${fe_address}:${fe_port}${be_api_rewrite} -> http://127.0.0.1:${port}${prefix}${be_api}`,
);
$.info(
`[FRONTEND -> BACKEND] ${fe_address}:${fe_port}${be_download_rewrite} -> http://127.0.0.1:${port}${be_download}`,
`[FRONTEND -> BACKEND] ${fe_address}:${fe_port}${be_download_rewrite} -> http://127.0.0.1:${port}${prefix}${be_download}`,
);
$.info(
`[SHARE BACKEND] ${fe_address}:${fe_port}${be_share_rewrite}`,
Expand Down