2026-05-18 07:14:35 +07:00
|
|
|
import { serve } from 'bun';
|
2026-07-28 18:12:48 +07:00
|
|
|
import { config } from './config/index';
|
2026-07-28 20:09:42 +07:00
|
|
|
import { fileInfoCache } from './infrastructure/cache/index';
|
2026-07-28 18:12:48 +07:00
|
|
|
import { startBot } from './interfaces/bot/handler';
|
2026-07-28 20:09:42 +07:00
|
|
|
import { handleS3Request } from './interfaces/http/controllers/s3-controller';
|
|
|
|
|
import { cleanupRateLimitCache } from './interfaces/http/middleware/rate-limit';
|
2026-07-28 18:12:48 +07:00
|
|
|
import { routes } from './interfaces/http/routes/index';
|
|
|
|
|
import { isS3Request } from './interfaces/s3/auth';
|
|
|
|
|
import { extractS3BucketFromHost } from './interfaces/s3/virtual-host';
|
|
|
|
|
import { logger } from './shared/logger/index';
|
|
|
|
|
import { metricsCollector } from './shared/metrics/index';
|
2026-05-18 07:14:35 +07:00
|
|
|
|
2026-07-06 22:32:06 +07:00
|
|
|
// ─── Auto-run migration at startup ──────────────────────────────────────────
|
|
|
|
|
try {
|
|
|
|
|
const { runMigration } = await import('./db/migrate');
|
|
|
|
|
await runMigration();
|
|
|
|
|
} catch {
|
|
|
|
|
logger.warn('Auto-migration skipped (non-fatal)');
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-07 05:39:23 +07:00
|
|
|
const getS3RouteBucket = (req: Request): string | null => {
|
|
|
|
|
const host = req.headers.get('host') || '';
|
|
|
|
|
return extractS3BucketFromHost(host, config.s3VhostDomains);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const shouldHandleS3 = (req: Request, headers: Record<string, string>): boolean => {
|
|
|
|
|
const url = new URL(req.url);
|
|
|
|
|
return Boolean(
|
|
|
|
|
getS3RouteBucket(req) || isS3Request(headers) || url.searchParams.has('X-Amz-Signature'),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-28 20:09:42 +07:00
|
|
|
const _handleMaybeS3Root = (req: Request): Response | Promise<Response> => {
|
2026-07-07 05:39:23 +07:00
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
|
return handleS3Request(req, getS3RouteBucket(req));
|
|
|
|
|
}
|
|
|
|
|
const headers = Object.fromEntries(req.headers);
|
|
|
|
|
if (shouldHandleS3(req, headers)) {
|
|
|
|
|
return handleS3Request(req, getS3RouteBucket(req));
|
|
|
|
|
}
|
|
|
|
|
return new Response('Not Allowed', { status: 405 });
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-18 07:14:35 +07:00
|
|
|
const server = serve({
|
|
|
|
|
port: config.port,
|
2026-07-28 18:12:48 +07:00
|
|
|
routes,
|
2026-07-06 20:56:55 +07:00
|
|
|
fetch: async (req: Request) => {
|
2026-07-07 05:39:23 +07:00
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
|
return handleS3Request(req, getS3RouteBucket(req));
|
|
|
|
|
}
|
2026-07-06 20:56:55 +07:00
|
|
|
const headers = Object.fromEntries(req.headers);
|
2026-07-07 05:39:23 +07:00
|
|
|
if (shouldHandleS3(req, headers)) {
|
|
|
|
|
return handleS3Request(req, getS3RouteBucket(req));
|
2026-07-06 20:56:55 +07:00
|
|
|
}
|
|
|
|
|
return new Response('Not Found', { status: 404 });
|
2026-05-18 07:29:01 +07:00
|
|
|
},
|
2026-05-18 07:14:35 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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 });
|
|
|
|
|
|
2026-07-29 08:40:50 +07:00
|
|
|
logger.info('Closing HTTP server — no new requests accepted');
|
2026-05-18 07:14:35 +07:00
|
|
|
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'));
|
|
|
|
|
|
2026-07-06 01:25:18 +07:00
|
|
|
// Periodic maintenance intervals
|
2026-05-18 07:14:35 +07:00
|
|
|
setInterval(cleanupRateLimitCache, 60000);
|
2026-07-06 01:25:18 +07:00
|
|
|
setInterval(
|
|
|
|
|
() => {
|
|
|
|
|
const removed = fileInfoCache.cleanup();
|
|
|
|
|
if (removed > 0) {
|
|
|
|
|
logger.info(`Cleaned up ${removed} expired cache entries`);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
5 * 60 * 1000,
|
|
|
|
|
);
|
|
|
|
|
setInterval(
|
|
|
|
|
() => {
|
|
|
|
|
const snapshot = metricsCollector.getSnapshot();
|
|
|
|
|
logger.info('Metrics snapshot', {
|
|
|
|
|
uploadLatency: snapshot.uploadLatency,
|
|
|
|
|
uploadThroughput: snapshot.uploadThroughput.toFixed(2),
|
|
|
|
|
errorRate: snapshot.errorRate.toFixed(2),
|
|
|
|
|
cacheHitRate: snapshot.cacheHitRate.toFixed(2),
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
5 * 60 * 1000,
|
|
|
|
|
);
|
2026-05-18 07:14:35 +07:00
|
|
|
|
2026-07-28 20:09:42 +07:00
|
|
|
logger.info('Application running successfully');
|