-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_env.py
More file actions
91 lines (73 loc) · 3.21 KB
/
Copy pathset_env.py
File metadata and controls
91 lines (73 loc) · 3.21 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Script to help you set MongoDB environment variables
Run this script to easily update your MongoDB configuration
"""
import os
def set_environment_variables():
"""Interactive script to set MongoDB environment variables"""
print("🔧 MongoDB Environment Variable Setup")
print("=" * 50)
print("\nCurrent environment variables:")
print(f"MONGO_URI: {os.getenv('MONGO_URI', 'Not set')}")
print(f"DB_NAME: {os.getenv('DB_NAME', 'Not set')}")
print(f"USE_LOCAL: {os.getenv('USE_LOCAL', 'Not set')}")
print("\nOptions:")
print("1. Set new Atlas credentials")
print("2. Switch to local MongoDB")
print("3. View current config")
print("4. Exit")
choice = input("\nEnter your choice (1-4): ").strip()
if choice == "1":
print("\n📡 Setting up MongoDB Atlas connection...")
print("Get your connection string from MongoDB Atlas:")
print("1. Go to Database → Connect")
print("2. Choose 'Connect your application'")
print("3. Copy the connection string")
mongo_uri = input("\nEnter your MongoDB URI: ").strip()
db_name = input("Enter your database name: ").strip()
if mongo_uri and db_name:
# Set environment variables for current session
os.environ['MONGO_URI'] = mongo_uri
os.environ['DB_NAME'] = db_name
os.environ['USE_LOCAL'] = 'false'
print("\n✅ Environment variables set for current session!")
print("To make them permanent, add these to your system environment variables:")
print(f"MONGO_URI={mongo_uri}")
print(f"DB_NAME={db_name}")
print(f"USE_LOCAL=false")
elif choice == "2":
print("\n🏠 Switching to local MongoDB...")
os.environ['USE_LOCAL'] = 'true'
print("✅ Switched to local MongoDB mode!")
print("Make sure MongoDB is running locally on port 27017")
elif choice == "3":
print("\n📋 Current configuration:")
print(f"MONGO_URI: {os.getenv('MONGO_URI', 'Not set')}")
print(f"DB_NAME: {os.getenv('DB_NAME', 'Not set')}")
print(f"USE_LOCAL: {os.getenv('USE_LOCAL', 'Not set')}")
elif choice == "4":
print("👋 Goodbye!")
return
else:
print("❌ Invalid choice. Please try again.")
set_environment_variables()
def test_connection():
"""Test the current configuration"""
print("\n🧪 Testing connection with current configuration...")
try:
from config import MONGO_URI, DB_NAME, USE_LOCAL
print(f"Using URI: {MONGO_URI[:50]}...")
print(f"Using Database: {DB_NAME}")
print(f"Local mode: {USE_LOCAL}")
# Test the connection
from db_layer.connection import client
client.admin.command('ping')
print("✅ Connection successful!")
except Exception as e:
print(f"❌ Connection failed: {e}")
if __name__ == "__main__":
set_environment_variables()
# Ask if user wants to test connection
test_choice = input("\nWould you like to test the connection? (y/n): ").strip().lower()
if test_choice == 'y':
test_connection()