sol-mem-gen/server.ts
adwait fa168e5c61 Add feature to share generated memes on twitter and verify tweet (#10)
Part of https://www.notion.so/Option-to-post-paid-for-memes-to-twitter-x-18ca6b22d4728051804ef4f55065d5ba

- Generate dynamic page with required img and meta tags
- Add button to share generated meme to twitter

Co-authored-by: Adw8 <adwaitgharpure@gmail.com>
Co-authored-by: IshaVenikar <ishavenikar7@gmail.com>
Co-authored-by: AdityaSalunkhe21 <adityasalunkhe2204@gmail.com>
Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
Reviewed-on: #10
Co-authored-by: adwait <adwait@noreply.git.vdb.to>
Co-committed-by: adwait <adwait@noreply.git.vdb.to>
2025-02-06 05:45:02 +00:00

57 lines
1.5 KiB
TypeScript

import { createServer } from 'http';
import { parse } from 'url';
import next from 'next';
import { DataSource, EntityTarget } from 'typeorm';
// 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';
import { initializeDataSource } from './src/data-source';
import { Payment } from './src/entity/Payment';
import { Tweet } from './src/entity/Tweet';
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: QuotesService
appDataSource: DataSource
entities: { [key: string]: EntityTarget<any>}
}
}
}
// TODO: Look for a better way to use quotesService
// Initialize global quotes service
global.quotesService = quotesService
global.entities = {
Payment,
Tweet
};
app.prepare().then(async() => {
global.appDataSource = await initializeDataSource();
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
});