[backend] feat: 添加项目基础配置
This commit is contained in:
@@ -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.
@@ -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)
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user