2023-04-07 01:13:15 +08:00
|
|
|
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';
|
2023-03-07 14:00:12 +08:00
|
|
|
import SystemContext from './tenants/SystemContext.js';
|
2023-04-11 01:48:19 +08:00
|
|
|
import { consoleLog } from './utils/console.js';
|
2023-02-09 18:31:14 +08:00
|
|
|
|
2023-01-07 17:22:15 +08:00
|
|
|
dotenv.config({ path: await findUp('.env', {}) });
|
|
|
|
|
2023-04-03 15:40:56 +08:00
|
|
|
const { appInsights } = await import('@logto/app-insights/node');
|
2023-03-30 13:36:46 +08:00
|
|
|
|
2023-04-19 20:39:37 +08:00
|
|
|
if (await appInsights.setup('core')) {
|
2023-04-11 01:48:19 +08:00
|
|
|
consoleLog.info('Initialized ApplicationInsights');
|
2023-03-30 13:36:46 +08:00
|
|
|
}
|
2023-03-18 02:11:28 +08:00
|
|
|
|
2023-02-06 18:44:34 +08:00
|
|
|
// Import after env has been configured
|
2023-03-14 20:26:42 +08:00
|
|
|
const { loadConnectorFactories } = await import('./utils/connectors/index.js');
|
2023-01-12 16:37:21 +08:00
|
|
|
const { EnvSet } = await import('./env-set/index.js');
|
2023-04-07 01:13:15 +08:00
|
|
|
const { redisCache } = await import('./caches/index.js');
|
2023-01-18 20:38:05 +08:00
|
|
|
const { default: initI18n } = await import('./i18n/init.js');
|
|
|
|
const { tenantPool, checkRowLevelSecurity } = await import('./tenants/index.js');
|
2023-01-07 17:22:15 +08:00
|
|
|
|
2022-12-23 19:09:09 +08:00
|
|
|
try {
|
|
|
|
const app = new Koa({
|
2023-01-11 16:41:53 +08:00
|
|
|
proxy: EnvSet.values.trustProxyHeader,
|
2022-12-23 19:09:09 +08:00
|
|
|
});
|
2023-03-13 12:01:14 +08:00
|
|
|
const sharedAdminPool = await EnvSet.sharedPool;
|
2023-04-07 01:13:15 +08:00
|
|
|
|
2023-02-09 18:31:14 +08:00
|
|
|
await Promise.all([
|
2023-04-07 01:13:15 +08:00
|
|
|
initI18n(),
|
|
|
|
redisCache.connect(),
|
|
|
|
loadConnectorFactories(),
|
2023-03-13 12:01:14 +08:00
|
|
|
checkRowLevelSecurity(sharedAdminPool),
|
|
|
|
checkAlterationState(sharedAdminPool),
|
2023-04-07 01:13:15 +08:00
|
|
|
SystemContext.shared.loadStorageProviderConfig(sharedAdminPool),
|
2023-02-09 18:31:14 +08:00
|
|
|
]);
|
2023-01-18 20:38:05 +08:00
|
|
|
|
2022-12-23 19:09:09 +08:00
|
|
|
// Import last until init completed
|
|
|
|
const { default: initApp } = await import('./app/init.js');
|
|
|
|
await initApp(app);
|
|
|
|
} catch (error: unknown) {
|
2023-04-11 01:48:19 +08:00
|
|
|
consoleLog.error('Error while initializing app:');
|
|
|
|
consoleLog.error(error);
|
2022-12-23 19:09:09 +08:00
|
|
|
|
2023-04-18 12:20:07 +08:00
|
|
|
void Promise.all([trySafe(tenantPool.endAll()), trySafe(redisCache.disconnect())]);
|
2022-12-23 19:09:09 +08:00
|
|
|
}
|