[backend] feat: 添加项目基础配置

This commit is contained in:
xsl
2026-01-26 08:09:35 +08:00
parent e4b59c5ee4
commit 827549fa0d
7 changed files with 86 additions and 0 deletions
+12
View File
@@ -0,0 +1,12 @@
from .settings import get_settings, Settings
from .database import get_db, init_db, Base, engine, AsyncSessionLocal
__all__ = [
"get_settings",
"Settings",
"get_db",
"init_db",
"Base",
"engine",
"AsyncSessionLocal",
]
Binary file not shown.
Binary file not shown.
Binary file not shown.
+36
View File
@@ -0,0 +1,36 @@
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from sqlalchemy.orm import declarative_base
from .settings import get_settings
settings = get_settings()
DATABASE_URL = (
f"mysql+aiomysql://{settings.DB_USER}:{settings.DB_PASSWORD}"
f"@{settings.DB_HOST}:{settings.DB_PORT}/{settings.DB_NAME}"
f"?charset={settings.DB_CHARSET}"
)
engine = create_async_engine(DATABASE_URL, echo=settings.DEBUG, future=True)
AsyncSessionLocal = async_sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False
)
Base = declarative_base()
async def get_db():
async with AsyncSessionLocal() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()
async def init_db():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
+29
View File
@@ -0,0 +1,29 @@
from pydantic_settings import BaseSettings
from functools import lru_cache
class Settings(BaseSettings):
APP_NAME: str = "海洋项目管理系统"
APP_VERSION: str = "1.0.0"
DEBUG: bool = True
DB_HOST: str = "localhost"
DB_PORT: int = 3306
DB_USER: str = "root"
DB_PASSWORD: str = "rootpassword"
DB_NAME: str = "project_manager"
DB_CHARSET: str = "utf8mb4"
SECRET_KEY: str = "your-secret-key-change-this-in-production"
ALGORITHM: str = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES: int = 1440
CORS_ORIGINS: list[str] = ["http://localhost:3000", "http://127.0.0.1:3000"]
class Config:
env_file = ".env"
case_sensitive = True
@lru_cache()
def get_settings():
return Settings()
+9
View File
@@ -0,0 +1,9 @@
fastapi==0.104.1
uvicorn[standard]==0.24.0
sqlalchemy==2.0.23
aiomysql==0.2.0
pydantic==2.5.0
pydantic-settings==2.1.0
pyjwt==2.8.0
bcrypt==4.1.1
python-multipart==0.0.6