feat: initial commit - Phase 1 & 2 core features

This commit is contained in:
hiderfong
2026-04-22 17:07:33 +08:00
commit 1773bda06b
25005 changed files with 6252106 additions and 0 deletions
View File
+35
View File
@@ -0,0 +1,35 @@
from pydantic_settings import BaseSettings
from typing import List
class Settings(BaseSettings):
PROJECT_NAME: str = "PropDataGuard"
VERSION: str = "0.1.0"
DESCRIPTION: str = "财产保险行业数据分级分类管理平台"
DATABASE_URL: str = "postgresql+psycopg2://pdg:pdg_secret_2024@localhost:5432/prop_data_guard"
REDIS_URL: str = "redis://localhost:6379/0"
SECRET_KEY: str = "prop-data-guard-super-secret-key-change-in-production"
ALGORITHM: str = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES: int = 30
REFRESH_TOKEN_EXPIRE_DAYS: int = 7
MINIO_ENDPOINT: str = "localhost:9000"
MINIO_ACCESS_KEY: str = "pdgminio"
MINIO_SECRET_KEY: str = "pdgminio_secret_2024"
MINIO_SECURE: bool = False
MINIO_BUCKET_NAME: str = "pdg-files"
CORS_ORIGINS: List[str] = ["http://localhost:5173", "http://127.0.0.1:5173"]
FIRST_SUPERUSER_USERNAME: str = "admin"
FIRST_SUPERUSER_PASSWORD: str = "admin123"
FIRST_SUPERUSER_EMAIL: str = "admin@propdataguard.com"
class Config:
env_file = ".env"
case_sensitive = True
settings = Settings()
+17
View File
@@ -0,0 +1,17 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from app.core.config import settings
engine = create_engine(settings.DATABASE_URL, pool_pre_ping=True)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
+20
View File
@@ -0,0 +1,20 @@
from app.core.config import settings
import redis
import minio
redis_client = redis.from_url(settings.REDIS_URL, decode_responses=True)
minio_client = minio.Minio(
settings.MINIO_ENDPOINT,
access_key=settings.MINIO_ACCESS_KEY,
secret_key=settings.MINIO_SECRET_KEY,
secure=settings.MINIO_SECURE,
)
def init_minio_bucket():
try:
if not minio_client.bucket_exists(settings.MINIO_BUCKET_NAME):
minio_client.make_bucket(settings.MINIO_BUCKET_NAME)
except Exception as e:
print(f"MinIO init warning: {e}")
+50
View File
@@ -0,0 +1,50 @@
from datetime import datetime, timedelta, timezone
from typing import Optional, Tuple
from jose import jwt, JWTError
from passlib.context import CryptContext
from app.core.config import settings
pwd_context = CryptContext(schemes=["argon2"], deprecated="auto")
def verify_password(plain_password: str, hashed_password: str) -> bool:
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password: str) -> str:
return pwd_context.hash(password)
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
to_encode = data.copy()
if expires_delta:
expire = datetime.now(timezone.utc) + expires_delta
else:
expire = datetime.now(timezone.utc) + timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire, "type": "access"})
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
return encoded_jwt
def create_refresh_token(data: dict) -> str:
to_encode = data.copy()
expire = datetime.now(timezone.utc) + timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS)
to_encode.update({"exp": expire, "type": "refresh"})
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=settings.ALGORITHM)
return encoded_jwt
def decode_token(token: str) -> Optional[dict]:
try:
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=[settings.ALGORITHM])
return payload
except JWTError:
return None
def create_token_pair(user_id: int, username: str) -> Tuple[str, str]:
access = create_access_token({"sub": str(user_id), "username": username})
refresh = create_refresh_token({"sub": str(user_id), "username": username})
return access, refresh