sol-mem-gen/server.ts

32 lines
917 B
TypeScript

import { createServer } from 'http';
import { parse } from 'url';
import next from 'next';
import dotenv from 'dotenv';
dotenv.config();
import { fetchAndCacheQuotes, getLatestQuote } from './cache';
const port = parseInt(process.env.PORT || '3000', 10);
const app = next({ dev: process.env.NODE_ENV !== 'production' });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = createServer(async (req, res) => {
const parsedUrl = parse(req.url!, true);
(req as any).getLatestQuote = getLatestQuote;
// Default Next.js request handling
handle(req, res, parsedUrl);
});
server.listen(port, () => {
console.log(`> Server listening at http://localhost:${port}`);
});
// Initial call and interval setup
fetchAndCacheQuotes(); // Initial store
setInterval(fetchAndCacheQuotes, 5 * 60 * 1000); // Update cache every 5 minutes
});
export { getLatestQuote };