Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhea0 committed Jan 23, 2024
1 parent ede52f0 commit 8e94bc0
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 641 deletions.
96 changes: 15 additions & 81 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,24 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
with:
ref: main
ref: updates
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull images
- name: Pull image
run: |
docker pull ${{ env.IMAGE }}-builder:latest || true
docker pull ${{ env.IMAGE }}-final:latest || true
- name: Build images
docker pull ${{ env.IMAGE }}:latest || true
- name: Build image
run: |
docker build \
--target builder \
--cache-from ${{ env.IMAGE }}-builder:latest \
--tag ${{ env.IMAGE }}-builder:latest \
--file ./project/Dockerfile.prod \
"./project"
docker build \
--cache-from ${{ env.IMAGE }}-final:latest \
--tag ${{ env.IMAGE }}-final:latest \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.IMAGE }}:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Push images
- name: Push image
run: |
docker push ${{ env.IMAGE }}-builder:latest
docker push ${{ env.IMAGE }}-final:latest
docker push ${{ env.IMAGE }}:latest
test:
name: Test Docker Image
Expand All @@ -49,26 +41,19 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
with:
ref: main
ref: updates
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull images
- name: Pull image
run: |
docker pull ${{ env.IMAGE }}-builder:latest || true
docker pull ${{ env.IMAGE }}-final:latest || true
- name: Build images
docker pull ${{ env.IMAGE }}:latest || true
- name: Build image
run: |
docker build \
--target builder \
--cache-from ${{ env.IMAGE }}-builder:latest \
--tag ${{ env.IMAGE }}-builder:latest \
--file ./project/Dockerfile.prod \
"./project"
docker build \
--cache-from ${{ env.IMAGE }}-final:latest \
--tag ${{ env.IMAGE }}-final:latest \
--cache-from ${{ env.IMAGE }}:latest \
--tag ${{ env.IMAGE }}:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Run container
Expand All @@ -81,9 +66,7 @@ jobs:
-e DATABASE_URL=sqlite://sqlite.db \
-e DATABASE_TEST_URL=sqlite://sqlite.db \
-p 5003:8765 \
${{ env.IMAGE }}-final:latest
- name: Install requirements
run: docker exec fastapi-tdd pip install black==23.1.0 flake8==6.0.0 isort==5.12.0 pytest==7.2.2
${{ env.IMAGE }}:latest
- name: Pytest
run: docker exec fastapi-tdd python -m pytest .
- name: Flake8
Expand All @@ -92,52 +75,3 @@ jobs:
run: docker exec fastapi-tdd python -m black . --check
- name: isort
run: docker exec fastapi-tdd python -m isort . --check-only

deploy:
name: Deploy to Heroku
runs-on: ubuntu-latest
needs: [build, test]
env:
HEROKU_APP_NAME: radiant-everglades-49858
HEROKU_REGISTRY_IMAGE: registry.heroku.com/${HEROKU_APP_NAME}/summarizer
steps:
- name: Checkout
uses: actions/checkout@v3
with:
ref: main
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull images
run: |
docker pull ${{ env.IMAGE }}-builder:latest || true
docker pull ${{ env.IMAGE }}-final:latest || true
- name: Build images
run: |
docker build \
--target builder \
--cache-from ${{ env.IMAGE }}-builder:latest \
--tag ${{ env.IMAGE }}-builder:latest \
--file ./project/Dockerfile.prod \
"./project"
docker build \
--cache-from ${{ env.IMAGE }}-final:latest \
--tag ${{ env.IMAGE }}:latest \
--tag ${{ env.HEROKU_REGISTRY_IMAGE }}:latest \
--file ./project/Dockerfile.prod \
"./project"
- name: Log in to the Heroku Container Registry
run: docker login -u _ -p ${HEROKU_AUTH_TOKEN} registry.heroku.com
env:
HEROKU_AUTH_TOKEN: ${{ secrets.HEROKU_AUTH_TOKEN }}
- name: Push to the registry
run: docker push ${{ env.HEROKU_REGISTRY_IMAGE }}:latest
- name: Set environment variables
run: |
echo "HEROKU_REGISTRY_IMAGE=${{ env.HEROKU_REGISTRY_IMAGE }}" >> $GITHUB_ENV
echo "HEROKU_AUTH_TOKEN=${{ secrets.HEROKU_AUTH_TOKEN }}" >> $GITHUB_ENV
- name: Release
run: |
chmod +x ./release.sh
./release.sh
7 changes: 3 additions & 4 deletions project/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# pull official base image
FROM python:3.11.2-slim-buster
FROM python:3.12.1-slim-bookworm

# set working directory
WORKDIR /usr/src/app
Expand All @@ -10,14 +10,13 @@ ENV PYTHONUNBUFFERED 1

# install system dependencies
RUN apt-get update \
&& apt-get -y install netcat gcc postgresql \
&& apt-get -y install netcat-traditional gcc postgresql \
&& apt-get clean

# install python dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
COPY ./requirements-dev.txt .
RUN pip install -r requirements-dev.txt
RUN pip install -r requirements.txt

# add app
COPY . .
Expand Down
49 changes: 6 additions & 43 deletions project/Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -1,41 +1,5 @@
###########
# BUILDER #
###########

# pull official base image
FROM python:3.11.2-slim-buster as builder

# install system dependencies
RUN apt-get update \
&& apt-get -y install gcc postgresql \
&& apt-get clean

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt

# lint
COPY . /usr/src/app/
RUN pip install black==23.1.0 flake8==6.0.0 isort==5.12.0
RUN flake8 .
RUN black --exclude=migrations .
RUN isort .


#########
# FINAL #
#########

# pull official base image
FROM python:3.11.2-slim-buster
FROM python:3.12.1-slim-bookworm

# create directory for the app user
RUN mkdir -p /home/app
Expand All @@ -57,21 +21,20 @@ ENV TESTING 0

# install system dependencies
RUN apt-get update \
&& apt-get -y install netcat gcc postgresql \
&& apt-get -y install netcat-traditional gcc postgresql \
&& apt-get clean

# install python dependencies
COPY --from=builder /usr/src/app/wheels /wheels
COPY --from=builder /usr/src/app/requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache /wheels/*
RUN pip install "uvicorn[standard]==0.21.1"
COPY ./requirements.txt .
RUN pip install -r requirements.txt
RUN pip install "uvicorn[standard]==0.26.0"

# add app
COPY . .

# chown all the files to the app user
RUN chown -R app:app $HOME
RUN chown -R app:app $APP_HOME

# change to the app user
USER app
Expand Down
30 changes: 9 additions & 21 deletions project/app/api/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,22 @@
from app.models.tortoise import TextSummary


async def get_all() -> List:
summaries = await TextSummary.all().values()
return summaries


async def get(id: int) -> Union[dict, None]:
summary = await TextSummary.filter(id=id).first().values()
if summary:
return summary
return None


async def post(payload: SummaryPayloadSchema) -> int:
summary = TextSummary(url=payload.url, summary="")
await summary.save()
return summary.id
async def get_all() -> List:
summaries = await TextSummary.all().values()
return summaries


async def put(id: int, payload: SummaryPayloadSchema) -> Union[dict, None]:
summary = await TextSummary.filter(id=id).update(
url=payload.url, summary=payload.summary
async def post(payload: SummaryPayloadSchema) -> int:
summary = TextSummary(
url=payload.url,
summary="dummy summary",
)
if summary:
updated_summary = await TextSummary.filter(id=id).first().values()
return updated_summary
return None


async def delete(id: int) -> int:
summary = await TextSummary.filter(id=id).first().delete()
return summary
await summary.save()
return summary.id
50 changes: 9 additions & 41 deletions project/app/api/summaries.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,31 @@
from typing import List

from fastapi import APIRouter, BackgroundTasks, HTTPException, Path
from fastapi import APIRouter, HTTPException

from app.api import crud
from app.models.pydantic import SummaryPayloadSchema, SummaryResponseSchema
from app.models.tortoise import SummarySchema
from app.summarizer import generate_summary

from app.models.pydantic import ( # isort:skip
SummaryPayloadSchema,
SummaryResponseSchema,
SummaryUpdatePayloadSchema,
)

router = APIRouter()


@router.get("/", response_model=List[SummarySchema])
async def read_all_summaries() -> List[SummarySchema]:
return await crud.get_all()


@router.get("/{id}/", response_model=SummarySchema)
async def read_summary(id: int = Path(..., gt=0)) -> SummarySchema:
async def read_summary(id: int) -> SummarySchema:
summary = await crud.get(id)
if not summary:
raise HTTPException(status_code=404, detail="Summary not found")

return summary


@router.get("/", response_model=List[SummarySchema])
async def read_all_summaries() -> List[SummarySchema]:
return await crud.get_all()


@router.post("/", response_model=SummaryResponseSchema, status_code=201)
async def create_summary(
payload: SummaryPayloadSchema, background_tasks: BackgroundTasks
) -> SummaryResponseSchema:
async def create_summary(payload: SummaryPayloadSchema) -> SummaryResponseSchema:
summary_id = await crud.post(payload)

background_tasks.add_task(generate_summary, summary_id, payload.url)

response_object = {"id": summary_id, "url": payload.url}
return response_object


@router.put("/{id}/", response_model=SummarySchema)
async def update_summary(
payload: SummaryUpdatePayloadSchema, id: int = Path(..., gt=0)
) -> SummarySchema:
summary = await crud.put(id, payload)
if not summary:
raise HTTPException(status_code=404, detail="Summary not found")

return summary


@router.delete("/{id}/", response_model=SummaryResponseSchema)
async def delete_summary(id: int = Path(..., gt=0)) -> SummaryResponseSchema:
summary = await crud.get(id)
if not summary:
raise HTTPException(status_code=404, detail="Summary not found")

await crud.delete(id)

return summary
3 changes: 2 additions & 1 deletion project/app/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
from functools import lru_cache

from pydantic import AnyUrl, BaseSettings
from pydantic import AnyUrl
from pydantic_settings import BaseSettings

log = logging.getLogger("uvicorn")

Expand Down
8 changes: 2 additions & 6 deletions project/app/models/pydantic.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from pydantic import AnyHttpUrl, BaseModel
from pydantic import BaseModel


class SummaryPayloadSchema(BaseModel):
url: AnyHttpUrl
url: str


class SummaryResponseSchema(SummaryPayloadSchema):
id: int


class SummaryUpdatePayloadSchema(SummaryPayloadSchema):
summary: str
21 changes: 0 additions & 21 deletions project/app/summarizer.py

This file was deleted.

Loading

0 comments on commit 8e94bc0

Please sign in to comment.