-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.ts
62 lines (54 loc) · 2.3 KB
/
init.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import {Connection, Types} from 'mongoose';
import { MongooseStripeContainerSingleton, connectToDb, disconnectFromDb, resolveConfigReader, resolveStripeClient, MongooseDaoFactory } from '@user-credits/stripe-mongoose';
import { ServiceProxy } from '../lib/server/rest/ServiceProxy';
import { AwilixContainer } from "awilix/lib/container";
import type { IService } from '@user-credits/core';
import { PaymentService } from '@user-credits/core';
import type { IConfigReader } from '@user-credits/stripe-mongoose';
import { asValue } from 'awilix';
let ioc: AwilixContainer<object>;
let iConfigReader: IConfigReader;
let service: IService<Types.ObjectId>;
let serviceProxy: ServiceProxy<Types.ObjectId>;
let initializationPromise = null;
let isInitializing = false;
export function isInitialized(): boolean {
return ioc != null;
}
async function init(): Promise<void> {
console.log('Initializing container...');
iConfigReader = await resolveConfigReader();
ioc = await MongooseStripeContainerSingleton.getInstance() as unknown as AwilixContainer<object>;
const connection: Connection = await connectToDb(iConfigReader.dbUrl, iConfigReader.dbName);
// Connect to MongoDB
const mongooseDaoFactory = new MongooseDaoFactory(connection);
const paymentClient = await resolveStripeClient();
service = new PaymentService(mongooseDaoFactory, paymentClient, iConfigReader.currency ?? 'usd') as unknown as IService<Types.ObjectId>;
ioc.register({ service: asValue(service) });
serviceProxy = new ServiceProxy<Types.ObjectId>(service);
ioc.register({ serviceProxy: asValue(serviceProxy) });
console.log('Connected to MongoDB');
}
export async function beforeStartup() {
if (!isInitialized() && !isInitializing) {
isInitializing = true;
// Create a new promise only if it's not already in progress
initializationPromise = init().then(() => {
// Reset the initialization flag once initialization is complete
isInitializing = false;
}).catch((err)=>console.error(err));
}
// Wait for the initialization to complete, even if it's in progress
await initializationPromise;
}
export async function onShutDown() {
if(isInitialized()) {
ioc = null;
iConfigReader = null;
service = null;
serviceProxy = null;
// Disconnect from MongoDB on server shutdown
await disconnectFromDb();
console.log('Disconnected from MongoDB');
}
}