forked from msgflux/msgspec-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_dotenv_file.py
More file actions
80 lines (62 loc) · 2.03 KB
/
03_dotenv_file.py
File metadata and controls
80 lines (62 loc) · 2.03 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
"""
Loading settings from .env files.
This example shows how to load settings from .env files,
which is useful for local development and deployment.
"""
import tempfile
from pathlib import Path
from msgspec_ext import BaseSettings, SettingsConfigDict
class AppSettings(BaseSettings):
"""Application settings loaded from .env file."""
model_config = SettingsConfigDict(
env_file=".env.example", env_file_encoding="utf-8"
)
app_name: str
environment: str = "development"
api_key: str
database_url: str
max_connections: int = 100
enable_logging: bool = True
def main():
print("=" * 60)
print("Example 3: Loading from .env Files")
print("=" * 60)
print()
# Create a temporary .env file
env_content = """# Application Configuration
APP_NAME=my-awesome-app
ENVIRONMENT=production
API_KEY=sk-1234567890abcdef
DATABASE_URL=postgresql://user:pass@localhost:5432/mydb
MAX_CONNECTIONS=200
ENABLE_LOGGING=false
"""
env_file = Path(".env.example")
env_file.write_text(env_content)
try:
print("Created .env.example file:")
print("-" * 60)
print(env_content)
print("-" * 60)
print()
# Load settings from .env file
settings = AppSettings()
print("Loaded Settings:")
print(f" App Name: {settings.app_name}")
print(f" Environment: {settings.environment}")
print(f" API Key: {settings.api_key}")
print(f" Database URL: {settings.database_url}")
print(f" Max Connections: {settings.max_connections}")
print(f" Enable Logging: {settings.enable_logging}")
print()
print("✅ Settings loaded from .env file!")
print()
print("💡 Tips:")
print(" - Use .env.local for local overrides (add to .gitignore)")
print(" - Use .env.production for production settings")
print(" - Never commit secrets to version control")
finally:
# Clean up
env_file.unlink(missing_ok=True)
if __name__ == "__main__":
main()