diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..57eba7d --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b2acbd2..0cb1a8e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,4 +1,4 @@ -name: ci +name: docker-push on: push: @@ -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 \ No newline at end of file + tags: ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest diff --git a/app.py b/app.py index fdce85d..c23ed0b 100644 --- a/app.py +++ b/app.py @@ -4,11 +4,12 @@ import datetime from pydantic import BaseModel + app = FastAPI() -origins = [ - "http://localhost:5173" -] + +origins = ["http://localhost:5173"] + app.add_middleware( CORSMiddleware, @@ -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: @@ -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: @@ -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: @@ -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 = ?; """ @@ -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)} \ No newline at end of file + return {"error": str(e)} diff --git a/docker-python-kubernetes.yaml b/docker-python-kubernetes.yaml index 8b1d4ba..0ec8f12 100644 --- a/docker-python-kubernetes.yaml +++ b/docker-python-kubernetes.yaml @@ -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 @@ -32,5 +31,5 @@ spec: selector: app: weighttracker ports: - - port: 8000 - nodePort: 30001 + - port: 8000 + nodePort: 30001