|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # -*- coding: utf-8 -*- |
| 3 | +import os |
| 4 | + |
3 | 5 | from contextlib import asynccontextmanager |
4 | 6 |
|
5 | 7 | import socketio |
|
9 | 11 | from fastapi_limiter import FastAPILimiter |
10 | 12 | from fastapi_pagination import add_pagination |
11 | 13 | from starlette.middleware.authentication import AuthenticationMiddleware |
| 14 | +from starlette.staticfiles import StaticFiles |
12 | 15 |
|
13 | 16 | from backend.common.exception.exception_handler import register_exception |
14 | 17 | from backend.common.log import set_customize_logfile, setup_logging |
15 | 18 | from backend.core.conf import settings |
16 | | -from backend.core.path_conf import STATIC_DIR |
| 19 | +from backend.core.path_conf import STATIC_DIR, UPLOAD_DIR |
17 | 20 | from backend.database.db import create_table |
18 | 21 | from backend.database.redis import redis_client |
19 | 22 | from backend.middleware.jwt_auth_middleware import JwtAuthMiddleware |
@@ -101,14 +104,17 @@ def register_logger() -> None: |
101 | 104 |
|
102 | 105 | def register_static_file(app: FastAPI): |
103 | 106 | """ |
104 | | - 静态文件交互开发模式, 生产将自动关闭,生产必须使用 nginx 静态资源服务 |
| 107 | + 静态资源服务,生产应使用 nginx 代理静态资源服务 |
105 | 108 |
|
106 | 109 | :param app: |
107 | 110 | :return: |
108 | 111 | """ |
| 112 | + # 上传静态资源 |
| 113 | + if not os.path.exists(UPLOAD_DIR): |
| 114 | + os.makedirs(UPLOAD_DIR) |
| 115 | + app.mount('/static/upload', StaticFiles(directory=UPLOAD_DIR), name='upload') |
| 116 | + # 固有静态资源 |
109 | 117 | if settings.FASTAPI_STATIC_FILES: |
110 | | - from fastapi.staticfiles import StaticFiles |
111 | | - |
112 | 118 | app.mount('/static', StaticFiles(directory=STATIC_DIR), name='static') |
113 | 119 |
|
114 | 120 |
|
|
0 commit comments