sol-mem-gen/server.ts
adwait 36f298ff96 Use MTM price based on USDC for meme generation (#7)
Part of https://www.notion.so/Use-MTM-price-based-on-pool-189a6b22d47280ff9373c89f40b378d1

Co-authored-by: Adw8 <adwaitgharpure@gmail.com>
Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
Co-authored-by: Adwait Gharpure <adwaitgharpure@gmail.com>
Reviewed-on: #7
Co-authored-by: adwait <adwait@noreply.git.vdb.to>
Co-committed-by: adwait <adwait@noreply.git.vdb.to>
2025-01-30 15:03:09 +00:00

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
});