Skip to content
Snippets Groups Projects
Commit 338bf914 authored by pedro f's avatar pedro f
Browse files

make api_host configurable

parent 82ab1554
No related branches found
No related tags found
1 merge request!2/analysis route integration
## Configuration ## Configuration
This sucks, but we'll come up with something better later. 1. Create a `.env.local` file in the root directory of the project:
```bash
# API host for internal requests.
#
# For example, if the API is running on localhost:8080,
# set INTERNAL_API_HOST=localhost:8080.
#
# If the API is running as a docker compose service called "api",
# set INTERNAL_API_HOST=api:8080.
INTERNAL_API_HOST=
1. Set the API host in `next.config.mjs`: # API host for public requests.
```js #
/* next.config.mjs */ # This is the host that the client will use to make requests to the API.
const nextConfig = { # It is needed because the client connects to the API from the browser
// ... # for websocket connections.
async rewrites() { #
return [ # For example, if the API is running on saci.inf.ufpr.br:8080,
{ # set NEXT_PUBLIC_API_HOST=saci.inf.ufpr.br:8080.
source: "/api/:path*", NEXT_PUBLIC_API_HOST=
destination: "http{s}://{api_host}/:path*",
},
];
},
};
``` ```
2. Set the API host in `src/app/analysis/[id]/page.js`:
```js
/* src/app/analysis/[id]/page.js */
const api_host = "{api_host}";
// ...
```
e.g. api_host = "localhost:8080"
## Development ## Development
......
...@@ -4,7 +4,7 @@ const nextConfig = { ...@@ -4,7 +4,7 @@ const nextConfig = {
return [ return [
{ {
source: "/api/:path*", source: "/api/:path*",
destination: "http://localhost:8080/:path*", destination: `http://${process.env.INTERNAL_API_HOST}/:path*`,
}, },
]; ];
}, },
......
...@@ -6,8 +6,6 @@ import { notFound } from "next/navigation"; ...@@ -6,8 +6,6 @@ import { notFound } from "next/navigation";
import "@/styles/analysis/activity-timeline.css"; import "@/styles/analysis/activity-timeline.css";
import "@/styles/analysis/page.css"; import "@/styles/analysis/page.css";
const api_host = "localhost:8080";
export default function Analysis({ params: { id } }) { export default function Analysis({ params: { id } }) {
const [report, setReport] = useState(null); const [report, setReport] = useState(null);
const [failed, setFailed] = useState(false); const [failed, setFailed] = useState(false);
...@@ -21,7 +19,7 @@ export default function Analysis({ params: { id } }) { ...@@ -21,7 +19,7 @@ export default function Analysis({ params: { id } }) {
}, [failed]); }, [failed]);
useEffect(() => { useEffect(() => {
const ws = new WebSocket(`ws://${api_host}/status/${id}`); const ws = new WebSocket(`ws://${process.env.NEXT_PUBLIC_API_HOST}/status/${id}`);
ws.onmessage = (event) => { ws.onmessage = (event) => {
setReport(JSON.parse(event.data)); setReport(JSON.parse(event.data));
...@@ -71,7 +69,7 @@ export default function Analysis({ params: { id } }) { ...@@ -71,7 +69,7 @@ export default function Analysis({ params: { id } }) {
} }
function validateId(id, setFailed) { function validateId(id, setFailed) {
fetch(`https://${api_host}/analysis/${id}`, { cache: 'no-store' }) fetch(`http://${process.env.NEXT_PUBLIC_API_HOST}/analysis/${id}`, { cache: 'no-store' })
.then((res) => { .then((res) => {
if (!res.ok) { if (!res.ok) {
throw new Error(`Failed to validate id: got ${res.status} response from api`); throw new Error(`Failed to validate id: got ${res.status} response from api`);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment