Skip to content

Commit 3efffbb

Browse files
Merge pull request #30 from RodrigoDevBack/rodrigo-refact-backend-front
Rodrigo refact backend front
2 parents 7250f80 + b150d2b commit 3efffbb

47 files changed

Lines changed: 1929 additions & 379 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/deploy.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Deploy para VPS
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout do código
14+
uses: actions/checkout@v3
15+
16+
- name: Configurar chave SSH
17+
run: |
18+
mkdir -p ~/.ssh
19+
echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
20+
chmod 600 ~/.ssh/id_ed25519
21+
ssh-keyscan -H root@191.252.177.252 >> ~/.ssh/known_hosts
22+
23+
- name: Deploy para VPS
24+
run: |
25+
ssh root@191.252.177.252 << 'EOF'
26+
cd /www/sites/e-commerce/
27+
git pull origin main
28+
docker-compose down
29+
docker-compose up -d --build
30+
EOF

.gitignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
.venv/
2-
**__pycache__
2+
**__pycache__
3+
4+
src/backend/app/integrations/images_products/*
5+
!src/backend/app/integrations/images_products/.gitkeep
6+
7+
src/backend/app/integrations/code_validate_email/*
8+
!src/backend/app/integrations/code_validate_email/.gitkeep
9+
10+
src/backend/app/integrations/code_recover_password/*
11+
!src/backend/app/integrations/code_recover_password/.gitkeep

docker-compose.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ services:
33
build: ./src/frontend/
44
container_name: frontend_app
55
ports:
6-
- "8000:80"
6+
- "8000:8000"
7+
networks:
8+
- default
79
depends_on:
810
- backend
911
volumes:
1012
- ./src/frontend:/frontend
1113
backend:
1214
build: ./src/backend/app
1315
container_name: backend_app
16+
networks:
17+
- default
1418
ports:
1519
- "5000:5000"
1620
depends_on:
@@ -32,8 +36,14 @@ services:
3236
POSTGRES_DB: postgres
3337
ports:
3438
- "5433:5432"
39+
networks:
40+
- default
3541
volumes:
3642
- postgres_data:/var/lib/postgresql/data
3743

3844
volumes:
3945
postgres_data:
46+
47+
networks:
48+
default:
49+
driver: bridge

src/backend/app/db/config_db.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ async def lifespan(app: FastAPI):
1616
await Tortoise.generate_schemas()
1717
yield
1818
await Tortoise.close_connections()
19-

src/backend/app/integrations/code_recover_password/.gitkeep

Whitespace-only changes.

src/backend/app/integrations/code_validate_email/rodrigomoraes4030@gmail.com/rodrigomoraes4030@gmail.com.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/backend/app/integrations/email_client.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
password_email = os.getenv('PASSWORD')
1111

1212
class Email_Client():
13+
server = smtplib.SMTP("smtp.gmail.com", 587)
14+
server.starttls()
1315

1416
def create_code(self, gmail: str):
1517
os.makedirs(f'/app/integrations/code_validate_email/{gmail}', exist_ok=True)
@@ -38,11 +40,8 @@ def send_email(self, name: str, gmail: str, code: int):
3840
email['From'] = sender_email
3941
email['To'] = gmail
4042
try:
41-
server = smtplib.SMTP("smtp.gmail.com", 587)
42-
server.starttls()
43-
server.login(email['From'], password_email)
44-
server.sendmail(email['From'], email['To'], email.as_string())
45-
server.quit()
43+
self.server.login(email['From'], password_email)
44+
self.server.sendmail(email['From'], email['To'], email.as_string())
4645
return {'Response': 200}
4746
except Exception as e:
4847
raise HTTPException(status.HTTP_400_BAD_REQUEST, {'Fail': e})
@@ -63,11 +62,8 @@ def send_email_admin(self, name: str, gmail: str, code: int):
6362
email['From'] = sender_email
6463
email['To'] = gmail
6564
try:
66-
server = smtplib.SMTP("smtp.gmail.com", 587)
67-
server.starttls()
68-
server.login(email['From'], password_email)
69-
server.sendmail(email['From'], email['To'], email.as_string())
70-
server.quit()
65+
self.server.login(email['From'], password_email)
66+
self.server.sendmail(email['From'], email['To'], email.as_string())
7167
return {'Response': 200}
7268
except Exception as e:
7369
raise HTTPException(status.HTTP_400_BAD_REQUEST, {'Fail': e})

src/backend/app/integrations/image_save.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,42 @@
22
from fastapi.staticfiles import StaticFiles
33
import os, shutil, time
44

5-
def configure_image(app: FastAPI):
5+
def configure_image(app: FastAPI) -> FastAPI:
66
app.mount("/images_products", StaticFiles(directory="/app/integrations/images_products"), name="/images_products")
7+
return app
78

89

9-
def generate_new_name_image(filename):
10+
def generate_new_name_image(filename) -> str:
1011
name, extension = os.path.splitext(filename)
1112
alteration = int(time.time())
1213
return f"{name}_{alteration}{extension}"
1314

1415

15-
def create_new_directory_product(name_product):
16+
def create_new_directory_product(name_product) -> None:
1617
os.makedirs(f"/app/integrations/images_products/{name_product}", exist_ok=True)
1718

1819

19-
def add_image(name_product, file):
20+
def delete_product_image(name_product, name_image) -> bool:
21+
path = f"/app/integrations/images_products/{name_product}/{name_image}"
22+
try:
23+
if os.path.exists(path):
24+
os.remove(path)
25+
return True
26+
except FileNotFoundError or PermissionError or Exception:
27+
return False
28+
29+
30+
def delete_directory_product(name_product) -> bool:
31+
path = f"/app/integrations/images_products/{name_product}"
32+
if os.path.exists(path):
33+
shutil.rmtree(path)
34+
return True
35+
return False
36+
37+
def add_image(name_product, file) -> str:
2038
new_name_file = generate_new_name_image(file.filename)
2139
create_new_directory_product(name_product)
2240
file_loc = os.path.join(f"/app/integrations/images_products/{name_product}", new_name_file)
2341
with open(file_loc, "wb") as buffer:
2442
shutil.copyfileobj(file.file, buffer)
2543
return new_name_file
26-
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import smtplib
2+
import os
3+
from dotenv import load_dotenv
4+
from fastapi import HTTPException, status
5+
from email.mime.text import MIMEText
6+
from random import randint
7+
8+
load_dotenv()
9+
sender_email = os.getenv('SENDER_EMAIL')
10+
password_email = os.getenv('PASSWORD')
11+
12+
class Password_Recover_Email():
13+
server = smtplib.SMTP("smtp.gmail.com", 587)
14+
server.starttls()
15+
16+
def create_code(self, gmail: str):
17+
os.makedirs(f'/app/integrations/code_recover_password/{gmail}', exist_ok=True)
18+
with open(f'/app/integrations/code_recover_password/{gmail}/{gmail}.txt', 'w') as txt:
19+
txt.write(str(randint(100000, 999999)))
20+
txt.close
21+
22+
def get_code(self, gmail: str) -> int:
23+
with open(f'/app/integrations/code_recover_password/{gmail}/{gmail}.txt', 'r') as txt:
24+
code = txt.read()
25+
return int(code)
26+
27+
def send_email(self, name: str, gmail: str, code: int):
28+
email = MIMEText(
29+
f"""<!DOCTYPE html>
30+
<html lang="pt-BR">
31+
<body>
32+
<h1>Olá {name}!</h1>
33+
<h2> Use este código para recuperar sua senha.</h2>
34+
<h2>{code}</h2>
35+
</body>
36+
</html>
37+
""", 'html', 'utf-8'
38+
)
39+
email['Subject'] = 'Código de recuperação de senha'
40+
email['From'] = sender_email
41+
email['To'] = gmail
42+
try:
43+
self.server.login(email['From'], password_email)
44+
self.server.sendmail(email['From'], email['To'], email.as_string())
45+
return {'Response': 200}
46+
except Exception as e:
47+
raise HTTPException(status.HTTP_400_BAD_REQUEST, {'Fail': e})

src/backend/app/main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from fastapi import FastAPI
22
from db.config_db import lifespan
33
from routers import user_routes, admin_routes, client_app_routes, cart_routes
4+
from integrations.image_save import configure_image
45

56
def config_app():
67
app = FastAPI(lifespan=lifespan)
7-
app.include_router(user_routes.router_user)
8-
app.include_router(admin_routes.router_admin)
9-
app.include_router(client_app_routes.router_client_app)
10-
app.include_router(cart_routes.router_cart)
8+
app.include_router(router = user_routes.router_user, prefix="/user")
9+
app.include_router(router = admin_routes.router_admin, prefix="/admin")
10+
app.include_router(router = client_app_routes.router_client_app, prefix="/app")
11+
app.include_router(router = cart_routes.router_cart, prefix="/cart")
12+
app = configure_image(app)
1113
return app
1214

1315
app = config_app()

0 commit comments

Comments
 (0)