forked from mito-systems/sol-mem-gen
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { createServer } from 'http';
|
|
import { parse } from 'url';
|
|
import next from 'next';
|
|
|
|
// Reference: https://github.com/motdotla/dotenv?tab=readme-ov-file#how-do-i-use-dotenv-with-import
|
|
import 'dotenv/config'
|
|
|
|
import { QuotesService } from './quotes-service';
|
|
|
|
const port = parseInt(process.env.PORT || '3000', 10);
|
|
const app = next({ dev: process.env.NODE_ENV !== 'production' });
|
|
const handle = app.getRequestHandler();
|
|
|
|
const quotesService = new QuotesService();
|
|
|
|
declare global {
|
|
namespace NodeJS {
|
|
interface Global {
|
|
quotesService: typeof quotesService
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO: Look for a better way to use quotesService
|
|
// Initialize global quotes service
|
|
(global as any).quotesService = quotesService
|
|
|
|
app.prepare().then(async() => {
|
|
const server = createServer(async (req, res) => {
|
|
const parsedUrl = parse(req.url!, true);
|
|
|
|
handle(req, res, parsedUrl);
|
|
});
|
|
|
|
await quotesService.fetchAndCacheQuotes(); // Initial store
|
|
|
|
server.listen(port, () => {
|
|
console.log(`> Server listening at http://localhost:${port}`);
|
|
});
|
|
|
|
// Interval setup
|
|
setInterval(async () => await quotesService.fetchAndCacheQuotes(), 5 * 60 * 1000); // Update cache every 5 minutes
|
|
});
|