Skip to content

Commit

Permalink
Felles path for dev og prod
Browse files Browse the repository at this point in the history
  • Loading branch information
eilifjohansen committed Jan 16, 2025
1 parent 9d1e5ed commit b542662
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 78 deletions.
33 changes: 28 additions & 5 deletions app/api/create/route.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
import { NextResponse } from 'next/server';
import { getToken, validateToken, requestOboToken } from '@navikt/oasis';

export async function POST(request: Request) {
const apiUrl = process.env.NODE_ENV === 'production'
? 'http://skup-backend/api/apps'
: 'https://skupapi.intern.nav.no/api/apps';
: 'http://0.0.0.0:8086/api/apps';

try {
let token: string | null;
if (process.env.NODE_ENV === 'production') {
token = getToken(request);
if (!token) {
return NextResponse.json({ error: 'Missing token' }, { status: 401 });
}

const validation = await validateToken(token);
if (!validation.ok) {
return NextResponse.json({ error: 'Token validation failed' }, { status: 401 });
}

const obo = await requestOboToken(token, 'api://prod-gcp.team-researchops.skup-backend/.default');
if (!obo.ok) {
return NextResponse.json({ error: 'OBO token request failed' }, { status: 401 });
}

token = obo.token;
} else {
token = 'placeholder-token';
}

const appData = await request.json();
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer kinda-clever-token',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify(appData),
});

if (!response.ok) {
const errorData = await response.json();
console.error('Network response was not ok:', response.status, errorData.detail || errorData.message || 'An unknown error occurred');
throw new Error(errorData.detail || errorData.message || 'An unknown error occurred');
const errorDetails = await response.text();
console.error('Network response was not ok:', response.status, errorDetails);
throw new Error(`Network response was not ok: ${response.status} - ${errorDetails}`);
}

const data = await response.json();
Expand Down
27 changes: 25 additions & 2 deletions app/api/delete/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextResponse } from 'next/server';
import { getToken, validateToken, requestOboToken } from '@navikt/oasis';

export async function DELETE(request: Request) {
const { searchParams } = new URL(request.url);
Expand All @@ -10,14 +11,36 @@ export async function DELETE(request: Request) {

const apiUrl = process.env.NODE_ENV === 'production'
? `http://skup-backend/api/apps/${appId}`
: `https://skupapi.intern.nav.no/api/apps/${appId}`;
: `http://0.0.0.0:8086/api/apps/${appId}`;

try {
let token: string | null;
if (process.env.NODE_ENV === 'production') {
token = getToken(request);
if (!token) {
return NextResponse.json({ error: 'Missing token' }, { status: 401 });
}

const validation = await validateToken(token);
if (!validation.ok) {
return NextResponse.json({ error: 'Token validation failed' }, { status: 401 });
}

const obo = await requestOboToken(token, 'api://prod-gcp.team-researchops.skup-backend/.default');
if (!obo.ok) {
return NextResponse.json({ error: 'OBO token request failed' }, { status: 401 });
}

token = obo.token;
} else {
token = 'placeholder-token';
}

const response = await fetch(apiUrl, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer kinda-clever-token',
'Authorization': `Bearer ${token}`,
},
});

Expand Down
31 changes: 27 additions & 4 deletions app/api/read/route.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { NextResponse } from 'next/server';
import { getToken, validateToken, requestOboToken } from '@navikt/oasis';

export async function GET() {
export async function GET(request: Request) {
const apiUrl = process.env.NODE_ENV === 'production'
? 'http://skup-backend/api/apps'
: 'https://skupapi.intern.nav.no/api/apps';
: 'http://0.0.0.0:8086/api/apps';

try {
let token: string | null;
if (process.env.NODE_ENV === 'production') {
token = getToken(request);
if (!token) {
return NextResponse.json({ error: 'Missing token' }, { status: 401 });
}

const validation = await validateToken(token);
if (!validation.ok) {
return NextResponse.json({ error: 'Token validation failed' }, { status: 401 });
}

const obo = await requestOboToken(token, 'api://prod-gcp.team-researchops.skup-backend/.default');
if (!obo.ok) {
return NextResponse.json({ error: 'OBO token request failed' }, { status: 401 });
}

token = obo.token;
} else {
token = 'placeholder-token';
}

const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer kinda-clever-token',
'Authorization': `Bearer ${token}`,
},
});

Expand All @@ -25,7 +48,7 @@ export async function GET() {
} catch (error) {
if (error instanceof Error) {
console.error('Fetch failed:', error.message, error.stack);
return NextResponse.json({ error: 'Fetch failed', message: error.message, stack: error.stack }, { status: 500 });
return NextResponse.json({ error: 'Fetch failed', message: error.message }, { status: 500 });
} else {
console.error('An unknown error occurred');
return NextResponse.json({ error: 'An unknown error occurred' }, { status: 500 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function CreateApp({ onAppCreated }: { onAppCreated: () => void }
const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault();
try {
const response = await fetch('/api/obocreate', {
const response = await fetch('/api/create', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use client";
import React, { useState, useEffect } from 'react';
import Read from './components/obocrud/read';
import Create from './components/obocrud/create';
import Delete from './components/obocrud/delete';
import Read from '@/app/components/crud/read';
import Create from '@/app/components/crud/create';
import Delete from '@/app/components/crud/delete';

interface App {
app_id: string;
Expand All @@ -18,7 +18,7 @@ export default function MainSection() {

const fetchApps = async () => {
try {
const response = await fetch('/api/oboread')
const response = await fetch('/api/read')
if (!response.ok) {
const errorDetails = await response.text();
console.error('Network response was not ok:', response.status, errorDetails);
Expand Down
24 changes: 4 additions & 20 deletions app/api/obocreate/route.ts → test/create/route.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
import { NextResponse } from 'next/server';
import { getToken, validateToken, requestOboToken } from '@navikt/oasis';

export async function POST(request: Request) {
const apiUrl = process.env.NODE_ENV === 'production'
? 'http://skup-backend/api/apps'
: 'https://skupapi.intern.nav.no/api/apps';

try {
const token = getToken(request);
if (!token) {
return NextResponse.json({ error: 'Missing token' }, { status: 401 });
}

const validation = await validateToken(token);
if (!validation.ok) {
return NextResponse.json({ error: 'Token validation failed' }, { status: 401 });
}

const obo = await requestOboToken(token, 'api://prod-gcp.team-researchops.skup-backend/.default');
if (!obo.ok) {
return NextResponse.json({ error: 'OBO token request failed' }, { status: 401 });
}

const appData = await request.json();
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${obo.token}`,
'Authorization': 'Bearer kinda-clever-token',
},
body: JSON.stringify(appData),
});

if (!response.ok) {
const errorDetails = await response.text();
console.error('Network response was not ok:', response.status, errorDetails);
throw new Error(`Network response was not ok: ${response.status} - ${errorDetails}`);
const errorData = await response.json();
console.error('Network response was not ok:', response.status, errorData.detail || errorData.message || 'An unknown error occurred');
throw new Error(errorData.detail || errorData.message || 'An unknown error occurred');
}

const data = await response.json();
Expand Down
18 changes: 1 addition & 17 deletions app/api/obodelete/route.ts → test/delete/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { NextResponse } from 'next/server';
import { getToken, validateToken, requestOboToken } from '@navikt/oasis';

export async function DELETE(request: Request) {
const { searchParams } = new URL(request.url);
Expand All @@ -14,26 +13,11 @@ export async function DELETE(request: Request) {
: `https://skupapi.intern.nav.no/api/apps/${appId}`;

try {
const token = getToken(request);
if (!token) {
return NextResponse.json({ error: 'Missing token' }, { status: 401 });
}

const validation = await validateToken(token);
if (!validation.ok) {
return NextResponse.json({ error: 'Token validation failed' }, { status: 401 });
}

const obo = await requestOboToken(token, 'api://prod-gcp.team-researchops.skup-backend/.default');
if (!obo.ok) {
return NextResponse.json({ error: 'OBO token request failed' }, { status: 401 });
}

const response = await fetch(apiUrl, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${obo.token}`,
'Authorization': 'Bearer kinda-clever-token',
},
});

Expand Down
6 changes: 3 additions & 3 deletions app/dev/page.tsx → test/dev/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use client";
import React, { useState, useEffect } from 'react';
import Read from '@/app/components/devcrud/read';
import Create from '@/app/components/devcrud/create';
import Delete from '@/app/components/devcrud/delete';
import Read from '@/test/devcrud/read';
import Create from '@/test/devcrud/create';
import Delete from '@/test/devcrud/delete';

interface App {
app_id: string;
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions app/components/obocrud/delete.tsx → test/devcrud/delete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function DeleteApp({ onAppDeleted }: { onAppDeleted: () => void }
const fetchApps = async () => {
setIsLoading(true);
try {
const response = await fetch('/api/oboread');
const response = await fetch('/api/read');
if (!response.ok) {
throw new Error('Kunne ikke hente appene. Vennligst sjekk nettverkstilkoblingen din og prøv igjen.');
}
Expand All @@ -44,7 +44,7 @@ export default function DeleteApp({ onAppDeleted }: { onAppDeleted: () => void }
}

try {
const response = await fetch(`/api/obodelete?app_id=${selectedApp.app_id}`, {
const response = await fetch(`/api/delete?app_id=${selectedApp.app_id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Expand Down
File renamed without changes.
24 changes: 4 additions & 20 deletions app/api/oboread/route.ts → test/read/route.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
import { NextResponse } from 'next/server';
import { getToken, validateToken, requestOboToken } from '@navikt/oasis';

export async function GET(request: Request) {
export async function GET() {
const apiUrl = process.env.NODE_ENV === 'production'
? 'http://skup-backend/api/apps'
: 'https://skupapi.intern.nav.no/api/apps';
: 'http://0.0.0.0:8086/api/apps';

try {
const token = getToken(request);
if (!token) {
return NextResponse.json({ error: 'Missing token' }, { status: 401 });
}

const validation = await validateToken(token);
if (!validation.ok) {
return NextResponse.json({ error: 'Token validation failed' }, { status: 401 });
}

const obo = await requestOboToken(token, 'api://prod-gcp.team-researchops.skup-backend/.default');
if (!obo.ok) {
return NextResponse.json({ error: 'OBO token request failed' }, { status: 401 });
}

const response = await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${obo.token}`,
'Authorization': 'Bearer kinda-clever',
},
});

Expand All @@ -41,7 +25,7 @@ export async function GET(request: Request) {
} catch (error) {
if (error instanceof Error) {
console.error('Fetch failed:', error.message, error.stack);
return NextResponse.json({ error: 'Fetch failed', message: error.message, stack: error.stack }, { status: 500 });
return NextResponse.json({ error: 'Fetch failed', message: error.message }, { status: 500 });
} else {
console.error('An unknown error occurred');
return NextResponse.json({ error: 'An unknown error occurred' }, { status: 500 });
Expand Down

0 comments on commit b542662

Please sign in to comment.