Spaces:
Sleeping
Sleeping
import os | |
from fastapi import FastAPI, Depends, HTTPException, status | |
from fastapi.responses import FileResponse | |
from fastapi.security import HTTPBasic, HTTPBasicCredentials | |
from fastapi.middleware.cors import CORSMiddleware | |
from dotenv import load_dotenv | |
from create_database import create_db | |
from routers import teacher, review, user | |
# Cargar variables de entorno desde el archivo .env | |
load_dotenv() | |
if not os.path.exists("site.db"): | |
create_db() | |
# Obtener credenciales de las variables de entorno | |
BASIC_AUTH_USER = os.getenv("BASIC_AUTH_USER") | |
BASIC_AUTH_PASSWORD = os.getenv("BASIC_AUTH_PASSWORD") | |
app = FastAPI( | |
title="API de revisión de profesores", | |
description="API para revisar profesores", | |
version="2.8.7", | |
openapi_url="/teacher-openapi.json", | |
docs_url="/teacher-docs", | |
redoc_url="/teacher-redoc", | |
contact={ | |
"name": "teacher", | |
"url": "https://www.test.com", | |
"email": "calificandoprofesoruniversidad@gmail.com", | |
}, | |
license_info={"name": "MIT", "url": "https://opensource.org/licenses/MIT"}, | |
) | |
# CORS | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
security = HTTPBasic() | |
def authenticate(credentials: HTTPBasicCredentials = Depends(security)): | |
if ( | |
credentials.username != BASIC_AUTH_USER | |
or credentials.password != BASIC_AUTH_PASSWORD | |
): | |
raise HTTPException( | |
status_code=status.HTTP_401_UNAUTHORIZED, | |
detail="Credenciales incorrectas", | |
headers={"WWW-Authenticate": "Basic"}, | |
) | |
def read_root(): | |
return {"message": "Bienvenido a la API de la revisión de profesores"} | |
app.include_router(teacher.router, dependencies=[Depends(authenticate)]) | |
app.include_router(review.router, dependencies=[Depends(authenticate)]) | |
app.include_router(user.router, dependencies=[Depends(authenticate)]) | |