✨ refactor: update environment variable usage and add caching
This commit is contained in:
@@ -11,8 +11,8 @@ export async function POST(request: NextRequest) {
|
||||
};
|
||||
const { email, name, message } = body;
|
||||
|
||||
const user = process.env.NEXT_PUBLIC_MY_EMAIL ?? "";
|
||||
const pass = process.env.NEXT_PUBLIC_MY_PASSWORD ?? "";
|
||||
const user = process.env.MY_EMAIL ?? "";
|
||||
const pass = process.env.MY_PASSWORD ?? "";
|
||||
|
||||
if (!user || !pass) {
|
||||
console.error("Missing email/password environment variables");
|
||||
|
||||
@@ -1,25 +1,54 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import http from "http";
|
||||
import fetch from "node-fetch";
|
||||
import NodeCache from "node-cache";
|
||||
|
||||
export const runtime = "nodejs"; // Force Node runtime
|
||||
|
||||
const GHOST_API_URL = process.env.NEXT_PUBLIC_GHOST_API_URL;
|
||||
const GHOST_API_KEY = process.env.NEXT_PUBLIC_GHOST_API_KEY;
|
||||
const GHOST_API_URL = process.env.GHOST_API_URL;
|
||||
const GHOST_API_KEY = process.env.GHOST_API_KEY;
|
||||
const cache = new NodeCache({ stdTTL: 300 }); // Cache für 5 Minuten
|
||||
|
||||
type GhostPost = {
|
||||
slug: string;
|
||||
id: string;
|
||||
title: string;
|
||||
feature_image: string;
|
||||
visibility: string;
|
||||
published_at: string;
|
||||
updated_at: string;
|
||||
html: string;
|
||||
reading_time: number;
|
||||
meta_description: string;
|
||||
};
|
||||
|
||||
type GhostPostsResponse = {
|
||||
posts: Array<GhostPost>;
|
||||
};
|
||||
|
||||
export async function GET() {
|
||||
const cacheKey = "ghostPosts";
|
||||
const cachedPosts = cache.get<GhostPostsResponse>(cacheKey);
|
||||
|
||||
if (cachedPosts) {
|
||||
return NextResponse.json(cachedPosts);
|
||||
}
|
||||
|
||||
try {
|
||||
const agent = new http.Agent({ keepAlive: true });
|
||||
const response = await fetch(
|
||||
`${GHOST_API_URL}/ghost/api/content/posts/?key=${GHOST_API_KEY}&limit=all`,
|
||||
{ agent: agent as unknown as undefined }
|
||||
);
|
||||
if (!response.ok) {
|
||||
console.error(`Failed to fetch posts: ${response.statusText}`);
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
const posts = await response.json();
|
||||
const posts: GhostPostsResponse = await response.json() as GhostPostsResponse;
|
||||
|
||||
if (!posts || !posts.posts) {
|
||||
console.error("Invalid posts data");
|
||||
return NextResponse.json([]);
|
||||
}
|
||||
|
||||
cache.set(cacheKey, posts); // Daten im Cache speichern
|
||||
|
||||
return NextResponse.json(posts);
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch posts from Ghost:", error);
|
||||
|
||||
@@ -2,8 +2,8 @@ import { NextResponse } from "next/server";
|
||||
|
||||
export const runtime = "nodejs"; // Force Node runtime
|
||||
|
||||
const GHOST_API_URL = process.env.NEXT_PUBLIC_GHOST_API_URL;
|
||||
const GHOST_API_KEY = process.env.NEXT_PUBLIC_GHOST_API_KEY;
|
||||
const GHOST_API_URL = process.env.GHOST_API_URL;
|
||||
const GHOST_API_KEY = process.env.GHOST_API_KEY;
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
@@ -7,11 +7,15 @@ dotenv.config({ path: path.resolve(__dirname, '.env') });
|
||||
|
||||
const nextConfig: NextConfig = {
|
||||
env: {
|
||||
NEXT_PUBLIC_GHOST_API_KEY: process.env.NEXT_PUBLIC_GHOST_API_KEY,
|
||||
NEXT_PUBLIC_GHOST_API_URL: process.env.NEXT_PUBLIC_GHOST_API_URL,
|
||||
NEXT_PUBLIC_MY_EMAIL: process.env.NEXT_PUBLIC_MY_EMAIL,
|
||||
NEXT_PUBLIC_MY_PASSWORD: process.env.NEXT_PUBLIC_MY_PASSWORD,
|
||||
NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL,
|
||||
NEXT_PUBLIC_BASE_URL: process.env.NEXT_PUBLIC_BASE_URL
|
||||
},
|
||||
serverRuntimeConfig: {
|
||||
GHOST_API_URL: process.env.GHOST_API_URL,
|
||||
GHOST_API_KEY: process.env.GHOST_API_KEY,
|
||||
MY_EMAIL: process.env.MY_EMAIL,
|
||||
MY_INFO_EMAIL: process.env.MY_INFO_EMAIL,
|
||||
MY_PASSWORD: process.env.MY_PASSWORD,
|
||||
MY_INFO_PASSWORD: process.env.MY_INFO_PASSWORD
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
22
package-lock.json
generated
22
package-lock.json
generated
@@ -15,6 +15,7 @@
|
||||
"dotenv": "^16.4.7",
|
||||
"gray-matter": "^4.0.3",
|
||||
"next": "15.1.7",
|
||||
"node-cache": "^5.1.2",
|
||||
"node-fetch": "^3.3.2",
|
||||
"nodemailer": "^6.10.0",
|
||||
"react": "^19.0.0",
|
||||
@@ -3631,6 +3632,15 @@
|
||||
"node": ">=12"
|
||||
}
|
||||
},
|
||||
"node_modules/clone": {
|
||||
"version": "2.1.2",
|
||||
"resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz",
|
||||
"integrity": "sha512-3Pe/CF1Nn94hyhIYpjtiLhdCoEoz0DqQ+988E9gmeEdQZlojxnOb74wctFyuwWQHzqyf9X7C7MG8juUpqBJT8w==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/co": {
|
||||
"version": "4.6.0",
|
||||
"resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz",
|
||||
@@ -8039,6 +8049,18 @@
|
||||
"node": "^10 || ^12 || >=14"
|
||||
}
|
||||
},
|
||||
"node_modules/node-cache": {
|
||||
"version": "5.1.2",
|
||||
"resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz",
|
||||
"integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"clone": "2.x"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/node-domexception": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
"dotenv": "^16.4.7",
|
||||
"gray-matter": "^4.0.3",
|
||||
"next": "15.1.7",
|
||||
"node-cache": "^5.1.2",
|
||||
"node-fetch": "^3.3.2",
|
||||
"nodemailer": "^6.10.0",
|
||||
"react": "^19.0.0",
|
||||
|
||||
Reference in New Issue
Block a user