diff --git a/packages/api/src/main.js b/packages/api/src/main.js index 571593bd1..a50c34019 100644 --- a/packages/api/src/main.js +++ b/packages/api/src/main.js @@ -6,6 +6,7 @@ const http = require('http'); const cors = require('cors'); const getPort = require('get-port'); const path = require('path'); +const fs = require('fs/promises'); const useController = require('./utility/useController'); const socket = require('./utility/socket'); @@ -91,9 +92,52 @@ function start() { express.static(path.join(__dirname, isProApp() ? '../../dbgate-web-premium/public' : '../../dbgate-web/public')) ); } else if (process.env.DEVWEB) { - // console.log('__dirname', __dirname); - // console.log(path.join(__dirname, '../../web/public/build')); - app.use(getExpressPath('/'), express.static(path.join(__dirname, '../../web/public'))); + const PUBLIC_DIR = path.join(__dirname, '../../web/public'); + + // 1) HTML token-replacement middleware + app.get([getExpressPath('/'), getExpressPath('/*.html')], async (req, res, next) => { + try { + // Resolve the requested file ("/" -> "index.html") + const relPath = req.path === getExpressPath('/') ? '/index.html' : req.path; + const filePath = path.join(PUBLIC_DIR, relPath); + + // Read HTML + let html = await fs.readFile(filePath, 'utf8'); + + // Replace tokens — keep this explicit to avoid leaking env accidentally + if (process.env.DBGATE_GTM_ID) { + html = html.replace(/#HEAD_SCRIPT#/g, ` + + `); + html = html.replace(/#BODY_SCRIPT#/g, process.env.PAGE_BODY_SCRIPT ?? ` + +`); + } else { + html = html.replace(/#HEAD_SCRIPT#/g, process.env.PAGE_HEAD_SCRIPT ?? ''); + html = html.replace(/#BODY_SCRIPT#/g, process.env.PAGE_BODY_SCRIPT ?? ''); + } + + // Optional: add more tokens: + // html = html.replaceAll('#TITLE#', process.env.PAGE_TITLE ?? ''); + + res.type('html').send(html); // Express will set an ETag by default + } catch (err) { + if (err.code === 'ENOENT') return next(); // let static middleware handle 404s + next(err); + } + }); + + // 2) Static assets for everything else (css/js/images/etc.) + app.use( + getExpressPath('/'), + express.static(PUBLIC_DIR, { + // extensions: ['html'], // enable "/about" -> "/about.html" if you want + }) + ); } else { app.get(getExpressPath('/'), (req, res) => { res.send('DbGate API'); diff --git a/packages/web/index.html.tpl b/packages/web/index.html.tpl index 1dfe12c64..ca0b37299 100644 --- a/packages/web/index.html.tpl +++ b/packages/web/index.html.tpl @@ -1,6 +1,7 @@ + #HEAD_SCRIPT# @@ -107,6 +108,7 @@ + #BODY_SCRIPT#