0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00
logto/packages/core/src/index.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

import { trySafe } from '@silverhand/essentials';
2023-01-07 17:22:15 +08:00
import dotenv from 'dotenv';
import { findUp } from 'find-up';
2021-08-30 11:30:54 +08:00
import Koa from 'koa';
2021-07-11 17:59:44 +08:00
2023-02-09 18:31:14 +08:00
import { checkAlterationState } from './env-set/check-alteration-state.js';
import SystemContext from './tenants/SystemContext.js';
2023-02-09 18:31:14 +08:00
2023-01-07 17:22:15 +08:00
dotenv.config({ path: await findUp('.env', {}) });
const { appInsights } = await import('@logto/app-insights/node');
if (await appInsights.setup('logto')) {
console.debug('Initialized ApplicationInsights');
}
// Import after env has been configured
const { loadConnectorFactories } = await import('./utils/connectors/index.js');
2023-01-12 16:37:21 +08:00
const { EnvSet } = await import('./env-set/index.js');
const { redisCache } = await import('./caches/index.js');
const { default: initI18n } = await import('./i18n/init.js');
const { tenantPool, checkRowLevelSecurity } = await import('./tenants/index.js');
2023-01-07 17:22:15 +08:00
try {
const app = new Koa({
proxy: EnvSet.values.trustProxyHeader,
});
const sharedAdminPool = await EnvSet.sharedPool;
2023-02-09 18:31:14 +08:00
await Promise.all([
initI18n(),
redisCache.connect(),
loadConnectorFactories(),
checkRowLevelSecurity(sharedAdminPool),
checkAlterationState(sharedAdminPool),
SystemContext.shared.loadStorageProviderConfig(sharedAdminPool),
2023-02-09 18:31:14 +08:00
]);
// Import last until init completed
const { default: initApp } = await import('./app/init.js');
await initApp(app);
} catch (error: unknown) {
console.error('Error while initializing app:');
console.error(error);
await Promise.all([trySafe(tenantPool.endAll()), trySafe(redisCache.disconnect())]);
}