From dc0b3f2eb29ee70311bba8e0c75bedf214acaccd Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 22 Dec 2025 23:44:29 +0800 Subject: [PATCH] Fix QlBaseUrl: use URL rewrite for base path support (#2876) * Initial plan * Add QlBaseUrl support to backend routes Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Fix whitelist check to use base-URL-aware paths Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Update websocket and frontend to support base URL Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Address code review feedback: fix JWT regex and path construction Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Fix path construction: use req.path directly for whitelist check Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Add clarifying comments and improve code readability Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Apply code review suggestions: improve clarity and simplify logic Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> * Simplify baseUrl implementation using URL rewrite Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> --- back/config/index.ts | 14 ++++++++++++++ back/loaders/express.ts | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/back/config/index.ts b/back/config/index.ts index 76eb08c6..f7c7a54c 100644 --- a/back/config/index.ts +++ b/back/config/index.ts @@ -64,6 +64,19 @@ if (!process.env.QL_DIR) { const lastVersionFile = `https://qn.whyour.cn/version.yaml`; +// Get and normalize QlBaseUrl +let baseUrl = process.env.QlBaseUrl || ''; +if (baseUrl) { + // Ensure it starts with / + if (!baseUrl.startsWith('/')) { + baseUrl = `/${baseUrl}`; + } + // Remove trailing slash for consistency in route definitions + if (baseUrl.endsWith('/')) { + baseUrl = baseUrl.slice(0, -1); + } +} + const rootPath = process.env.QL_DIR as string; const envFound = dotenv.config({ path: path.join(rootPath, '.env') }); @@ -116,6 +129,7 @@ if (envFound.error) { export default { ...config, jwt: config.jwt, + baseUrl, rootPath, tmpPath, dataPath, diff --git a/back/loaders/express.ts b/back/loaders/express.ts index f7d4a65b..63065a21 100644 --- a/back/loaders/express.ts +++ b/back/loaders/express.ts @@ -15,6 +15,13 @@ import path from 'path'; export default ({ app }: { app: Application }) => { app.set('trust proxy', 'loopback'); app.use(cors()); + + // Rewrite URLs to strip baseUrl prefix if configured + // This allows the rest of the app to work without baseUrl awareness + if (config.baseUrl) { + app.use(rewrite(`${config.baseUrl}/*`, '/$1')); + } + app.get(`${config.api.prefix}/env.js`, serveEnv); app.use(`${config.api.prefix}/static`, express.static(config.uploadPath));