Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ci workflow #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Continuous Integration

on:
pull_request:
branches: [main]
workflow_dispatch:

jobs:
python_ci:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: [3.11, 3.12]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 mypy
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

- name: Lint with flake8
run: |
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=120 --statistics

- name: Check types with mypy
run: mypy app.py
16 changes: 6 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ci
name: docker-push

on:
push:
Expand All @@ -9,22 +9,18 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
-
name: Checkout
- name: Checkout
uses: actions/checkout@v4
-
name: Login to Docker Hub
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Set up Docker Buildx
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Build and push
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest
tags: ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest
37 changes: 25 additions & 12 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import datetime
from pydantic import BaseModel


app = FastAPI()

origins = [
"http://localhost:5173"
]

origins = ["http://localhost:5173"]


app.add_middleware(
CORSMiddleware,
Expand All @@ -18,28 +19,34 @@
allow_headers=["*"],
)


appDB = "weight_tracker.db"


class Weight(BaseModel):
id: int
weight: float
timestamp: datetime.datetime


class User(BaseModel):
id: int
name: str
height: float


class Bmi(BaseModel):
id: int
weight: float
height: float
bmi: float


@app.get("/")
async def read_root():
return {"message": "Welcome to the weight tracker!"}


@app.post("/id/{id}/log_weight")
async def log_weight(id: int, weight: float):
try:
Expand All @@ -54,21 +61,25 @@ async def log_weight(id: int, weight: float):
return {"message": "Logging weight tracking data", "data": data}
except Exception as e:
return {"error": str(e)}



@app.get("/id/{id}/latest_weight", response_model=Weight)
async def get_latest_weight(id: int):
try:
query = "SELECT * FROM weight WHERE id = ? ORDER BY timestamp DESC LIMIT 1"
query = """
SELECT * FROM weight WHERE id = ? ORDER BY timestamp DESC LIMIT 1
"""
connection = sqlite3.connect(appDB)
cursor = connection.cursor()
result = cursor.execute(query, (id,))
data = result.fetchone()
connection.close()
weight_data = Weight(id=data[0], weight=data[1], timestamp=data[2] )
weight_data = Weight(id=data[0], weight=data[1], timestamp=data[2])
return weight_data.model_dump()
except Exception as e:
return {"error": str(e)}



@app.get("/id/{id}/user", response_model=User)
async def get_user(id: int):
try:
Expand All @@ -78,12 +89,13 @@ async def get_user(id: int):
result = cursor.execute(query, (id,))
data = result.fetchone()
connection.close()
user_data = User(id=data[0], name=data[1], height=data[2] )
user_data = User(id=data[0], name=data[1], height=data[2])
print(user_data.model_dump().get("name"))
return user_data.model_dump()
except Exception as e:
return {"error": str(e)}



@app.get("/id/{id}/bmi", response_model=Bmi)
async def get_bmi(id: int):
try:
Expand All @@ -96,13 +108,14 @@ async def get_bmi(id: int):
return bmi_data.model_dump()
except Exception as e:
return {"error": str(e)}



@app.post("/id/{id}/update_user")
async def update_user(id: int, name: str, height: float):
try:
data = (name, height, id)
query = """
UPDATE user
UPDATE user
SET name = ?, height = ?
WHERE id = ?;
"""
Expand All @@ -113,4 +126,4 @@ async def update_user(id: int, name: str, height: float):
connection.close()
return {"message": "Updated user data", "data": data}
except Exception as e:
return {"error": str(e)}
return {"error": str(e)}
15 changes: 7 additions & 8 deletions docker-python-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ spec:
app: weighttracker
spec:
containers:
- name: weighttracker
image: gianniacquisto/weight-tracker
imagePullPolicy: Always
ports:
- containerPort: 8000

- name: weighttracker
image: gianniacquisto/weight-tracker
imagePullPolicy: Always
ports:
- containerPort: 8000

---
apiVersion: v1
Expand All @@ -32,5 +31,5 @@ spec:
selector:
app: weighttracker
ports:
- port: 8000
nodePort: 30001
- port: 8000
nodePort: 30001
Loading