Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FRONTEND - Add API key to all requests to bloom backend #203

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions frontend/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
NEXT_PUBLIC_MAPTILER_TO=3IWed9ZbNv0p8UVD6Ogv
NEXT_PUBLIC_DOMAIN=http://localhost:3000

NEXT_PUBLIC_BACKEND_BASE_URL=http://localhost:8000
NEXT_PUBLIC_BACKEND_API_KEY=bloom
22 changes: 17 additions & 5 deletions frontend/app/map/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import PositionPreview from "@/components/core/map/position-preview"
import { getVessels, getVesselsLatestPositions } from "@/services/backend-rest-client"

async function fetchVessels() {
const response = await getVessels();
return response.data ?? [];
try {
const response = await getVessels();
return response?.data;

} catch(error) {
console.log("An error occured while fetching vessels: " + error)
return [];
}
}

// TODO(CT): move this logic within a cron job
async function fetchLatestPositions() {
// TODO(CT): move this logic within a cron job
const response = await getVesselsLatestPositions();
return response.data ?? [];
try {
const response = await getVesselsLatestPositions();
return response?.data;

} catch(error) {
console.log("An error occured while fetching vessels latest positions: " + error)
return [];
}
}

export default async function MapPage() {
Expand Down
11 changes: 9 additions & 2 deletions frontend/services/backend-rest-client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@

import { Vessel, VesselExcursion, VesselExcursionSegment, VesselPositions } from "@/types/vessel";
import axios from "axios";
import axios, { InternalAxiosRequestConfig } from "axios";

const BASE_URL = "http://localhost:8000";
const BASE_URL = process.env.NEXT_PUBLIC_BACKEND_BASE_URL;
const API_KEY = process.env.NEXT_PUBLIC_BACKEND_API_KEY ?? 'no-key-found';

// Authenticate all requests to Bloom backend
axios.interceptors.request.use((request: InternalAxiosRequestConfig) => {
request.headers.set('x-key', API_KEY);
return request;
});

export function getVessels() {
return axios.get<Vessel[]>(`${BASE_URL}/vessels`);
Expand Down
Loading