-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
202 lines (169 loc) · 6.08 KB
/
Copy pathconfig.py
File metadata and controls
202 lines (169 loc) · 6.08 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MongoDB查询接口配置文件
"""
import os
from typing import Dict, Any
class Config:
"""配置类"""
# MongoDB默认连接配置
MONGODB_CONFIG = {
"default": {
"connection_string": os.getenv("MONGODB_URI", "mongodb://localhost:27017/"),
"database_name": os.getenv("MONGODB_DATABASE", "test_db"),
"collection_name": os.getenv("MONGODB_COLLECTION", "users"),
"server_selection_timeout_ms": 5000,
"connect_timeout_ms": 10000,
"socket_timeout_ms": 10000,
"max_pool_size": 10,
"min_pool_size": 1
},
# 开发环境配置
"development": {
"connection_string": "mongodb://localhost:27017/",
"database_name": "dev_db",
"collection_name": "users",
"server_selection_timeout_ms": 5000,
"connect_timeout_ms": 10000,
"socket_timeout_ms": 10000,
"max_pool_size": 5,
"min_pool_size": 1
},
# 生产环境配置
"production": {
"connection_string": os.getenv("MONGODB_URI", "mongodb://localhost:27017/"),
"database_name": os.getenv("MONGODB_DATABASE", "prod_db"),
"collection_name": os.getenv("MONGODB_COLLECTION", "users"),
"server_selection_timeout_ms": 10000,
"connect_timeout_ms": 20000,
"socket_timeout_ms": 20000,
"max_pool_size": 20,
"min_pool_size": 5
}
}
# Redis缓存配置
REDIS_CONFIG = {
"host": os.getenv("REDIS_HOST", "127.0.0.1"),
"port": int(os.getenv("REDIS_PORT", 31337)),
"db": int(os.getenv("REDIS_DB", 9)),
"password": os.getenv("REDIS_PASSWORD", 'lobbyredisLock527788'),
"default_ttl_seconds": 86400, # 默认缓存时间24小时 (24 * 60 * 60)
}
# API配置
API_CONFIG = {
"host": os.getenv("API_HOST", "0.0.0.0"),
"port": int(os.getenv("API_PORT", "8000")),
"debug": os.getenv("API_DEBUG", "False").lower() == "true",
"reload": os.getenv("API_RELOAD", "True").lower() == "true",
"log_level": os.getenv("API_LOG_LEVEL", "info")
}
# 日志配置
LOGGING_CONFIG = {
"level": os.getenv("LOG_LEVEL", "INFO"),
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
"file": os.getenv("LOG_FILE", "mongodb_api.log")
}
# 查询配置
QUERY_CONFIG = {
"default_limit": 100,
"max_limit": 1000,
"default_skip": 0,
"max_skip": 10000,
"timeout_seconds": 30
}
@classmethod
def get_mongodb_config(cls, environment: str = "default") -> Dict[str, Any]:
"""
获取MongoDB配置
Args:
environment: 环境名称 (default, development, production)
Returns:
Dict: MongoDB配置字典
"""
return cls.MONGODB_CONFIG.get(environment, cls.MONGODB_CONFIG["default"])
@classmethod
def get_redis_config(cls) -> Dict[str, Any]:
"""
获取Redis配置
Returns:
Dict: Redis配置字典
"""
return cls.REDIS_CONFIG
@classmethod
def get_api_config(cls) -> Dict[str, Any]:
"""
获取API配置
Returns:
Dict: API配置字典
"""
return cls.API_CONFIG
@classmethod
def get_logging_config(cls) -> Dict[str, Any]:
"""
获取日志配置
Returns:
Dict: 日志配置字典
"""
return cls.LOGGING_CONFIG
@classmethod
def get_query_config(cls) -> Dict[str, Any]:
"""
获取查询配置
Returns:
Dict: 查询配置字典
"""
return cls.QUERY_CONFIG
@classmethod
def validate_connection_string(cls, connection_string: str) -> bool:
"""
验证MongoDB连接字符串格式
Args:
connection_string: 连接字符串
Returns:
bool: 是否有效
"""
if not connection_string:
return False
# 基本格式检查
if not connection_string.startswith("mongodb://") and not connection_string.startswith("mongodb+srv://"):
return False
return True
@classmethod
def get_environment(cls) -> str:
"""
获取当前环境
Returns:
str: 环境名称
"""
return os.getenv("ENVIRONMENT", "default")
# 使用示例
if __name__ == "__main__":
# 获取默认配置
default_config = Config.get_mongodb_config()
print("默认MongoDB配置:")
print(f" 连接字符串: {default_config['connection_string']}")
print(f" 数据库: {default_config['database_name']}")
print(f" 集合: {default_config['collection_name']}")
# 获取开发环境配置
dev_config = Config.get_mongodb_config("development")
print("\n开发环境MongoDB配置:")
print(f" 连接字符串: {dev_config['connection_string']}")
print(f" 数据库: {dev_config['database_name']}")
print(f" 集合: {dev_config['collection_name']}")
# 获取Redis配置
redis_config = Config.get_redis_config()
print("\nRedis配置:")
print(f" 主机: {redis_config['host']}")
print(f" 端口: {redis_config['port']}")
print(f" 默认TTL: {redis_config['default_ttl_seconds']}秒 ({redis_config['default_ttl_seconds'] / 3600}小时)")
# 获取API配置
api_config = Config.get_api_config()
print("\nAPI配置:")
print(f" 主机: {api_config['host']}")
print(f" 端口: {api_config['port']}")
print(f" 调试模式: {api_config['debug']}")
# 验证连接字符串
test_connection = "mongodb://localhost:27017/"
is_valid = Config.validate_connection_string(test_connection)
print(f"\n连接字符串验证: {test_connection} -> {'有效' if is_valid else '无效'}")