Skip to content

Commit 0ab0424

Browse files
committed
feat: "Added environment variable checks and initialized config properties in Config class"
1 parent e24fc93 commit 0ab0424

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

.env.example

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Telegram Bot Token (Set your Telegram bot token here)
2+
TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here
3+
4+
# Database Configuration (Local)
5+
# Set your local database credentials here
6+
DB_USER=your_db_user_here
7+
DB_PASSWORD=your_db_password_here
8+
DB_HOST=localhost
9+
DB_NAME=your_db_name_here
10+
DB_PORT=5432
11+
DB_URL=postgres://your_db_user_here:your_db_password_here@localhost:5432/your_db_name_here
12+
13+
# Database Configuration (Remote/Production)
14+
# Set your production database URL here
15+
DATABASE_URL=postgresql://your_db_user_here:your_db_password_here@your_host_here:your_port_here/your_db_name_here
16+
17+
# Application Configuration
18+
# Set the port where your application will run
19+
PORT=3000
20+
21+
# Set your web hook URL here (for webhook integration with Telegram)
22+
WEB_HOOK=https://your-webhook-url-here
23+
24+
# Environment
25+
# Set to 'production' in production environment, 'development' for local development
26+
NODE_ENV=development

src/config/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@ class Config {
1111
private constructor() {
1212
// Ensure that the token is available in the environment
1313
const token = process.env.TELEGRAM_BOT_TOKEN;
14-
const web_hook = process.env.WEB_HOOK!;
14+
const web_hook = process.env.WEB_HOOK;
15+
const port = process.env.PORT;
16+
const nodeEnv = process.env.NODE_ENV || 'development';
17+
if (!token) {
18+
throw new Error('Telegram bot token is missing. Please set TELEGRAM_BOT_TOKEN in the environment.');
19+
}
20+
if (!web_hook) {
21+
throw new Error('Web hook URL is missing. Please set WEB_HOOK in the environment.');
22+
}
23+
1524
if (!token) {
1625
throw new Error('Telegram bot token is missing. Please set TELEGRAM_BOT_TOKEN in the environment.');
1726
}
18-
const port = process.env.PORT!;
1927
// Set the environment, defaulting to development if not set
20-
const environment: 'development' | 'production' = process.env.NODE_ENV === 'production' ? 'production' : 'development';
28+
const environment: 'development' | 'production' = nodeEnv === 'production' ? 'production' : 'development';
2129

2230
// Database configuration with environment variables
2331
const dbUser = process.env.DB_USER!;
@@ -26,6 +34,8 @@ class Config {
2634
const dbPassword = process.env.DB_PASSWORD!;
2735
const dbPort = parseInt(process.env.DB_PORT!, 10);
2836
const dbUrl = environment === 'production' ? process.env.DATABASE_URL! : process.env.DB_URL!;
37+
38+
// Initialize the config properties
2939
this.token = token;
3040
this.environment = environment;
3141
this.port = Number(port);

0 commit comments

Comments
 (0)