Skip to content

Commit

Permalink
Add dynamic API_URL loading
Browse files Browse the repository at this point in the history
  • Loading branch information
tomijange committed Sep 6, 2023
1 parent 757e1fe commit 1cc21f2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ FROM nginx:1.20
WORKDIR /usr/share/nginx/html

RUN rm -rf ./*

COPY entrypoint.sh .
COPY --from=builder /app/build .
COPY --from=builder /app/nginx.conf /etc/nginx/conf.d/default.conf

ENTRYPOINT ["nginx", "-g", "daemon off;"]
ENTRYPOINT [ "./entrypoint.sh" ]
CMD ["nginx", "-g", "daemon off;"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The Docker-Image features a nginx server to enabled browser-routing in React.
To start the Dashboard just run:

```
docker run -p 80:80 ghcr.io/privacyengineering/hawk-core-monitor
docker run -e API_URL=http://localhost:8000 -p 80:80 ghcr.io/privacyengineering/hawk-core-monitor
```

**Note: The Dashboard needs a running Hawk-Service instance reverse-proxied on the same port by default.**
Expand Down
9 changes: 9 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -eo pipefail

# Create api.json file with API_URL
cat <<EOF > api.json
{"apiUrl": "${API_URL}"}
EOF

exec "$@"
42 changes: 31 additions & 11 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,39 @@ import {App} from "./layouts/App";

const store = configureStore();
export const useThunkDispatch = () => useDispatch<typeof store.dispatch>();
export const URL_BASE = process.env.REACT_APP_API_URL || "";
export let URL_BASE = "";
export const HEADER_JSON = {
'Accept': 'application/json',
'Content-Type': 'application/json'
};

ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>
</React.StrictMode>,
document.getElementById("root")
);
const fetchApiUrl = async () => {
const url = process.env.REACT_APP_API_URL;
if (!url) {
try {
const meta = await fetch("api.json").then(r => r.json())
if (meta?.apiUrl) {
return meta.apiUrl;
}
} catch (e) {
console.error("failed to fetch api url from api.json", e);
}
}
return url;
}

fetchApiUrl().then(url => {
console.log("fetched api url: ", url);
URL_BASE = url;

ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<BrowserRouter>
<App/>
</BrowserRouter>
</Provider>
</React.StrictMode>,
document.getElementById("root")
);
});

0 comments on commit 1cc21f2

Please sign in to comment.