diff --git a/.env.docker b/.env.docker new file mode 100644 index 00000000..b3489ba8 --- /dev/null +++ b/.env.docker @@ -0,0 +1,19 @@ +# Env: dev、pro +ENVIRONMENT='dev' +# MySQL +DB_HOST='mysql' +DB_PORT=3306 +DB_USER='root' +DB_PASSWORD='123456' +# Redis +REDIS_HOST='redis' +REDIS_PORT=6379 +REDIS_PASSWORD='' +REDIS_DATABASE=0 +# APScheduler +APS_REDIS_HOST='redis' +APS_REDIS_PORT=6379 +APS_REDIS_PASSWORD='' +APS_REDIS_DATABASE=1 +# Token +TOKEN_SECRET_KEY='1VkVF75nsNABBjK_7-qz7GtzNy3AMvktc9TCPwKczCk' diff --git a/Dockerfile b/Dockerfile index d2a6c8a1..62daeba5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,16 @@ -FROM python:3.8-slim +FROM python:3.10-slim WORKDIR /fba COPY . /fba +RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list \ + && sed -i s@/security.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list + +RUN apt-get update \ + && apt-get install -y --no-install-recommends gcc python3-dev \ + && rm -rf /var/lib/apt/lists/* + RUN pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple \ && pip install --no-cache-dir -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple @@ -11,6 +18,8 @@ ENV TZ = Asia/Shanghai RUN mkdir -p /var/log/fastapi_server +RUN cd /fba && touch .env + EXPOSE 8001 CMD ["uvicorn", "backend.app.main:app", "--host", "127.0.0.1", "--port", "8000"] diff --git a/README.md b/README.md index e33651a3..53013391 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,13 @@ git clone https://github.com/wu-clan/fastapi_best_architecture.git 2. Create a database `fba`, choose utf8mb4 encode 3. Install and start Redis -4. Copy .env.example to .env and view `backend/app/core/conf.py`, update database configuration information -5. Perform a database migration [alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html) +4. create a `.env` file in the `backend/app/` directory + ```shell + cd backend/app/ + touch .env + ``` +5. Copy .env.example to .env and view `backend/app/core/conf.py`, update database configuration information +6. Perform a database migration [alembic](https://alembic.sqlalchemy.org/en/latest/tutorial.html) ```shell cd backend/app/ @@ -46,8 +51,8 @@ git clone https://github.com/wu-clan/fastapi_best_architecture.git # Perform the migration alembic upgrade head ``` -6. Execute the backend/app/main.py file startup service -7. Browser access: http://127.0.0.1:8000/v1/docs +7. Execute the backend/app/main.py file startup service +8. Browser access: http://127.0.0.1:8000/v1/docs --- diff --git a/backend/app/common/log.py b/backend/app/common/log.py index 30d1a7f5..1446f2f8 100644 --- a/backend/app/common/log.py +++ b/backend/app/common/log.py @@ -3,13 +3,16 @@ from __future__ import annotations import os +from typing import TYPE_CHECKING -import loguru from loguru import logger from backend.app.core import path_conf from backend.app.core.conf import settings +if TYPE_CHECKING: + import loguru + class Logger: @staticmethod diff --git a/backend/app/database/db_mysql.py b/backend/app/database/db_mysql.py index 31bfefba..32395d8b 100644 --- a/backend/app/database/db_mysql.py +++ b/backend/app/database/db_mysql.py @@ -21,7 +21,7 @@ SQLALCHEMY_DATABASE_URL = ( try: # 数据库引擎 - async_engine = create_async_engine(SQLALCHEMY_DATABASE_URL, echo=settings.DB_ECHO, future=True) + async_engine = create_async_engine(SQLALCHEMY_DATABASE_URL, echo=settings.DB_ECHO, future=True, pool_pre_ping=True) # log.success('数据库连接成功') except Exception as e: log.error('❌ 数据库链接失败 {}', e) diff --git a/docker-compose.yml b/docker-compose.yml index 1b8cc04e..94af98d0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: "3.8" +version: "3.10" services: app: @@ -12,7 +12,7 @@ services: - redis volumes: - ./backend:/fba/backend - - ./docker_conf.py:/fba/backend/app/core/conf.py + - .env.docker:/fba/backend/app/.env - fba_static:/fba/backend/app/static networks: - fba_network diff --git a/docker_conf.py b/docker_conf.py deleted file mode 100644 index 327f1050..00000000 --- a/docker_conf.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- -from functools import lru_cache - -from pydantic import BaseSettings - - -class Settings(BaseSettings): - # FastAPI - TITLE: str = 'FastAPI' - VERSION: str = 'v0.0.1' - DESCRIPTION: str = 'FastAPI Best Architecture' - DOCS_URL: str | None = '/v1/docs' - REDOCS_URL: str | None = None - OPENAPI_URL: str | None = '/v1/openapi' - - # Static Server - STATIC_FILES: bool = False - - # MySQL - DB_ECHO: bool = False - DB_HOST: str = 'mysql' - DB_PORT: int = 3306 - DB_USER: str = 'root' - DB_PASSWORD: str = '123456' - DB_DATABASE: str = 'fba' - DB_CHARSET: str = 'utf8mb4' - - # Redis - REDIS_HOST: str = 'redis' - REDIS_PORT: int = 6379 - REDIS_PASSWORD: str = '' - REDIS_DATABASE: int = 0 - REDIS_TIMEOUT: int = 5 - - # APScheduler DB - APS_REDIS_HOST: str = 'redis' - APS_REDIS_PORT: int = 6379 - APS_REDIS_PASSWORD: str = '' - APS_REDIS_DATABASE: int = 1 - APS_REDIS_TIMEOUT: int = 10 - - # APScheduler Default - APS_COALESCE: bool = False # 是否合并运行 - APS_MAX_INSTANCES: int = 3 # 最大实例数 - APS_MISFIRE_GRACE_TIME: int = 60 # 任务错过执行时间后,最大容错时间,过期后不再执行,单位:秒 - - # Token - TOKEN_ALGORITHM: str = 'HS256' # 算法 - TOKEN_SECRET_KEY: str = '1VkVF75nsNABBjK_7-qz7GtzNy3AMvktc9TCPwKczCk' # 密钥 secrets.token_urlsafe(32)) - TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 1 # token 时效 60 * 24 * 1 = 1 天 - - # Cookies - COOKIES_MAX_AGE: int = 60 * 5 # cookies 时效 60 * 5 = 5 分钟 - - # Middleware - MIDDLEWARE_CORS: bool = True - MIDDLEWARE_GZIP: bool = True - MIDDLEWARE_ACCESS: bool = False - - -@lru_cache -def get_settings(): - """读取配置优化写法""" - return Settings() - - -settings = get_settings() diff --git a/gunicorn.conf.py b/gunicorn.conf.py index d4e789c6..3cd9ff1a 100644 --- a/gunicorn.conf.py +++ b/gunicorn.conf.py @@ -41,6 +41,6 @@ capture_output = True loglevel = 'debug' # python程序 -pythonpath = '/usr/local/lib/python3.8/site-packages' +pythonpath = '/usr/local/lib/python3.10/site-packages' # 启动 gunicorn -c gunicorn.conf.py main:app diff --git a/nginx.conf b/nginx.conf index 794659d5..d453b5ab 100644 --- a/nginx.conf +++ b/nginx.conf @@ -37,7 +37,7 @@ http { root /fba; - client_max_body_size 10m; #最大上传文件 + client_max_body_size 10m; # 最大上传文件 location / { proxy_pass http://app:8001;