Skip to content

Commit

Permalink
feat: Node 版后端支持挂载前端文件夹, 环境变量 SUB_STORE_FRONTEND_PATH, `SUB_STORE_F…
Browse files Browse the repository at this point in the history
…RONTEND_HOST`, `SUB_STORE_FRONTEND_PORT`
  • Loading branch information
xream committed Dec 10, 2023
1 parent 7aaa03d commit a177664
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 42 deletions.
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -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",
Expand Down
10 changes: 10 additions & 0 deletions backend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions backend/src/restful/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
}
}
}
2 changes: 1 addition & 1 deletion backend/src/vendor/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 0 additions & 40 deletions package-lock.json

This file was deleted.

0 comments on commit a177664

Please sign in to comment.