diff --git a/server.ts b/server.ts index fceaefe..16d841e 100644 --- a/server.ts +++ b/server.ts @@ -10,6 +10,22 @@ const port = parseInt(process.env.PORT || '3000', 10); const app = next({ dev: process.env.NODE_ENV !== 'production' }); const handle = app.getRequestHandler(); +declare global { + namespace NodeJS { + interface Global { + quoteService: { + getLatestQuote: typeof getLatestQuote; + } + } + } +} + +// TODO: Look for a better way to use quoteService +// Initialize the global quote service +(global as any).quoteService = { + getLatestQuote +}; + app.prepare().then(() => { const server = createServer(async (req, res) => { const parsedUrl = parse(req.url!, true); @@ -27,5 +43,3 @@ app.prepare().then(() => { fetchAndCacheQuotes(); // Initial store setInterval(fetchAndCacheQuotes, 5 * 60 * 1000); // Update cache every 5 minutes }); - -export { getLatestQuote }; diff --git a/src/app/api/quotes/route.ts b/src/app/api/quotes/route.ts new file mode 100644 index 0000000..ee8f78d --- /dev/null +++ b/src/app/api/quotes/route.ts @@ -0,0 +1,6 @@ +import { NextRequest, NextResponse } from 'next/server'; + +export async function GET(req: NextRequest) { + const quote = (global as any).quoteService.getLatestQuote(); + return NextResponse.json(quote); +}