Skip to content

Commit

Permalink
refactor: Use environment variables for backend endpoint
Browse files Browse the repository at this point in the history
Instead of hardcoding in backend endpoints in the front-end use environemnt variables setup suggest by next.js. Makes it easier to switch endpoints on-the-fly in dev, test prod
  • Loading branch information
johanravn committed Oct 20, 2024
1 parent 6a7e3b9 commit 5a17c8b
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22.9.0
1 change: 1 addition & 0 deletions front/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_BACKEND_HOST=http://localhost:5000
1 change: 1 addition & 0 deletions front/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_BACKEND_HOST=http://localhost:5000
4 changes: 2 additions & 2 deletions front/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion front/src/app/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import createAPI from "@/lib/api";

const api = createAPI({
baseURL: "http://localhost:5000",
baseURL: `${process.env.NEXT_PUBLIC_BACKEND_HOST}`,
withCredentials: true,
});

Expand Down
3 changes: 2 additions & 1 deletion front/src/lib/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { z } from "zod";

import { GetManySchema } from "@/lib/schemas";

export const HOST = "http://localhost:5000";
export const HOST = `${process.env.NEXT_PUBLIC_BACKEND_HOST}`;
export const BASE_ROUTE = `/api/v1`;


export const instance = axios.create({
withCredentials: true,
baseURL: HOST,
Expand Down
2 changes: 1 addition & 1 deletion front/src/lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type APIConfig = {
};

const DEFAULT_CONFIG: APIConfig = {
baseURL: "http://localhost:5000",
baseURL: `${process.env.NEXT_PUBLIC_BACKEND_HOST}`,
withCredentials: true,
};

Expand Down

0 comments on commit 5a17c8b

Please sign in to comment.