Skip to content

Commit

Permalink
creating fastAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Nov 9, 2024
1 parent c0f71c4 commit 322cc8d
Show file tree
Hide file tree
Showing 8 changed files with 2,085 additions and 0 deletions.
12 changes: 12 additions & 0 deletions api/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
echo "Logging in to ECR"
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com

echo "Building image"
docker build --no-cache --platform=linux/amd64 -t $REGISTRY_NAME .

echo "Tagging image"
docker tag $REGISTRY_NAME:$TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$REGISTRY_NAME:$TAG

echo "Pushing image to ECR"
docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$REGISTRY_NAME:$TAG
12 changes: 12 additions & 0 deletions api/dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM --platform=linux/amd64 python:3.11-slim

WORKDIR /app

COPY . /app

RUN pip install --no-cache-dir -r requirements.txt

EXPOSE 80

# Start the app with Python
CMD ["python", "-m", "uvicorn", "src.main:app", "--host", "0.0.0.0", "--port", "80"]
1,198 changes: 1,198 additions & 0 deletions api/poetry.lock

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[tool.poetry]
name = "trackflow-api"
version = "0.1.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.11"
fastapi = "^0.115.4"
uvicorn = "^0.32.0"
supabase = "^2.10.0"
python-dotenv = "^1.0.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
7 changes: 7 additions & 0 deletions api/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


docker build -t trackflow .
docker tag trackflow:latest 498969721544.dkr.ecr.us-east-1.amazonaws.com/trackflow:latest
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 498969721544.dkr.ecr.us-east-1.amazonaws.com
docker push 498969721544.dkr.ecr.us-east-1.amazonaws.com/trackflow:latest

736 changes: 736 additions & 0 deletions api/requirements.txt

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions api/src/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from fastapi import FastAPI, HTTPException
from src.supabase_client import TrainingWeek, get_training_week

app = FastAPI()


@app.get("/training_week/{athlete_id}", response_model=TrainingWeek)
async def training_week_endpoint(athlete_id: int):
"""
Retrieve the most recent training_week row by athlete_id.
"""
try:
return get_training_week(athlete_id)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))
89 changes: 89 additions & 0 deletions api/src/supabase_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from __future__ import annotations

import json
import os
from enum import StrEnum
from typing import List

from dotenv import load_dotenv
from pydantic import BaseModel, Field
from supabase import Client, create_client

load_dotenv()


class Day(StrEnum):
MON = "Mon"
TUES = "Tues"
WED = "Wed"
THURS = "Thurs"
FRI = "Fri"
SAT = "Sat"
SUN = "Sun"


class SessionType(StrEnum):
EASY = "easy run"
LONG = "long run"
SPEED = "speed workout"
REST = "rest day"
MODERATE = "moderate run"


class TrainingSession(BaseModel):
day: Day
session_type: SessionType
distance: float = Field(description="Distance in miles")
notes: str = Field(
description="Detailed yet concise notes about the session from the coach's perspective"
)
completed: bool = Field(description="Whether the session has been completed")


class TrainingWeek(BaseModel):
sessions: List[TrainingSession]

@property
def total_mileage(self) -> float:
return sum(session.distance for session in self.sessions)

@property
def progress(self) -> float:
return (self.completed_sessions.total_mileage / self.total_mileage) * 100

@property
def completed_sessions(self) -> TrainingWeek:
return TrainingWeek(
sessions=[session for session in self.sessions if session.completed is True]
)


def init() -> Client:
url = os.getenv("SUPABASE_URL")
key = os.getenv("SUPABASE_KEY")
return create_client(url, key)


client = init()


def get_training_week(athlete_id: int) -> TrainingWeek:
"""
Get the most recent training_week row by athlete_id.
"""
table = client.table("training_week")
response = (
table.select("training_week")
.eq("athlete_id", athlete_id)
.order("created_at", desc=True)
.limit(1)
.execute()
)

try:
response_data = response.data[0]
return TrainingWeek(**json.loads(response_data["training_week"]))
except IndexError:
raise ValueError(
f"Could not find training_week row for athlete_id {athlete_id}"
)

0 comments on commit 322cc8d

Please sign in to comment.