2026-05-18 07:14:35 +07:00
|
|
|
import { serve } from 'bun';
|
2026-05-18 07:27:06 +07:00
|
|
|
import logger from './utils/logger';
|
|
|
|
|
import { config } from './env';
|
|
|
|
|
import { startBot } from './bot';
|
|
|
|
|
import { handleUpload } from './routes/upload';
|
|
|
|
|
import { handleFileRedirect, handleFileInfo } from './routes/files';
|
|
|
|
|
import { handleHealth } from './routes/health';
|
|
|
|
|
import { cleanupRateLimitCache } from './utils/rateLimit';
|
2026-05-18 07:14:35 +07:00
|
|
|
|
|
|
|
|
const server = serve({
|
|
|
|
|
port: config.port,
|
|
|
|
|
routes: {
|
|
|
|
|
'/api/upload': {
|
|
|
|
|
POST: handleUpload
|
|
|
|
|
},
|
|
|
|
|
'/f/:public_id': {
|
|
|
|
|
GET: handleFileRedirect
|
|
|
|
|
},
|
|
|
|
|
'/file/:public_id/info': {
|
|
|
|
|
GET: handleFileInfo
|
|
|
|
|
},
|
|
|
|
|
'/health': {
|
|
|
|
|
GET: handleHealth
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const bot = await startBot();
|
|
|
|
|
|
|
|
|
|
logger.info('Server started', { port: config.port, url: config.baseUrl });
|
|
|
|
|
|
2026-05-18 07:27:06 +07:00
|
|
|
const gracefulShutdown = async (signal: string): Promise<void> => {
|
2026-05-18 07:14:35 +07:00
|
|
|
logger.info('Graceful shutdown signal received', { signal });
|
|
|
|
|
|
|
|
|
|
logger.info('Closing HTTP server');
|
|
|
|
|
server.stop();
|
|
|
|
|
|
|
|
|
|
logger.info('Stopping Telegram bot');
|
2026-05-18 07:27:06 +07:00
|
|
|
bot.stop(signal);
|
2026-05-18 07:14:35 +07:00
|
|
|
|
|
|
|
|
logger.info('Server shutdown complete');
|
|
|
|
|
process.exit(0);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
|
|
|
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
|
|
|
|
|
|
|
|
setInterval(cleanupRateLimitCache, 60000);
|
|
|
|
|
|
|
|
|
|
logger.info('Application running successfully');
|