Skip to content

Commit 1c9ed32

Browse files
committed
Replace hardcoded uris in frontend
1 parent cdb7119 commit 1c9ed32

17 files changed

+77
-28
lines changed

docker-compose.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ services:
22
frontend:
33
build:
44
context: ./frontend
5+
args:
6+
- USER_SVC_PORT=$USER_SVC_PORT
7+
- QUESTION_SVC_PORT=$QUESTION_SVC_PORT
8+
- MATCHING_SVC_PORT=$MATCHING_SVC_PORT
59
ports:
610
- $FRONTEND_PORT:$FRONTEND_PORT
711
depends_on:
812
- question-service
913
- user-service
14+
environment:
15+
- PORT=$FRONTEND_PORT
1016

1117
question-service:
1218
build:
@@ -16,6 +22,7 @@ services:
1622
environment:
1723
- PORT=$QUESTION_SVC_PORT
1824
- DB_URI=$QUESTION_SVC_DB_URI
25+
- FRONTEND_PORT=$FRONTEND_PORT
1926

2027
user-service:
2128
build:

frontend/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2+
package-lock.json
23
.next

frontend/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Base stage
22
FROM node:20-alpine AS base
3+
ARG USER_SVC_PORT \
4+
QUESTION_SVC_PORT \
5+
MATCHING_SVC_PORT
36
WORKDIR /app
47
COPY package.json .
58
COPY yarn.lock .
69
RUN yarn install --frozen-lockfile
10+
ENV NEXT_PUBLIC_USER_SVC_PORT=$USER_SVC_PORT \
11+
NEXT_PUBLIC_QUESTION_SVC_PORT=$QUESTION_SVC_PORT \
12+
NEXT_PUBLIC_MATCHING_SVC_PORT=$MATCHING_SVC_PORT
713

814
# Production build stage
915
FROM base AS build

frontend/app/auth/auth-context.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { userServiceUri } from "@/lib/api-uri";
34
import { User, UserSchema } from "@/lib/schemas/user-schema";
45
import { useRouter } from "next/navigation";
56
import {
@@ -32,7 +33,7 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
3233
// Login using locally stored JWT token
3334
useEffect(() => {
3435
if (token) {
35-
fetch("http://localhost:3001/auth/verify-token", {
36+
fetch(`${userServiceUri}/auth/verify-token`, {
3637
method: "GET",
3738
headers: {
3839
Authorization: `Bearer ${token}`,
@@ -51,7 +52,7 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
5152

5253
// Login using email and password
5354
const login = async (email: string, password: string): Promise<User> => {
54-
const response = await fetch("http://localhost:3001/auth/login", {
55+
const response = await fetch(`${userServiceUri}/auth/login`, {
5556
method: "POST",
5657
headers: {
5758
"Content-Type": "application/json",

frontend/components/admin-user-management/admin-user-management.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import LoadingScreen from "@/components/common/loading-screen";
1616
import AdminEditUserModal from "@/components/admin-user-management/admin-edit-user-modal";
1717
import { PencilIcon, Trash2Icon } from "lucide-react";
1818
import { User, UserArraySchema } from "@/lib/schemas/user-schema";
19+
import { userServiceUri } from "@/lib/api-uri";
1920

2021
const fetcher = async (url: string): Promise<User[]> => {
2122
const token = localStorage.getItem("jwtToken");
@@ -43,7 +44,7 @@ export default function AdminUserManagement() {
4344
const auth = useAuth();
4445

4546
const { data, isLoading, mutate } = useSWR(
46-
"http://localhost:3001/users",
47+
`${userServiceUri}/users`,
4748
fetcher
4849
);
4950

@@ -67,7 +68,7 @@ export default function AdminUserManagement() {
6768
throw new Error("No authentication token found");
6869
}
6970

70-
const response = await fetch(`http://localhost:3001/users/${userId}`, {
71+
const response = await fetch(`${userServiceUri}/users/${userId}`, {
7172
method: "DELETE",
7273
headers: {
7374
Authorization: `Bearer ${token}`,

frontend/components/forget-password.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Label } from "@/components/ui/label";
1515
import { Button } from "@/components/ui/button";
1616
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
1717
import { AlertCircle } from "lucide-react";
18+
import { userServiceUri } from "@/lib/api-uri";
1819

1920
const ForgetPassword: React.FC = () => {
2021
const [email, setEmail] = useState("");
@@ -34,16 +35,13 @@ const ForgetPassword: React.FC = () => {
3435
}
3536

3637
try {
37-
const response = await fetch(
38-
"http://localhost:3001/users/forget-password",
39-
{
40-
method: "POST",
41-
headers: {
42-
"Content-Type": "application/json",
43-
},
44-
body: JSON.stringify({ email }),
45-
}
46-
);
38+
const response = await fetch(`${userServiceUri}/users/forget-password`, {
39+
method: "POST",
40+
headers: {
41+
"Content-Type": "application/json",
42+
},
43+
body: JSON.stringify({ email }),
44+
});
4745

4846
if (!response.ok) {
4947
const errorData = await response.json();

frontend/components/questions/questions-listing.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from "@/lib/schemas/question-schema";
1919
import QuestionFormModal from "./question-form-modal";
2020
import { updateQuestion } from "@/lib/update-question";
21+
import { questionServiceUri } from "@/lib/api-uri";
2122

2223
const fetcher = async (url: string): Promise<Question[]> => {
2324
const token = localStorage.getItem("jwtToken");
@@ -55,7 +56,7 @@ export default function QuestionListing() {
5556
const [search, setSearch] = useState(searchParams.get("search") || "");
5657

5758
const { data, isLoading, mutate } = useSWR(
58-
`http://localhost:8000/questions?category=${encodeURIComponent(category)}&complexity=${encodeURIComponent(complexity)}&search=${encodeURIComponent(search)}`,
59+
`${questionServiceUri}/questions?category=${encodeURIComponent(category)}&complexity=${encodeURIComponent(complexity)}&search=${encodeURIComponent(search)}`,
5960
fetcher,
6061
{
6162
keepPreviousData: true,
@@ -144,7 +145,7 @@ export default function QuestionListing() {
144145
try {
145146
const token = localStorage.getItem("jwtToken");
146147
const response = await fetch(
147-
"http://localhost:8000/questions/batch-upload",
148+
`${questionServiceUri}/questions/batch-upload`,
148149
{
149150
method: "POST",
150151
headers: {
@@ -189,7 +190,7 @@ export default function QuestionListing() {
189190

190191
try {
191192
const response = await fetch(
192-
`http://localhost:8000/questions/${selectedQuestion.id}`,
193+
`${questionServiceUri}/questions/${selectedQuestion.id}`,
193194
{
194195
method: "DELETE",
195196
}
@@ -262,7 +263,7 @@ export default function QuestionListing() {
262263

263264
const handleCreate = async (newQuestion: Question) => {
264265
try {
265-
const response = await fetch("http://localhost:8000/questions", {
266+
const response = await fetch(`${questionServiceUri}/questions`, {
266267
method: "POST",
267268
headers: {
268269
"Content-Type": "application/json",

frontend/components/user-settings/user-settings.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import LoadingScreen from "@/components/common/loading-screen";
2424
import { useAuth } from "@/app/auth/auth-context";
2525
import { cn } from "@/lib/utils";
2626
import { User, UserSchema } from "@/lib/schemas/user-schema";
27+
import { userServiceUri } from "@/lib/api-uri";
2728

2829
const fetcher = async (url: string): Promise<User> => {
2930
// Retrieve the JWT token from localStorage
@@ -54,7 +55,7 @@ export default function UserSettings({ userId }: { userId: string }) {
5455
const fileInputRef = useRef<HTMLInputElement>(null);
5556

5657
const { data, error, isLoading, mutate } = useSWR(
57-
`http://localhost:3001/users/${userId}`,
58+
`${userServiceUri}/users/${userId}`,
5859
fetcher
5960
);
6061
const [user, setUser] = useState<User | null>(null);
@@ -139,7 +140,7 @@ export default function UserSettings({ userId }: { userId: string }) {
139140
}
140141

141142
try {
142-
const response = await fetch(`http://localhost:3001/users/${userId}`, {
143+
const response = await fetch(`${userServiceUri}/users/${userId}`, {
143144
method: "PATCH",
144145
headers: {
145146
Authorization: `Bearer ${token}`,
@@ -177,7 +178,7 @@ export default function UserSettings({ userId }: { userId: string }) {
177178
}
178179

179180
try {
180-
const response = await fetch(`http://localhost:3001/users/${userId}`, {
181+
const response = await fetch(`${userServiceUri}/users/${userId}`, {
181182
method: "DELETE",
182183
headers: {
183184
Authorization: `Bearer ${token}`,
@@ -237,7 +238,7 @@ export default function UserSettings({ userId }: { userId: string }) {
237238

238239
try {
239240
const response = await fetch(
240-
`http://localhost:3001/users/${userId}/change-password`,
241+
`${userServiceUri}/users/${userId}/change-password`,
241242
{
242243
method: "PATCH",
243244
headers: {

frontend/lib/api-uri.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const userServiceUri: string = `http://host.docker.internal:${process.env.NEXT_PUBLIC_USER_SVC_PORT}`;
2+
export const questionServiceUri: string = `http://host.docker.internal:${process.env.NEXT_PUBLIC_QUESTION_SVC_PORT}`;
3+
export const matchingServiceUri: string = `http://host.docker.internal:${process.env.NEXT_PUBLIC_MATCHING_SVC_PORT}`;

frontend/lib/reset-password.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { userServiceUri } from "@/lib/api-uri";
2+
13
export const resetPassword = async (token: string, password: string) => {
2-
const response = await fetch("http://localhost:3001/users/reset-password", {
4+
const response = await fetch(`${userServiceUri}/users/reset-password`, {
35
method: "POST",
46
headers: {
57
"Content-Type": "application/json",

0 commit comments

Comments
 (0)