UNPKG

@anyme/anymejs

Version:
97 lines (94 loc) 3.18 kB
import { __decorate, __param, __metadata } from '../_virtual/_tslib.js'; import { GracefulExit } from '../utils/graceful-exit.js'; import { injectable, inject } from 'inversify'; import { SYMBOLS } from '../utils/constants.js'; let Anyme = class Anyme { config; logger; service; redis; dataSource; gracefulExit; constructor(config, logger, service, redis, dataSource) { this.config = config; this.logger = logger; this.service = service; this.redis = redis; this.dataSource = dataSource; this.gracefulExit = new GracefulExit(this.logger); } async bootstrap(port) { try { await this.initialize(); const { port: prt, server, scheme } = await this.service.createServer(); await this.service.applySocket(server); server.listen(port ?? prt, () => { this.logger.info(`🚀 Server running on ${scheme}://localhost:${port ?? prt}`); }); this.gracefulExit.register(server, { healthCheck: { "/health": async () => ({ timestamp: new Date().toLocaleString(), }), }, }); return server; } catch (error) { this.logger.error("❌ Failed to start server:", error); throw error; } } async initialize() { try { await Promise.all([this.initDatabase(), this.initRedis()]); await this.service.applyLimiter(); await this.service.applySession(); await this.service.applySSE(); await this.service.applyRoute(); } catch (error) { this.logger.error("❌ Failed to initialize", error); throw error; } } async initDatabase() { try { const { db } = await this.config.get(); if (!db.enable) return; const result = await this.dataSource.connectAll(); if (result.length > 0) this.gracefulExit.addCleanupTask(() => this.dataSource.closeAll()); } catch (error) { this.logger.error("❌ Failed to connect to database", error); throw error; } } async initRedis() { try { const { redis } = await this.config.get(); if (!redis.enable) return; const result = await this.redis.connectAll(); if (result.length > 0) this.gracefulExit.addCleanupTask(() => this.redis.closeAll()); } catch (error) { this.logger.error("❌ Failed to init Redis", error); throw error; } } }; Anyme = __decorate([ injectable("Singleton"), __param(0, inject(SYMBOLS.Config)), __param(1, inject(SYMBOLS.Logger)), __param(2, inject(SYMBOLS.Service)), __param(3, inject(SYMBOLS.Redis)), __param(4, inject(SYMBOLS.DB)), __metadata("design:paramtypes", [Object, Function, Function, Object, Object]) ], Anyme); export { Anyme }; //# sourceMappingURL=app.js.map