Skip to content

Commit 938dbfb

Browse files
committed
feat(config): implement singleton pattern and load environment variables
- Added Config class with singleton pattern to manage bot token, environment, and database configuration. - Environment variables are validated and used to configure the bot and database connection. - Throws an error if the bot token is missing from environment variables.
1 parent cde8ef1 commit 938dbfb

1 file changed

Lines changed: 47 additions & 44 deletions

File tree

src/config/index.ts

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
1-
import { ApprovedUser } from "../entities/ApprovedUser";
2-
import { GroupSettings } from "../entities/GroupSettings";
3-
import { User } from "../entities/User";
4-
import { Warning } from "../entities/Warning";
5-
import { EntityType, Environment } from "../types";
6-
const entities: EntityType[] = [User, Warning, GroupSettings, ApprovedUser];
7-
export const config: Record<
8-
Environment,
9-
{
10-
type: string;
11-
url?: string;
12-
host?: string;
13-
port?: number;
14-
username?: string;
15-
password?: string;
16-
database?: string;
17-
entities: EntityType[];
18-
synchronize: boolean;
19-
logging: boolean;
1+
import { DatabaseConfig } from '../types/ResponseTypes';
2+
3+
class Config {
4+
private static _instance: Config | null = null;
5+
public token: string;
6+
public environment: 'development' | 'production';
7+
public database: DatabaseConfig;
8+
9+
private constructor() {
10+
// Ensure that the token is available in the environment
11+
const token = process.env.TELEGRAM_BOT_TOKEN;
12+
if (!token) {
13+
throw new Error('Telegram bot token is missing. Please set TELEGRAM_BOT_TOKEN in the environment.');
14+
}
15+
16+
// Set the environment, defaulting to development if not set
17+
const environment: 'development' | 'production' = process.env.NODE_ENV === 'production' ? 'production' : 'development';
18+
19+
// Database configuration with environment variables
20+
const dbUser = process.env.DB_USER!;
21+
const dbHost = process.env.DB_HOST!;
22+
const dbName = process.env.DB_NAME!;
23+
const dbPassword = process.env.DB_PASSWORD!;
24+
const dbPort = parseInt(process.env.DB_PORT!, 10);
25+
const dbUrl = process.env.DB_URL!;
26+
27+
this.token = token;
28+
this.environment = environment;
29+
// Initialize the database configuration
30+
this.database = {
31+
user: dbUser,
32+
host: dbHost,
33+
databaseName: dbName,
34+
password: dbPassword,
35+
port: dbPort,
36+
url: dbUrl,
37+
};
38+
}
39+
40+
// Singleton pattern to ensure only one instance of the Config class
41+
public static getInstance(): Config {
42+
if (!Config._instance) {
43+
Config._instance = new Config();
44+
}
45+
return Config._instance;
2046
}
21-
> = {
22-
production: {
23-
type: "mysql",
24-
url: process.env.MYSQL_URL,
25-
entities: entities,
26-
synchronize: true,
27-
logging: false,
28-
},
29-
development: {
30-
type: "mysql",
31-
host: process.env.DB_HOST || "localhost",
32-
port: parseInt(process.env.DB_PORT || "3306", 10),
33-
username: process.env.DB_USERNAME || "root",
34-
password: process.env.DB_PASSWORD || "",
35-
database: process.env.DB_NAME || "test",
36-
entities: entities,
37-
synchronize: true,
38-
logging: false,
39-
},
40-
};
47+
}
4148

42-
export const getAppDataSourceConfig = () => {
43-
const env: Environment =
44-
(process.env.NODE_ENV as Environment) || "development";
45-
return config[env];
46-
};
49+
export default Config.getInstance();

0 commit comments

Comments
 (0)