diff --git a/main.mjs b/main.mjs index 9824411..89d414e 100644 --- a/main.mjs +++ b/main.mjs @@ -11,6 +11,10 @@ const __dirname = path.dirname(__filename); const app = express(); app.use(express.static(path.join(__dirname, "ui"))); +app.get('/apiurl', (req, res) => { + res.send(process.env.API_URL ?? 'http://localhost:3000'); +}); + app.get('*', (req, res) => { res.sendFile(__dirname + '/ui/index.html'); }); diff --git a/ui/api/ApiBase.mjs b/ui/api/ApiBase.mjs index 3e241d6..f7c0d53 100644 --- a/ui/api/ApiBase.mjs +++ b/ui/api/ApiBase.mjs @@ -3,10 +3,18 @@ export class ApiBase { 'Content-Type': 'application/json' }; static get apiBaseUrl() { - if (window.location.hostname === "localhost") { - return "http://localhost:3000"; + const apiUrl = sessionStorage.getItem("apiUrl"); + if (apiUrl) { + return apiUrl; } else { - return "https://api." + window.location.hostname; + fetch("/apiurl", { + method: 'GET', + headers: this.usualHeaders, + }).then(async (res) => { + const res2 = await this.basicResponseHandling(res); + sessionStorage.setItem("apiUrl", res2.data); + window.location.reload(); + }); } };