full upgrade (#31)

*  chore: update CI workflow to include testing and multi-arch build (#29)

*  chore: remove unused dependencies from package-lock.json and updated to a better local dev environment (#30)

*  test: add unit tests

*  test: add unit tests for whole project

*  feat: add whatwg-fetch for improved fetch support

*  chore: update Node.js version to 22 in workflow

*  refactor: update types and improve email handling tests

*  refactor: remove unused imports

*  fix: normalize image name to lowercase in workflows

*  fix: ensure Docker image names are consistently lowercase

*  chore: update

*  chore: update base URL to use secret variable

*  chore: update to login to ghcr

*  fix: add missing 'fi' to close if statement in workflow
This commit is contained in:
Denshooter
2025-02-16 16:36:21 +01:00
committed by GitHub
parent b4616234cf
commit 180b9aa9f8
35 changed files with 5499 additions and 1901 deletions

View File

@@ -0,0 +1,12 @@
export function mockFetch(data: Record<string, unknown>) {
return jest.fn().mockImplementation(() =>
Promise.resolve({
ok: true,
headers: {
get: jest.fn().mockReturnValue('image/jpeg'),
},
arrayBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(8)),
json: () => data,
}),
);
}

View File

@@ -0,0 +1,12 @@
export function mockFetch(responseText: string) {
return jest.fn().mockImplementation((url: string) => {
// Check if the URL being requested is our sitemap endpoint.
if (url.includes('/api/sitemap')) {
return Promise.resolve({
ok: true,
text: () => Promise.resolve(responseText),
});
}
return Promise.reject(new Error(`Unknown URL: ${url}`));
});
}

View File

@@ -0,0 +1,8 @@
export function mockFetch(data: Record<string, unknown>) {
return jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: true,
json: () => Promise.resolve(data),
});
});
}

View File

@@ -0,0 +1,9 @@
/**
* Jest Mock
* ./__mocks__/nodemailer.js
**/
import nodemailer from 'nodemailer';
import { getMockFor } from 'nodemailer-mock';
const nodemailermock = getMockFor(nodemailer);
export default nodemailermock;

View File

@@ -0,0 +1,83 @@
import { POST } from '@/app/api/email/route';
import { NextRequest, NextResponse } from 'next/server';
import nodemailermock from '@/app/__tests__/__mocks__/nodemailer';
jest.mock('next/server', () => ({
NextResponse: {
json: jest.fn(),
},
}));
beforeEach(() => {
nodemailermock.mock.reset();
process.env.MY_EMAIL = 'test@dki.one';
process.env.MY_PASSWORD = 'test-password';
});
describe('POST /api/email', () => {
it('should send an email', async () => {
const mockRequest = {
json: jest.fn().mockResolvedValue({
email: 'test@example.com',
name: 'Test User',
message: 'Hello!',
}),
} as unknown as NextRequest;
await POST(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({ message: 'Email sent' });
const sentEmails = nodemailermock.mock.getSentMail();
expect(sentEmails.length).toBe(1);
expect(sentEmails[0].to).toBe('test@dki.one');
expect(sentEmails[0].text).toBe('Hello!\n\n' + 'test@example.com');
});
it('should return an error if EMAIL or PASSWORD is missing', async () => {
delete process.env.MY_EMAIL;
delete process.env.MY_PASSWORD;
const mockRequest = {
json: jest.fn().mockResolvedValue({
email: 'test@example.com',
name: 'Test User',
message: 'Hello!',
}),
} as unknown as NextRequest;
await POST(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({ error: 'Missing EMAIL or PASSWORD' }, { status: 500 });
});
it('should return an error if request body is invalid', async () => {
const mockRequest = {
json: jest.fn().mockResolvedValue({
email: '',
name: 'Test User',
message: 'Test message',
}),
} as unknown as NextRequest;
await POST(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({ error: 'Invalid request body' }, { status: 400 });
});
it('should return an error if sending email fails', async () => {
nodemailermock.mock.setShouldFail(true);
const mockRequest = {
json: jest.fn().mockResolvedValue({
email: 'test@example.com',
name: 'Test User',
message: 'Hello!',
}),
} as unknown as NextRequest;
await POST(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({ error: 'Failed to send email' }, { status: 500 });
});
});

View File

@@ -0,0 +1,57 @@
import { GET } from '@/app/api/fetchAllProjects/route';
import { NextResponse } from 'next/server';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
jest.mock('next/server', () => ({
NextResponse: {
json: jest.fn(),
},
}));
describe('GET /api/fetchAllProjects', () => {
beforeAll(() => {
process.env.GHOST_API_URL = 'http://localhost:2368';
process.env.GHOST_API_KEY = 'some-key';
global.fetch = mockFetch({
posts: [
{
id: '67ac8dfa709c60000117d312',
title: 'Just Doing Some Testing',
meta_description: 'Hello bla bla bla bla',
slug: 'just-doing-some-testing',
updated_at: '2025-02-13T14:25:38.000+00:00',
},
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
it('should return a list of projects', async () => {
await GET();
expect(NextResponse.json).toHaveBeenCalledWith({
posts: [
{
id: '67ac8dfa709c60000117d312',
title: 'Just Doing Some Testing',
meta_description: 'Hello bla bla bla bla',
slug: 'just-doing-some-testing',
updated_at: '2025-02-13T14:25:38.000+00:00',
},
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
});

View File

@@ -0,0 +1,53 @@
import { GET } from '@/app/api/fetchImage/route';
import { NextRequest } from 'next/server';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch-img';
jest.mock('next/server', () => {
class NextResponseClass {
body: unknown;
init: unknown;
constructor(body: unknown, init?: unknown) {
this.body = body;
this.init = init;
}
static json(body: unknown, init?: unknown) {
return new NextResponseClass(body, init);
}
static from(body: unknown, init?: unknown) {
return new NextResponseClass(body, init);
}
}
return { NextResponse: NextResponseClass };
});
global.fetch = mockFetch({
ok: true,
headers: {
get: jest.fn().mockReturnValue('image/jpeg'),
},
arrayBuffer: jest.fn().mockResolvedValue(new ArrayBuffer(8)),
});
describe('GET /api/fetchImage', () => {
it('should return an error if no image URL is provided', async () => {
const mockRequest = {
url: 'http://localhost/api/fetchImage',
} as unknown as NextRequest;
const response = await GET(mockRequest);
expect(response.body).toEqual({ error: 'Missing URL parameter' });
expect(response.init.status).toBe(400);
});
it('should fetch an image if URL is provided', async () => {
const mockRequest = {
url: 'http://localhost/api/fetchImage?url=https://example.com/image.jpg',
} as unknown as NextRequest;
const response = await GET(mockRequest);
expect(response.body).toBeDefined();
expect(response.init.headers['Content-Type']).toBe('image/jpeg');
});
});

View File

@@ -0,0 +1,47 @@
import { GET } from '@/app/api/fetchProject/route';
import { NextRequest, NextResponse } from 'next/server';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
jest.mock('next/server', () => ({
NextResponse: {
json: jest.fn(),
},
}));
describe('GET /api/fetchProject', () => {
beforeAll(() => {
process.env.GHOST_API_URL = 'http://localhost:2368';
process.env.GHOST_API_KEY = 'some-key';
global.fetch = mockFetch({
posts: [
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
it('should fetch a project by slug', async () => {
const mockRequest = {
url: 'http://localhost/api/fetchProject?slug=blockchain-based-voting-system',
} as unknown as NextRequest;
await GET(mockRequest);
expect(NextResponse.json).toHaveBeenCalledWith({
posts: [
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
});

View File

@@ -0,0 +1,14 @@
import { GET } from '@/app/api/og/route';
import { ImageResponse } from 'next/og';
jest.mock('next/og', () => ({
ImageResponse: jest.fn(),
}));
describe('GET /api/og', () => {
it('should return an Open Graph image', async () => {
await GET();
expect(ImageResponse).toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,44 @@
import { GET } from '@/app/api/sitemap/route';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
jest.mock('next/server', () => ({
NextResponse: jest.fn().mockImplementation((body, init) => ({ body, init })),
}));
describe('GET /api/sitemap', () => {
beforeAll(() => {
process.env.GHOST_API_URL = 'http://localhost:2368';
process.env.GHOST_API_KEY = 'test-api-key';
process.env.NEXT_PUBLIC_BASE_URL = 'https://dki.one';
global.fetch = mockFetch({
posts: [
{
id: '67ac8dfa709c60000117d312',
title: 'Just Doing Some Testing',
meta_description: 'Hello bla bla bla bla',
slug: 'just-doing-some-testing',
updated_at: '2025-02-13T14:25:38.000+00:00',
},
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
it('should return a sitemap', async () => {
const response = await GET();
expect(response.body).toContain('<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">');
expect(response.body).toContain('<loc>https://dki.one/</loc>');
expect(response.body).toContain('<loc>https://dki.one/legal-notice</loc>');
expect(response.body).toContain('<loc>https://dki.one/privacy-policy</loc>');
expect(response.body).toContain('<loc>https://dki.one/projects/just-doing-some-testing</loc>');
expect(response.body).toContain('<loc>https://dki.one/projects/blockchain-based-voting-system</loc>');
expect(response.init.headers['Content-Type']).toBe('application/xml');
});
});

View File

@@ -0,0 +1,29 @@
import { render, screen, fireEvent } from '@testing-library/react';
import Contact from '@/app/components/Contact';
import '@testing-library/jest-dom';
// Mock the fetch function
global.fetch = jest.fn(() =>
Promise.resolve({
json: () => Promise.resolve({ message: 'Email sent' }),
})
) as jest.Mock;
describe('Contact', () => {
it('renders the contact form', () => {
render(<Contact />);
expect(screen.getByPlaceholderText('Name')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Email')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Message')).toBeInTheDocument();
});
it('submits the form', async () => {
render(<Contact />);
fireEvent.change(screen.getByPlaceholderText('Name'), { target: { value: 'John Doe' } });
fireEvent.change(screen.getByPlaceholderText('Email'), { target: { value: 'john@example.com' } });
fireEvent.change(screen.getByPlaceholderText('Message'), { target: { value: 'Hello!' } });
fireEvent.click(screen.getByText('Send'));
expect(await screen.findByText('Email sent')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import Footer from '@/app/components/Footer';
import '@testing-library/jest-dom';
describe('Footer', () => {
it('renders the footer', () => {
render(<Footer />);
expect(screen.getByText('Connect with me on social platforms:')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,28 @@
import { render, screen } from '@testing-library/react';
import Header from '@/app/components/Header';
import '@testing-library/jest-dom';
describe('Header', () => {
it('renders the header', () => {
render(<Header />);
expect(screen.getByText('Dennis Konkol')).toBeInTheDocument();
const aboutButtons = screen.getAllByText('About');
expect(aboutButtons.length).toBeGreaterThan(0);
const projectsButtons = screen.getAllByText('Projects');
expect(projectsButtons.length).toBeGreaterThan(0);
const contactButtons = screen.getAllByText('Contact');
expect(contactButtons.length).toBeGreaterThan(0);
});
it('renders the mobile header', () => {
render(<Header />);
const openMenuButton = screen.getByLabelText('Open menu');
expect(openMenuButton).toBeInTheDocument();
const closeMenuButton = screen.getByLabelText('Close menu');
expect(closeMenuButton).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,15 @@
import { render, screen } from '@testing-library/react';
import Hero from '@/app/components/Hero';
import '@testing-library/jest-dom';
describe('Hero', () => {
it('renders the hero section', () => {
render(<Hero />);
expect(screen.getByText('Hi, Im Dennis')).toBeInTheDocument();
expect(screen.getByText('Student & Software Engineer')).toBeInTheDocument();
expect(screen.getByText('Based in Osnabrück, Germany')).toBeInTheDocument();
expect(screen.getByText('Passionate about technology, coding, and solving real-world problems. I enjoy building innovative solutions and continuously expanding my knowledge.')).toBeInTheDocument();
expect(screen.getByText('Currently working on exciting projects that merge creativity with functionality. Always eager to learn and collaborate!')).toBeInTheDocument();
expect(screen.getByAltText('Image of Dennis')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,44 @@
import { render, screen, waitFor } from '@testing-library/react';
import Projects from '@/app/components/Projects';
import '@testing-library/jest-dom';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
describe('Projects', () => {
beforeAll(() => {
process.env.GHOST_API_URL = 'http://localhost:2368';
process.env.GHOST_API_KEY = 'some-key';
global.fetch = mockFetch({
posts: [
{
id: '67ac8dfa709c60000117d312',
title: 'Just Doing Some Testing',
meta_description: 'Hello bla bla bla bla',
slug: 'just-doing-some-testing',
updated_at: '2025-02-13T14:25:38.000+00:00',
},
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
meta_description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
it('renders the projects section', async () => {
render(<Projects />);
expect(await screen.findByText('Projects')).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByText('Just Doing Some Testing')).toBeInTheDocument();
expect(screen.getByText('Hello bla bla bla bla')).toBeInTheDocument();
expect(screen.getByText('Blockchain Based Voting System')).toBeInTheDocument();
expect(screen.getByText('This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.')).toBeInTheDocument();
expect(screen.getByText('More to come')).toBeInTheDocument();
});
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import LegalNotice from '@/app/legal-notice/page';
import '@testing-library/jest-dom';
describe('LegalNotice', () => {
it('renders the legal notice page', () => {
render(<LegalNotice />);
expect(screen.getByText('Impressum')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import NotFound from '@/app/not-found';
import '@testing-library/jest-dom';
describe('NotFound', () => {
it('renders the 404 page', () => {
render(<NotFound />);
expect(screen.getByText("Oops! The page you're looking for doesn't exist.")).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import Home from '@/app/page';
import '@testing-library/jest-dom';
describe('Home', () => {
it('renders the home page', () => {
render(<Home />);
expect(screen.getByText('Hi, Im Dennis')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,10 @@
import { render, screen } from '@testing-library/react';
import PrivacyPolicy from '@/app/privacy-policy/page';
import '@testing-library/jest-dom';
describe('PrivacyPolicy', () => {
it('renders the privacy policy page', () => {
render(<PrivacyPolicy />);
expect(screen.getByText('Datenschutzerklärung')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,45 @@
import { render, screen, waitFor } from '@testing-library/react';
import ProjectDetails from '@/app/projects/[slug]/page';
import '@testing-library/jest-dom';
import { useRouter, useSearchParams, useParams, usePathname } from 'next/navigation';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch';
jest.mock('next/navigation', () => ({
useRouter: jest.fn(),
useSearchParams: jest.fn(),
useParams: jest.fn(),
usePathname: jest.fn(),
}));
describe('ProjectDetails', () => {
beforeAll(() => {
process.env.GHOST_API_URL = 'http://localhost:2368';
process.env.GHOST_API_KEY = 'some-key';
global.fetch = mockFetch({
posts: [
{
id: '67aaffc3709c60000117d2d9',
title: 'Blockchain Based Voting System',
description: 'This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.',
html: '<p>This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.</p>',
slug: 'blockchain-based-voting-system',
updated_at: '2025-02-13T16:54:42.000+00:00',
},
],
});
});
it('renders the project details page', async () => {
(useRouter as jest.Mock).mockReturnValue({});
(useSearchParams as jest.Mock).mockReturnValue(new URLSearchParams());
(useParams as jest.Mock).mockReturnValue({ slug: 'blockchain-based-voting-system' });
(usePathname as jest.Mock).mockReturnValue('/projects/blockchain-based-voting-system');
render(<ProjectDetails />);
await waitFor(() => {
expect(screen.getByText('Blockchain Based Voting System')).toBeInTheDocument();
expect(screen.getByText('This project aims to revolutionize voting systems by leveraging blockchain to ensure security, transparency, and immutability.')).toBeInTheDocument();
});
});
});

View File

@@ -0,0 +1,44 @@
import '@testing-library/jest-dom';
import { GET } from '@/app/sitemap.xml/route';
import { mockFetch } from '@/app/__tests__/__mocks__/mock-fetch-sitemap';
jest.mock('next/server', () => ({
NextResponse: jest.fn().mockImplementation((body, init) => ({ body, init })),
}));
describe('Sitemap Component', () => {
beforeAll(() => {
process.env.NEXT_PUBLIC_BASE_URL = 'https://dki.one';
global.fetch = mockFetch(`
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://dki.one/</loc>
</url>
<url>
<loc>https://dki.one/legal-notice</loc>
</url>
<url>
<loc>https://dki.one/privacy-policy</loc>
</url>
<url>
<loc>https://dki.one/projects/just-doing-some-testing</loc>
</url>
<url>
<loc>https://dki.one/projects/blockchain-based-voting-system</loc>
</url>
</urlset>
`);
});
it('should render the sitemap XML', async () => {
const response = await GET();
expect(response.body).toContain('<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">');
expect(response.body).toContain('<loc>https://dki.one/</loc>');
expect(response.body).toContain('<loc>https://dki.one/legal-notice</loc>');
expect(response.body).toContain('<loc>https://dki.one/privacy-policy</loc>');
expect(response.body).toContain('<loc>https://dki.one/projects/just-doing-some-testing</loc>');
expect(response.body).toContain('<loc>https://dki.one/projects/blockchain-based-voting-system</loc>');
expect(response.init.headers['Content-Type']).toBe('application/xml');
});
});

View File

@@ -1,10 +1,7 @@
import {type NextRequest, NextResponse} from "next/server";
import { type NextRequest, NextResponse } from "next/server";
import nodemailer from "nodemailer";
import SMTPTransport from "nodemailer/lib/smtp-transport";
import Mail from "nodemailer/lib/mailer";
import dotenv from "dotenv";
dotenv.config();
export async function POST(request: NextRequest) {
const body = (await request.json()) as {
@@ -12,7 +9,7 @@ export async function POST(request: NextRequest) {
name: string;
message: string;
};
const {email, name, message} = body;
const { email, name, message } = body;
const user = process.env.MY_EMAIL ?? "";
const pass = process.env.MY_PASSWORD ?? "";
@@ -20,8 +17,16 @@ export async function POST(request: NextRequest) {
if (!user || !pass) {
console.error("Missing email/password environment variables");
return NextResponse.json(
{error: "Internal server error"},
{status: 500},
{ error: "Missing EMAIL or PASSWORD" },
{ status: 500 },
);
}
if (!email || !name || !message) {
console.error("Invalid request body");
return NextResponse.json(
{ error: "Invalid request body" },
{ status: 400 },
);
}
@@ -61,9 +66,9 @@ export async function POST(request: NextRequest) {
try {
await sendMailPromise();
return NextResponse.json({message: "Email sent"});
return NextResponse.json({ message: "Email sent" });
} catch (err) {
console.error("Error sending email:", err);
return NextResponse.json({error: err}, {status: 500});
return NextResponse.json({ error: "Failed to send email" }, { status: 500 });
}
}

View File

@@ -1,26 +1,26 @@
import {NextResponse} from "next/server";
import { NextResponse } from "next/server";
export const runtime = "nodejs"; // Force Node runtime
const GHOST_API_URL = "http://192.168.179.31:2368";
const GHOST_API_URL = process.env.GHOST_API_URL || "http://192.168.179.31:2368";
const GHOST_API_KEY = process.env.GHOST_API_KEY;
export async function GET() {
try {
const response = await fetch(
`${GHOST_API_URL}/ghost/api/content/posts/?key=${GHOST_API_KEY}&limit=all`,
);
if (!response.ok) {
console.error(`Failed to fetch posts: ${response.statusText}`);
return NextResponse.json([]);
}
const posts = await response.json();
return NextResponse.json(posts);
} catch (error) {
console.error("Failed to fetch posts from Ghost:", error);
return NextResponse.json(
{error: "Failed to fetch projects"},
{status: 500},
);
try {
const response = await fetch(
`${GHOST_API_URL}/ghost/api/content/posts/?key=${GHOST_API_KEY}&limit=all`,
);
if (!response.ok) {
console.error(`Failed to fetch posts: ${response.statusText}`);
return NextResponse.json([]);
}
const posts = await response.json();
return NextResponse.json(posts);
} catch (error) {
console.error("Failed to fetch posts from Ghost:", error);
return NextResponse.json(
{ error: "Failed to fetch projects" },
{ status: 500 },
);
}
}

View File

@@ -2,7 +2,7 @@ import { NextResponse } from "next/server";
export const runtime = "nodejs"; // Force Node runtime
const GHOST_API_URL = "http://big-bear-ghost:2368";
const GHOST_API_URL = process.env.GHOST_API_URL || "http://192.168.179.31:2368";
const GHOST_API_KEY = process.env.GHOST_API_KEY;
export async function GET(request: Request) {

View File

@@ -1,34 +0,0 @@
import {NextResponse} from 'next/server';
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
export async function GET() {
try {
const projectsDirectory = path.join(process.cwd(), 'public/projects');
const filenames = fs.readdirSync(projectsDirectory);
const projects = filenames
.filter((filename) => {
const filePath = path.join(projectsDirectory, filename);
return fs.statSync(filePath).isFile();
})
.map((filename) => {
const filePath = path.join(projectsDirectory, filename);
const fileContents = fs.readFileSync(filePath, 'utf8');
const {data} = matter(fileContents);
return {
id: data.id,
title: data.title,
description: data.description,
slug: filename.replace('.md', ''),
};
});
return NextResponse.json(projects);
} catch (error) {
console.error("Failed to fetch projects:", error);
return NextResponse.json({error: 'Failed to fetch projects'}, {status: 500});
}
}

View File

@@ -1,87 +1,102 @@
import {NextResponse} from "next/server";
import { NextResponse } from "next/server";
interface Project {
slug: string;
updated_at?: string; // Optional timestamp for last modification
slug: string;
updated_at?: string; // Optional timestamp for last modification
}
interface ProjectsData {
posts: Project[];
posts: Project[];
}
export const dynamic = 'force-dynamic';
export const dynamic = "force-dynamic";
export const runtime = "nodejs"; // Force Node runtime
const GHOST_API_URL = "http://192.168.179.31:2368";
const GHOST_API_URL = process.env.GHOST_API_URL || "http://192.168.179.31:2368";
const GHOST_API_KEY = process.env.GHOST_API_KEY;
// Funktion, um die XML für die Sitemap zu generieren
function generateXml(sitemapRoutes: { url: string; lastModified: string }[]) {
const xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>';
const urlsetOpen = '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">';
const urlsetClose = '</urlset>';
const xmlHeader = '<?xml version="1.0" encoding="UTF-8"?>';
const urlsetOpen =
'<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">';
const urlsetClose = "</urlset>";
const urlEntries = sitemapRoutes
.map(
(route) => `
const urlEntries = sitemapRoutes
.map(
(route) => `
<url>
<loc>${route.url}</loc>
<lastmod>${route.lastModified}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>`
)
.join("");
</url>`,
)
.join("");
return `${xmlHeader}${urlsetOpen}${urlEntries}${urlsetClose}`;
return `${xmlHeader}${urlsetOpen}${urlEntries}${urlsetClose}`;
}
export async function GET() {
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://dki.one";
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://dki.one";
// Statische Routen
const staticRoutes = [
{url: `${baseUrl}/`, lastModified: new Date().toISOString(), priority: 1, changeFreq: "weekly"},
{url: `${baseUrl}/legal-notice`, lastModified: new Date().toISOString(), priority: 0.5, changeFreq: "yearly"},
{url: `${baseUrl}/privacy-policy`, lastModified: new Date().toISOString(), priority: 0.5, changeFreq: "yearly"},
];
// Statische Routen
const staticRoutes = [
{
url: `${baseUrl}/`,
lastModified: new Date().toISOString(),
priority: 1,
changeFreq: "weekly",
},
{
url: `${baseUrl}/legal-notice`,
lastModified: new Date().toISOString(),
priority: 0.5,
changeFreq: "yearly",
},
{
url: `${baseUrl}/privacy-policy`,
lastModified: new Date().toISOString(),
priority: 0.5,
changeFreq: "yearly",
},
];
try {
const response = await fetch(
`${GHOST_API_URL}/ghost/api/content/posts/?key=${GHOST_API_KEY}&limit=all`
);
if (!response.ok) {
console.error(`Failed to fetch posts: ${response.statusText}`);
return new NextResponse(generateXml(staticRoutes), {
headers: {"Content-Type": "application/xml"},
})
}
const projectsData = await response.json() as ProjectsData;
const projects = projectsData.posts;
// Dynamische Projekt-Routen generieren
const sitemapRoutes = projects.map((project) => {
const lastModified = project.updated_at || new Date().toISOString();
return {
url: `${baseUrl}/projects/${project.slug}`,
lastModified,
priority: 0.8,
changeFreq: "monthly",
};
});
const allRoutes = [...staticRoutes, ...sitemapRoutes];
// Rückgabe der Sitemap im XML-Format
return new NextResponse(generateXml(allRoutes), {
headers: {"Content-Type": "application/xml"},
});
} catch (error) {
console.error("Failed to fetch posts from Ghost:", error);
// Rückgabe der statischen Routen, falls Fehler auftritt
return new NextResponse(generateXml(staticRoutes), {
headers: {"Content-Type": "application/xml"},
});
try {
const response = await fetch(
`${GHOST_API_URL}/ghost/api/content/posts/?key=${GHOST_API_KEY}&limit=all`,
);
if (!response.ok) {
console.error(`Failed to fetch posts: ${response.statusText}`);
return new NextResponse(generateXml(staticRoutes), {
headers: { "Content-Type": "application/xml" },
});
}
const projectsData = (await response.json()) as ProjectsData;
const projects = projectsData.posts;
// Dynamische Projekt-Routen generieren
const sitemapRoutes = projects.map((project) => {
const lastModified = project.updated_at || new Date().toISOString();
return {
url: `${baseUrl}/projects/${project.slug}`,
lastModified,
priority: 0.8,
changeFreq: "monthly",
};
});
const allRoutes = [...staticRoutes, ...sitemapRoutes];
// Rückgabe der Sitemap im XML-Format
return new NextResponse(generateXml(allRoutes), {
headers: { "Content-Type": "application/xml" },
});
} catch (error) {
console.log("Failed to fetch posts from Ghost:", error);
// Rückgabe der statischen Routen, falls Fehler auftritt
return new NextResponse(generateXml(staticRoutes), {
headers: { "Content-Type": "application/xml" },
});
}
}

View File

@@ -1,13 +0,0 @@
// app/components/ClientCookieConsentBanner.tsx
"use client";
import dynamic from 'next/dynamic';
const CookieConsentBanner = dynamic(() => import('./CookieConsentBanner'), {ssr: false});
const ClientCookieConsentBanner = ({onConsentChange}: { onConsentChange: (consent: string) => void }) => {
return <CookieConsentBanner onConsentChange={onConsentChange}/>;
};
export default ClientCookieConsentBanner;

View File

@@ -1,98 +0,0 @@
import React, {useEffect, useState} from "react";
import CookieConsent from "react-cookie-consent";
import Link from "next/link";
const CookieConsentBanner = ({onConsentChange}: { onConsentChange: (consent: string) => void }) => {
const [isVisible, setIsVisible] = useState(false);
const [isFadingIn, setIsFadingIn] = useState(false);
useEffect(() => {
const consent = localStorage.getItem("CookieConsent");
if (!consent) {
setIsVisible(true);
setTimeout(() => setIsFadingIn(true), 10); // Delay to trigger CSS transition
}
}, []);
const handleAccept = () => {
setIsFadingIn(false);
setTimeout(() => {
localStorage.setItem("CookieConsent", "accepted");
setIsVisible(false);
onConsentChange("accepted");
}, 500); // Match the duration of the fade-out transition
};
const handleDecline = () => {
setIsFadingIn(false);
setTimeout(() => {
localStorage.setItem("CookieConsent", "declined");
setIsVisible(false);
onConsentChange("declined");
}, 500); // Match the duration of the fade-out transition
};
if (!isVisible) {
return null;
}
return (
<CookieConsent
location="bottom"
buttonText="Accept All"
declineButtonText="Decline"
enableDeclineButton
cookieName="CookieConsent"
containerClasses={`${isFadingIn ? 'fade-in' : 'fade-out'}`}
style={{
background: "rgba(211,211,211,0.44)",
color: "#333",
boxShadow: "0 4px 8px rgba(0, 0, 0, 0.1)",
padding: "1rem",
borderRadius: "8px",
backdropFilter: "blur(10px)",
margin: "2rem",
width: "calc(100% - 4rem)",
display: "flex",
justifyContent: "space-between",
alignItems: "center",
textAlign: "left",
transition: "opacity 0.5s ease",
opacity: isFadingIn ? 1 : 0,
}}
buttonWrapperClasses="button-wrapper"
buttonStyle={{
backgroundColor: "#4CAF50",
color: "#FFF",
fontSize: "14px",
borderRadius: "4px",
padding: "0.5rem 1rem",
margin: "0.5rem",
width: "100%",
maxWidth: "200px",
}}
declineButtonStyle={{
backgroundColor: "#f44336",
color: "#FFF",
fontSize: "14px",
borderRadius: "4px",
padding: "0.5rem 1rem",
margin: "0.5rem",
width: "100%",
maxWidth: "200px",
}}
expires={90}
onAccept={handleAccept}
onDecline={handleDecline}
>
<div className="content-wrapper text-xl">
This website uses cookies to enhance your experience. By using our website, you consent to the use of
cookies.
You can read more in our <Link href="/" className="text-blue-800 transition-underline">privacy
policy</Link>.
</div>
</CookieConsent>
);
};
export default CookieConsentBanner;

View File

@@ -3,7 +3,7 @@ import {NextResponse} from "next/server";
export const dynamic = 'force-dynamic';
export async function GET() {
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://dki.one"; // Stelle sicher, dass die Base-URL korrekt ist
const baseUrl = process.env.NEXT_PUBLIC_BASE_URL || "https://dki.one";
const apiUrl = `${baseUrl}/api/sitemap`; // Verwende die vollständige URL zur API
try {