From cc486f6637142d7a3cb4180c926dd87ef345d9c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Victor?= Date: Mon, 13 May 2024 13:52:41 -0300 Subject: [PATCH] Atualiza imports para deploy na vercel --- .gitignore | 3 ++- index.py | 13 +++++++++++ src/controller/authController.py | 12 +++++------ src/controller/userController.py | 10 ++++----- src/{domain/models => database}/__init__.py | 0 src/{ => database}/database.py | 0 src/domain/userSchema.py | 8 +++---- src/main.py | 24 +++++++++------------ src/model/userModel.py | 2 +- src/repository/userRepository.py | 6 ++---- src/utils/dotenv.py | 2 +- src/utils/security.py | 2 +- src/utils/send_mail.py | 2 +- tests/test_auth.py | 2 +- tests/test_user.py | 2 +- vercel.json | 15 +++++++++++++ 16 files changed, 62 insertions(+), 41 deletions(-) create mode 100644 index.py rename src/{domain/models => database}/__init__.py (100%) rename src/{ => database}/database.py (100%) create mode 100644 vercel.json diff --git a/.gitignore b/.gitignore index 8cf2f7c..44447c7 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ unb_tv.egg-info data/postgress .pytest_cache data -.coverage \ No newline at end of file +.coverage +.vercel diff --git a/index.py b/index.py new file mode 100644 index 0000000..aa835ae --- /dev/null +++ b/index.py @@ -0,0 +1,13 @@ +import uvicorn +from dotenv import load_dotenv + +load_dotenv() + +from src.utils import dotenv + +load_dotenv() +dotenv.validate_dotenv() + +if __name__ == '__main__': # pragma: no cover + port = 8000 + uvicorn.run('src.main:app', reload=True, port=int(port), host="0.0.0.0") \ No newline at end of file diff --git a/src/controller/authController.py b/src/controller/authController.py index 1cc8d7f..32f5e9a 100644 --- a/src/controller/authController.py +++ b/src/controller/authController.py @@ -1,15 +1,13 @@ -import os from fastapi import APIRouter, HTTPException, Response, status, Depends -from utils import security, enumeration, send_mail -from database import get_db +from src.utils import security, enumeration, send_mail +from src.database.database import get_db from sqlalchemy.orm import Session from datetime import datetime, timedelta -from constants import errorMessages +from src.constants import errorMessages from starlette.responses import JSONResponse -from domain import userSchema, authSchema -from repository import userRepository -import secrets +from src.domain import userSchema, authSchema +from src.repository import userRepository auth = APIRouter( prefix="/auth" diff --git a/src/controller/userController.py b/src/controller/userController.py index da27673..bbddd22 100644 --- a/src/controller/userController.py +++ b/src/controller/userController.py @@ -1,11 +1,11 @@ from fastapi import APIRouter, HTTPException, Response, status, Depends, Header -from database import get_db +from src.database.database import get_db from sqlalchemy.orm import Session -from constants import errorMessages -from domain import userSchema -from repository import userRepository -from utils import security, enumeration +from src.constants import errorMessages +from src.domain import userSchema +from src.repository import userRepository +from src.utils import security, enumeration from fastapi_filter import FilterDepends diff --git a/src/domain/models/__init__.py b/src/database/__init__.py similarity index 100% rename from src/domain/models/__init__.py rename to src/database/__init__.py diff --git a/src/database.py b/src/database/database.py similarity index 100% rename from src/database.py rename to src/database/database.py diff --git a/src/domain/userSchema.py b/src/domain/userSchema.py index 7ff4086..065962c 100644 --- a/src/domain/userSchema.py +++ b/src/domain/userSchema.py @@ -3,12 +3,12 @@ from fastapi_filter import FilterDepends, with_prefix from sqlalchemy import or_ from fastapi_filter.contrib.sqlalchemy import Filter -from model import userModel +from src.model import userModel class UserUpdate(BaseModel): - name: str | None = None - email: str | None = None - connection: str | None = None + name: str = None + email: str = None + connection: str = None class User(BaseModel): model_config = ConfigDict(from_attributes = True) diff --git a/src/main.py b/src/main.py index ddda43b..f1ab4f1 100644 --- a/src/main.py +++ b/src/main.py @@ -1,18 +1,14 @@ -import uvicorn, sys +# import uvicorn, sys from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware -from dotenv import load_dotenv -from utils import dotenv -load_dotenv() -dotenv.validate_dotenv() +from src.controller import userController, authController +from src.model import userModel +from src.database import database -from controller import userController, authController -from database import engine -from model import userModel -userModel.Base.metadata.create_all(bind=engine) +userModel.Base.metadata.create_all(bind=database.engine) app = FastAPI() @@ -35,9 +31,9 @@ def read_root(): return {"message": "UnB-TV!"} -if __name__ == '__main__': # pragma: no cover - port = 8000 - if (len(sys.argv) == 2): - port = sys.argv[1] +# if __name__ == '__main__': # pragma: no cover +# port = 8000 +# if (len(sys.argv) == 2): +# port = sys.argv[1] - uvicorn.run('main:app', reload=True, port=int(port), host="0.0.0.0") \ No newline at end of file +# uvicorn.run('main:app', reload=True, port=int(port), host="0.0.0.0") \ No newline at end of file diff --git a/src/model/userModel.py b/src/model/userModel.py index 4a7366e..09ac81b 100644 --- a/src/model/userModel.py +++ b/src/model/userModel.py @@ -3,7 +3,7 @@ from sqlalchemy import Boolean, Column, ForeignKey, Integer, String from sqlalchemy.orm import relationship -from database import Base +from src.database.database import Base class User(Base): __tablename__ = "users" diff --git a/src/repository/userRepository.py b/src/repository/userRepository.py index 7422d77..a2c612b 100644 --- a/src/repository/userRepository.py +++ b/src/repository/userRepository.py @@ -1,11 +1,9 @@ -from repository import userRepository - # Referencia: https://fastapi.tiangolo.com/tutorial/sql-databases/#crud-utils from sqlalchemy import or_ from sqlalchemy.orm import Session -from domain import userSchema -from model import userModel +from src.domain import userSchema +from src.model import userModel # Obtem usuario a partir do seu ID def get_user(db: Session, user_id: int): diff --git a/src/utils/dotenv.py b/src/utils/dotenv.py index 5d9802b..236a6f6 100644 --- a/src/utils/dotenv.py +++ b/src/utils/dotenv.py @@ -1,5 +1,5 @@ import os -from constants import errorMessages +from src.constants import errorMessages def validate_dotenv(): required_env_var = ["SECRET", "ALGORITHM", "MAIL_USERNAME", "MAIL_PASSWORD", "MAIL_FROM", "MAIL_PORT", "MAIL_SERVER"] diff --git a/src/utils/security.py b/src/utils/security.py index 6be9212..90993f8 100644 --- a/src/utils/security.py +++ b/src/utils/security.py @@ -4,7 +4,7 @@ from passlib.context import CryptContext from jose import JWTError, jwt from fastapi.security import OAuth2PasswordBearer -from constants import errorMessages +from src.constants import errorMessages SECRET_KEY = os.getenv("SECRET") ALGORITHM = os.getenv("ALGORITHM") diff --git a/src/utils/send_mail.py b/src/utils/send_mail.py index cc7d788..5cf74ef 100644 --- a/src/utils/send_mail.py +++ b/src/utils/send_mail.py @@ -5,7 +5,7 @@ from pydantic import BaseModel, EmailStr from starlette.responses import JSONResponse from typing import List -from domain import authSchema +from src.domain import authSchema conf = ConnectionConfig( MAIL_USERNAME = os.getenv("MAIL_USERNAME"), diff --git a/tests/test_auth.py b/tests/test_auth.py index 4250cbe..91e4546 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -13,7 +13,7 @@ from src.constants import errorMessages from src.model import userModel from src.utils import security, dotenv, send_mail, enumeration -from src.database import get_db, engine, Base +from src.database.database import get_db, engine, Base valid_user_active_admin = {"name": "Forsen", "email": "valid@email.com", "connection": "PROFESSOR", "password": "123456"} valid_user_active_user = {"name": "Guy Beahm", "email": "valid2@email.com", "connection": "ESTUDANTE", "password": "123456"} diff --git a/tests/test_user.py b/tests/test_user.py index 4f8b5ea..0a4dd72 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -3,7 +3,7 @@ from src.main import app from src.constants import errorMessages -from src.database import get_db, engine, Base +from src.database.database import get_db, engine, Base from tests import test_auth valid_user_active_admin = test_auth.valid_user_active_admin diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..cfbd1ec --- /dev/null +++ b/vercel.json @@ -0,0 +1,15 @@ +{ + "builds": [ + { + "src": "src/main.py", + "use": "@vercel/python", + "config": {"maxLambdaSize": "15mb"} + } + ], + "routes": [ + { + "src": "/(.*)", + "dest": "src/main.py" + } + ] +}