feat:初始化工程
This commit is contained in:
BIN
config/__pycache__/database.cpython-312.pyc
Normal file
BIN
config/__pycache__/database.cpython-312.pyc
Normal file
Binary file not shown.
BIN
config/__pycache__/setting.cpython-312.pyc
Normal file
BIN
config/__pycache__/setting.cpython-312.pyc
Normal file
Binary file not shown.
26
config/database.py
Normal file
26
config/database.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from config.setting import settings
|
||||
from models.log_alert import Base
|
||||
|
||||
engine = create_engine(
|
||||
settings.database_url, connect_args={"check_same_thread": False}
|
||||
)
|
||||
|
||||
# 会话工厂
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
|
||||
def init_db():
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def get_db():
|
||||
# 创建数据库会话实例
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
18
config/setting.py
Normal file
18
config/setting.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
# sqlite连接方式:sqlite:///
|
||||
database_url: str = "sqlite:///./logs.db"
|
||||
wechat_webhook: str = ""
|
||||
smtp_server: str = ""
|
||||
smtp_port: int = 587
|
||||
smtp_user: str = ""
|
||||
smtp_password: str = ""
|
||||
smtp_sender: str = "log-alert@example.com"
|
||||
|
||||
class Config:
|
||||
env_file = ".env"
|
||||
|
||||
|
||||
settings = Settings()
|
||||
Reference in New Issue
Block a user