Skip to content

Commit

Permalink
[infra] - deploy config with nginx implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
davi-lucciola committed Jul 29, 2024
1 parent 6745244 commit df57f67
Show file tree
Hide file tree
Showing 27 changed files with 221 additions and 169 deletions.
43 changes: 22 additions & 21 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
name: Continuos Delivery - CD

on:
workflow_run:
workflows: ["Continuos Integration - CI"]
types:
- completed
# workflow_run:
# workflows: ["Continuos Integration - CI"]
# types:
# - requested
workflow_dispatch:

jobs:
aws:
runs-on: self-hosted

steps:
# Running API Container
- name: Pull Docker Image (API)
run: |
sudo docker image rm -f davilucciola/knockbank-api
sudo docker pull davilucciola/knockbank-api:latest
# steps:
# # Running API Container
# - name: Pull Docker Image (API)
# run: |
# sudo docker image rm -f davilucciola/knockbank-api
# sudo docker pull davilucciola/knockbank-api:latest

- name: Delete Old Container (API)
run: sudo docker rm -f knockbank-api || true
# - name: Delete Old Container (API)
# run: sudo docker rm -f knockbank-api || true

- name: Run Docker Container (API)
env:
TOKEN_SECRET: ${{ secrets.TOKEN_SECRET }}
SQLALCHEMY_DATABASE_URI: ${{ secrets.SQLALCHEMY_DATABASE_URI }}
run: |
sudo docker run --name knockbank-api -p 5000:5000 --restart=always \
-e TOKEN_SECRET=$TOKEN_SECRET \
-e SQLALCHEMY_DATABASE_URI=$SQLALCHEMY_DATABASE_URI \
-d davilucciola/knockbank-api
# - name: Run Docker Container (API)
# env:
# TOKEN_SECRET: ${{ secrets.TOKEN_SECRET }}
# SQLALCHEMY_DATABASE_URI: ${{ secrets.SQLALCHEMY_DATABASE_URI }}
# run: |
# sudo docker run --name knockbank-api -p 5000:5000 --restart=always \
# -e TOKEN_SECRET=$TOKEN_SECRET \
# -e SQLALCHEMY_DATABASE_URI=$SQLALCHEMY_DATABASE_URI \
# -d davilucciola/knockbank-api
58 changes: 47 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,57 @@ jobs:
steps:
- uses: actions/checkout@v3

# Login Dockerhub
- name: Login Dockerhub
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
run: docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD
# API - Build
- name: Using Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install Dependencies
run: |
cd server
pip install --upgrade pip
pip install --no-cache-dir poetry
poetry config virtualenvs.create false
poetry install
# API - Docker Image
- name: Build Docker Image (API)
# API - Tests
- name: Running Tests
run: |
cd server
docker build -t davilucciola/knockbank-api .
poetry run pytest
env:
TOKEN_SECRET: testkey

# Web - Build
- name: Using NodeJS
uses: actions/setup-node@v4
with:
node-version: "20.8.0"
- name: Building NextJS
run: |
cd client
npm install
npm run build
# Web - Tests
# Work In Progress


# Login Dockerhub
# - name: Login Dockerhub
# env:
# DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
# DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
# run: docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD

# # API - Docker Image
# - name: Build Docker Image (API)
# run: |
# cd server
# docker build -t davilucciola/knockbank-api .

- name: Push Docker Image (API)
run: docker push davilucciola/knockbank-api:latest
# - name: Push Docker Image (API)
# run: docker push davilucciola/knockbank-api:latest

# Web - Docker Image
# - name: Build Web Image
Expand Down
47 changes: 0 additions & 47 deletions .github/workflows/test.yml

This file was deleted.

6 changes: 3 additions & 3 deletions client/src/modules/account/components/my-account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ import { ArrowLeft, Lock, Pencil, User } from "@phosphor-icons/react/dist/ssr";
import { AccountContext } from "@/modules/account/contexts/account-context";
import { useToast } from "@/components/ui/use-toast";
import { AuthContext } from "@/modules/auth/contexts/auth-context";
import { useUnauthorizedHandler } from "@/modules/auth/hooks/use-unauthorized-handler";
import { useRouter } from "next/navigation";

export function MyAccount({ account }: { account: Account | null }) {
const router = useRouter();
const { toast } = useToast();
const { logout } = useContext(AuthContext);
const { verifyToken } = useUnauthorizedHandler();
const { fetchAccount, updateAccount, blockAccount } =
useContext(AccountContext);
const [open, setOpen] = useState<boolean>(false);
Expand Down Expand Up @@ -104,7 +104,7 @@ export function MyAccount({ account }: { account: Account | null }) {
duration: toastDurationInMiliseconds,
});
await logout();
verifyToken();
router.push("/");
} catch (error) {
if (error instanceof Error) {
toast({
Expand Down
4 changes: 4 additions & 0 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM nginx:alpine
RUN rm /etc/nginx/conf.d/default.conf
WORKDIR /etc/nginx/conf.d/
COPY ./nginx.conf /etc/nginx/conf.d/
29 changes: 29 additions & 0 deletions deploy/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
upstream knockbank-api {
server api:5000;
}

upstream knockbank-web {
server web:3000;
}

server {
listen 80;

location / {
proxy_pass http://knockbank-web;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';
proxy_cache_bypass $http_upgrade;
}

location /api {
proxy_pass http://knockbank-api;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';
proxy_cache_bypass $http_upgrade;
}
}
40 changes: 24 additions & 16 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ version: '3.9'


services:
web:
container_name: knockbank-web
build:
context: ./client
ports:
- "3000:3000"

api:
container_name: knockbank-api
build:
context: ./server
ports:
- "5000:5000"

db:
image: mysql:5.7
container_name: knockbank-db
Expand All @@ -24,10 +10,32 @@ services:
MYSQL_USER: 'admin'
MYSQL_PASSWORD: 'admin'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3306:3306"
volumes:
- mysql-db:/var/lib/mysql

api:
container_name: knockbank-api
build:
context: ./server
depends_on:
- db

web:
container_name: knockbank-web
build:
context: ./client
depends_on:
- api

proxy:
container_name: nginx-proxy
restart: always
build:
context: ./deploy
depends_on:
- web
ports:
- "80:80"

volumes:
mysql-db:
23 changes: 13 additions & 10 deletions server/knockbankapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import tomllib
from typing import Type
from apiflask import APIFlask
from flask_cors import CORS
Expand All @@ -8,7 +7,13 @@

def create_app(config: Type[Config] = Config) -> APIFlask:
# Flask APP
app = APIFlask(__name__, title=Config.API_TITLE)
app = APIFlask(
__name__,
title=Config.API_TITLE,
version=Config.API_VERSION,
docs_path="/api/docs",
spec_path="/api/openapi.json",
)

# Config
CORS(app, origins=["*"])
Expand All @@ -17,15 +22,13 @@ def create_app(config: Type[Config] = Config) -> APIFlask:
app.config.from_object(config)

# Info
@app.get("/info")
@app.get(f"/api/info")
def info():
with open("./pyproject.toml", mode="rb") as pyproject:
version = tomllib.load(pyproject)["tool"]["poetry"]["version"]
return {
"title": Config.API_TITLE,
"description": Config.DESCRIPTION,
"version": version,
}
return {
"title": Config.API_TITLE,
"description": Config.DESCRIPTION,
"version": Config.API_VERSION,
}

# Routers
app.register_blueprint(auth_bp)
Expand Down
2 changes: 1 addition & 1 deletion server/knockbankapi/app/controllers/account_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from knockbankapi.infra.repositories import TransactionRepository


account_bp = APIBlueprint("Account", __name__, url_prefix="/account")
account_bp = APIBlueprint("Account", __name__, url_prefix="/api/account")


@account_bp.get("/me")
Expand Down
2 changes: 1 addition & 1 deletion server/knockbankapi/app/controllers/auth_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from knockbankapi.domain.services import AuthService


auth_bp = APIBlueprint("Auth", __name__)
auth_bp = APIBlueprint("Auth", __name__, url_prefix="/api")


@auth_bp.post("/login")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from knockbankapi.domain.services import TransactionService


transaction_bp = APIBlueprint("Transaction", __name__, url_prefix="/transaction")
transaction_bp = APIBlueprint("Transaction", __name__, url_prefix="/api/transaction")


@transaction_bp.get("/")
Expand Down
2 changes: 1 addition & 1 deletion server/knockbankapi/app/schemas/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TransactionTransfer(TransactionIn):
class TransactionOut(Schema):
id: int = Integer()
money: float = Float()
dateTime: dt = DateTime("%Y-%m-%dT%H:%M:%S-03:00")
dateTime: dt = DateTime()
transactionType: int = Integer()
account: dict = Nested(PersonBasic)
originAccount: dict = Nested(PersonBasic)
Expand Down
7 changes: 3 additions & 4 deletions server/knockbankapi/domain/models/transaction.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from pytz import timezone
from datetime import datetime as dt
from decimal import Decimal
from sqlalchemy import Integer, DateTime, Numeric, ForeignKey
Expand All @@ -23,9 +24,7 @@ class Transaction(BaseModel):
__tablename__ = "transactions"

id: Mapped[int] = mapped_column(BigIntegerPK, primary_key=True, autoincrement=True)
date_time: Mapped[dt] = mapped_column(
DateTime, nullable=False, default=dt.now
)
date_time: Mapped[dt] = mapped_column(DateTime, nullable=False, default=dt.now)
money: Mapped[Decimal] = mapped_column(Numeric(10, 2), nullable=False)
transaction_type: Mapped[int] = mapped_column(Integer, nullable=False)

Expand Down Expand Up @@ -68,7 +67,7 @@ def to_json(self):
return {
"id": self.id,
"money": float(self.money),
"dateTime": self.date_time,
"dateTime": self.date_time.astimezone(timezone('America/Sao_Paulo')),
"transactionType": self.transaction_type,
"account": {
"id": self.account.id,
Expand Down
Loading

0 comments on commit df57f67

Please sign in to comment.