Skip to content

Commit 2adada0

Browse files
authored
fix(docker): remove exposed ports for db and redis services (#25)
* fix(docker): remove exposed ports for db and redis services * fix(config): add APP_ENV config * fix(limiter): fix import error
1 parent 08d4b91 commit 2adada0

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

docker-compose.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ services:
1212
- .env
1313
volumes:
1414
- ./pg_data:/var/lib/postgresql/data
15-
ports:
16-
- "5432:5432"
1715
healthcheck:
1816
test: ["CMD-SHELL", "pg_isready -U zhixuelite -d zhixuelite"]
1917
interval: 5s
@@ -24,8 +22,6 @@ services:
2422
redis:
2523
image: redis:8-alpine
2624
restart: unless-stopped
27-
ports:
28-
- "6379:6379"
2925
command: ["redis-server", "--save", "", "--appendonly", "no", "--maxmemory", "64mb", "--maxmemory-policy", "volatile-lru"]
3026
healthcheck:
3127
test: ["CMD", "redis-cli", "ping"]

src/ZhiXueLite/app/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,17 @@ def create_app():
5959

6060
Session(app)
6161

62+
if limiter.enabled and config.RATELIMIT_STORAGE_URI == "memory://" and app.config["APP_ENV"] == "production":
63+
app.logger.warning(
64+
"Using in-memory rate limit storage in production is not recommended. "
65+
"That may lead to unexpected behavior when running multiple instances of the application. "
66+
"Consider using Redis.")
6267
limiter.init_app(app)
6368

6469
# 初始化缓存
6570
cache = Cache(app, config={
66-
'CACHE_TYPE': 'SimpleCache',
67-
'CACHE_DEFAULT_TIMEOUT': 300 # 5 分钟
71+
"CACHE_TYPE": "SimpleCache",
72+
"CACHE_DEFAULT_TIMEOUT": 600 # 10 分钟
6873
})
6974

7075
# 自定义频率限制错误处理器
@@ -185,7 +190,7 @@ def ping():
185190

186191
@app.route("/statistics", methods=["GET"])
187192
def get_statistics():
188-
"""获取系统统计信息(公开端点,缓存 5 分钟)"""
193+
"""获取系统统计信息"""
189194
cached_stats = cache.get("statistics_data")
190195
if cached_stats is not None:
191196
return jsonify({
@@ -217,7 +222,7 @@ def get_statistics():
217222
@app.cli.command("init-db")
218223
def init_db_command():
219224
"""清除所有数据"""
220-
if not app.config["DEBUG"]:
225+
if app.config["APP_ENV"] == "production":
221226
print("This command is strongly discouraged in production environments.")
222227
with app.app_context():
223228
if input("Are you sure you want to drop ALL tables? (y/N): ").lower() == "y":

src/ZhiXueLite/app/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ def validate(cls):
6666

6767
class DevelopmentConfig(Config):
6868
DEBUG = True
69+
APP_ENV = "development"
6970
SECRET_KEY = os.environ.get("DEV_SECRET_KEY", "dev")
7071
SQLALCHEMY_DATABASE_URI = os.environ.get("DEV_DATABASE_URL", "")
7172

@@ -75,6 +76,7 @@ class DevelopmentConfig(Config):
7576

7677
class ProductionConfig(Config):
7778
DEBUG = False
79+
APP_ENV = "production"
7880
SECRET_KEY = os.environ.get("PROD_SECRET_KEY", "")
7981
SQLALCHEMY_DATABASE_URI = os.environ.get("PROD_DATABASE_URL", "")
8082

@@ -86,6 +88,7 @@ class TestingConfig(Config):
8688
"""测试环境配置"""
8789
TESTING = True
8890
DEBUG = True
91+
APP_ENV = "testing"
8992
SECRET_KEY = os.environ.get("SECRET_KEY", "test-secret-key")
9093
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL", "sqlite:///:memory:")
9194
# SQLite 不支持连接池配置

0 commit comments

Comments
 (0)