Skip to content

Commit

Permalink
configurable api url
Browse files Browse the repository at this point in the history
  • Loading branch information
targoninc-alex committed May 27, 2024
1 parent d6a7de4 commit f0ceafb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
14 changes: 11 additions & 3 deletions ui/api/ApiBase.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
};

Expand Down

0 comments on commit f0ceafb

Please sign in to comment.