1
0
mirror of https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git synced 2025-08-14 00:25:46 +02:00
This commit is contained in:
MoonTestUse1
2024-12-28 06:24:53 +06:00
parent bde484f1e0
commit 0c95c62f1d
31 changed files with 64 additions and 1417 deletions

View File

@@ -5,6 +5,8 @@ from fastapi.openapi.utils import get_openapi
import logging
from logging.config import dictConfig
from .logging_config import logging_config
from .middleware import LoggingMiddleware
from .routers import auth
# Configure logging
dictConfig(logging_config)
@@ -18,6 +20,21 @@ app = FastAPI(
redoc_url=None
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add logging middleware
app.add_middleware(LoggingMiddleware)
# Include routers
app.include_router(auth.router, prefix="/api/auth", tags=["auth"])
# Custom OpenAPI documentation
@app.get("/api/docs", include_in_schema=False)
async def custom_swagger_ui_html():
@@ -34,6 +51,4 @@ async def get_open_api_endpoint():
version="1.0.0",
description="API for managing support requests and employees",
routes=app.routes
)
# Existing middleware and routes...
)

View File

@@ -0,0 +1,4 @@
"""API routes package"""
from . import auth
__all__ = ['auth']

View File

@@ -0,0 +1,42 @@
"""Authentication routes"""
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from ..database import get_db
from ..crud import auth as auth_crud
from ..models.employee import EmployeeBase
from logging import getLogger
router = APIRouter()
logger = getLogger(__name__)
@router.post("/login")
async def login(credentials: dict, db: Session = Depends(get_db)):
"""Employee login endpoint"""
try:
employee = auth_crud.authenticate_employee(
db,
credentials.get("lastName"),
credentials.get("password")
)
if not employee:
raise HTTPException(status_code=401, detail="Неверные учетные данные")
return employee
except Exception as e:
logger.error(f"Login error: {e}", exc_info=True)
raise HTTPException(status_code=500, detail="Ошибка сервера")
@router.post("/admin")
async def admin_login(credentials: dict, db: Session = Depends(get_db)):
"""Admin login endpoint"""
try:
is_valid = auth_crud.authenticate_admin(
db,
credentials.get("username"),
credentials.get("password")
)
if not is_valid:
raise HTTPException(status_code=401, detail="Неверные учетные данные")
return {"status": "success"}
except Exception as e:
logger.error(f"Admin login error: {e}", exc_info=True)
raise HTTPException(status_code=500, detail="Ошибка сервера")