Skip to content

Commit 6565b39

Browse files
committed
updates
1 parent 13cff42 commit 6565b39

17 files changed

+54
-681
lines changed

.github/workflows/main.yml

Lines changed: 13 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,19 @@ jobs:
1919
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
2020
env:
2121
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22-
- name: Pull images
22+
- name: Pull image
2323
run: |
24-
docker pull ${{ env.IMAGE }}-builder:latest || true
25-
docker pull ${{ env.IMAGE }}-final:latest || true
26-
- name: Build images
24+
docker pull ${{ env.IMAGE }}:latest || true
25+
- name: Build image
2726
run: |
2827
docker build \
29-
--target builder \
30-
--cache-from ${{ env.IMAGE }}-builder:latest \
31-
--tag ${{ env.IMAGE }}-builder:latest \
32-
--file ./project/Dockerfile.prod \
33-
"./project"
34-
docker build \
35-
--cache-from ${{ env.IMAGE }}-final:latest \
36-
--tag ${{ env.IMAGE }}-final:latest \
28+
--cache-from ${{ env.IMAGE }}:latest \
29+
--tag ${{ env.IMAGE }}:latest \
3730
--file ./project/Dockerfile.prod \
3831
"./project"
39-
- name: Push images
32+
- name: Push image
4033
run: |
41-
docker push ${{ env.IMAGE }}-builder:latest
42-
docker push ${{ env.IMAGE }}-final:latest
34+
docker push ${{ env.IMAGE }}:latest
4335
4436
test:
4537
name: Test Docker Image
@@ -54,21 +46,14 @@ jobs:
5446
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
5547
env:
5648
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
57-
- name: Pull images
49+
- name: Pull image
5850
run: |
59-
docker pull ${{ env.IMAGE }}-builder:latest || true
60-
docker pull ${{ env.IMAGE }}-final:latest || true
61-
- name: Build images
51+
docker pull ${{ env.IMAGE }}:latest || true
52+
- name: Build image
6253
run: |
6354
docker build \
64-
--target builder \
65-
--cache-from ${{ env.IMAGE }}-builder:latest \
66-
--tag ${{ env.IMAGE }}-builder:latest \
67-
--file ./project/Dockerfile.prod \
68-
"./project"
69-
docker build \
70-
--cache-from ${{ env.IMAGE }}-final:latest \
71-
--tag ${{ env.IMAGE }}-final:latest \
55+
--cache-from ${{ env.IMAGE }}:latest \
56+
--tag ${{ env.IMAGE }}:latest \
7257
--file ./project/Dockerfile.prod \
7358
"./project"
7459
- name: Run container
@@ -81,9 +66,7 @@ jobs:
8166
-e DATABASE_URL=sqlite://sqlite.db \
8267
-e DATABASE_TEST_URL=sqlite://sqlite.db \
8368
-p 5003:8765 \
84-
${{ env.IMAGE }}-final:latest
85-
- name: Install requirements
86-
run: docker exec fastapi-tdd pip install black==23.12.1 flake8==7.0.0 isort==5.13.2 pytest==7.4.4
69+
${{ env.IMAGE }}:latest
8770
- name: Pytest
8871
run: docker exec fastapi-tdd python -m pytest .
8972
- name: Flake8
@@ -92,52 +75,3 @@ jobs:
9275
run: docker exec fastapi-tdd python -m black . --check
9376
- name: isort
9477
run: docker exec fastapi-tdd python -m isort . --check-only
95-
96-
deploy:
97-
name: Deploy to Heroku
98-
runs-on: ubuntu-latest
99-
needs: [build, test]
100-
env:
101-
HEROKU_APP_NAME: quiet-citadel-80656
102-
HEROKU_REGISTRY_IMAGE: registry.heroku.com/${HEROKU_APP_NAME}/summarizer
103-
steps:
104-
- name: Checkout
105-
uses: actions/checkout@v3
106-
with:
107-
ref: main
108-
- name: Log in to GitHub Packages
109-
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin ghcr.io
110-
env:
111-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
112-
- name: Pull images
113-
run: |
114-
docker pull ${{ env.IMAGE }}-builder:latest || true
115-
docker pull ${{ env.IMAGE }}-final:latest || true
116-
- name: Build images
117-
run: |
118-
docker build \
119-
--target builder \
120-
--cache-from ${{ env.IMAGE }}-builder:latest \
121-
--tag ${{ env.IMAGE }}-builder:latest \
122-
--file ./project/Dockerfile.prod \
123-
"./project"
124-
docker build \
125-
--cache-from ${{ env.IMAGE }}-final:latest \
126-
--tag ${{ env.IMAGE }}:latest \
127-
--tag ${{ env.HEROKU_REGISTRY_IMAGE }}:latest \
128-
--file ./project/Dockerfile.prod \
129-
"./project"
130-
- name: Log in to the Heroku Container Registry
131-
run: docker login -u _ -p ${HEROKU_AUTH_TOKEN} registry.heroku.com
132-
env:
133-
HEROKU_AUTH_TOKEN: ${{ secrets.HEROKU_AUTH_TOKEN }}
134-
- name: Push to the registry
135-
run: docker push ${{ env.HEROKU_REGISTRY_IMAGE }}:latest
136-
- name: Set environment variables
137-
run: |
138-
echo "HEROKU_REGISTRY_IMAGE=${{ env.HEROKU_REGISTRY_IMAGE }}" >> $GITHUB_ENV
139-
echo "HEROKU_AUTH_TOKEN=${{ secrets.HEROKU_AUTH_TOKEN }}" >> $GITHUB_ENV
140-
- name: Release
141-
run: |
142-
chmod +x ./release.sh
143-
./release.sh

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.8'
2-
31
services:
42

53
web:

project/Dockerfile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pull official base image
2-
FROM python:3.12.1-slim-bookworm
2+
FROM python:3.13.3-slim-bookworm
33

44
# set working directory
55
WORKDIR /usr/src/app
@@ -16,8 +16,7 @@ RUN apt-get update \
1616
# install python dependencies
1717
RUN pip install --upgrade pip
1818
COPY ./requirements.txt .
19-
COPY ./requirements-dev.txt .
20-
RUN pip install -r requirements-dev.txt
19+
RUN pip install -r requirements.txt
2120

2221
# add app
2322
COPY . .

project/Dockerfile.prod

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
1-
###########
2-
# BUILDER #
3-
###########
4-
5-
# pull official base image
6-
FROM python:3.12.1-slim-bookworm as builder
7-
8-
# install system dependencies
9-
RUN apt-get update \
10-
&& apt-get -y install netcat-traditional gcc postgresql \
11-
&& apt-get clean
12-
13-
# set work directory
14-
WORKDIR /usr/src/app
15-
16-
# set environment variables
17-
ENV PYTHONDONTWRITEBYTECODE 1
18-
ENV PYTHONUNBUFFERED 1
19-
20-
# install dependencies
21-
RUN pip install --upgrade pip
22-
COPY ./requirements.txt .
23-
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /usr/src/app/wheels -r requirements.txt
24-
25-
# lint
26-
COPY . /usr/src/app/
27-
RUN pip install black==23.12.1 flake8==7.0.0 isort==5.13.2
28-
RUN flake8 .
29-
RUN black --exclude=migrations . --check
30-
RUN isort . --check-only
31-
32-
33-
#########
34-
# FINAL #
35-
#########
36-
371
# pull official base image
38-
FROM python:3.12.1-slim-bookworm
2+
FROM python:3.13.3-slim-bookworm
393

404
# create directory for the app user
415
RUN mkdir -p /home/app
@@ -61,17 +25,16 @@ RUN apt-get update \
6125
&& apt-get clean
6226

6327
# install python dependencies
64-
COPY --from=builder /usr/src/app/wheels /wheels
65-
COPY --from=builder /usr/src/app/requirements.txt .
6628
RUN pip install --upgrade pip
67-
RUN pip install --no-cache /wheels/*
68-
RUN pip install "uvicorn[standard]==0.26.0"
29+
COPY ./requirements.txt .
30+
RUN pip install -r requirements.txt
31+
RUN pip install "uvicorn[standard]==0.34.1"
6932

7033
# add app
7134
COPY . .
7235

7336
# chown all the files to the app user
74-
RUN chown -R app:app $HOME
37+
RUN chown -R app:app $APP_HOME
7538

7639
# change to the app user
7740
USER app

project/app/api/crud.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,9 @@ async def get_all() -> List:
1717

1818

1919
async def post(payload: SummaryPayloadSchema) -> int:
20-
summary = TextSummary(url=payload.url, summary="")
20+
summary = TextSummary(
21+
url=payload.url,
22+
summary="dummy summary",
23+
)
2124
await summary.save()
2225
return summary.id
23-
24-
25-
async def put(id: int, payload: SummaryPayloadSchema) -> Union[dict, None]:
26-
summary = await TextSummary.filter(id=id).update(
27-
url=payload.url, summary=payload.summary
28-
)
29-
if summary:
30-
updated_summary = await TextSummary.filter(id=id).first().values()
31-
return updated_summary
32-
return None
33-
34-
35-
async def delete(id: int) -> int:
36-
summary = await TextSummary.filter(id=id).first().delete()
37-
return summary

project/app/api/summaries.py

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
from typing import List
22

3-
from fastapi import APIRouter, BackgroundTasks, HTTPException, Path
3+
from fastapi import APIRouter, HTTPException
44

55
from app.api import crud
6+
from app.models.pydantic import SummaryPayloadSchema, SummaryResponseSchema
67
from app.models.tortoise import SummarySchema
7-
from app.summarizer import generate_summary
8-
9-
from app.models.pydantic import ( # isort:skip
10-
SummaryPayloadSchema,
11-
SummaryResponseSchema,
12-
SummaryUpdatePayloadSchema,
13-
)
148

159
router = APIRouter()
1610

1711

1812
@router.get("/{id}/", response_model=SummarySchema)
19-
async def read_summary(id: int = Path(..., gt=0)) -> SummarySchema:
13+
async def read_summary(id: int) -> SummarySchema:
2014
summary = await crud.get(id)
2115
if not summary:
2216
raise HTTPException(status_code=404, detail="Summary not found")
@@ -30,34 +24,8 @@ async def read_all_summaries() -> List[SummarySchema]:
3024

3125

3226
@router.post("/", response_model=SummaryResponseSchema, status_code=201)
33-
async def create_summary(
34-
payload: SummaryPayloadSchema, background_tasks: BackgroundTasks
35-
) -> SummaryResponseSchema:
27+
async def create_summary(payload: SummaryPayloadSchema) -> SummaryResponseSchema:
3628
summary_id = await crud.post(payload)
3729

38-
background_tasks.add_task(generate_summary, summary_id, str(payload.url))
39-
4030
response_object = {"id": summary_id, "url": payload.url}
4131
return response_object
42-
43-
44-
@router.put("/{id}/", response_model=SummarySchema)
45-
async def update_summary(
46-
payload: SummaryUpdatePayloadSchema, id: int = Path(..., gt=0)
47-
) -> SummarySchema:
48-
summary = await crud.put(id, payload)
49-
if not summary:
50-
raise HTTPException(status_code=404, detail="Summary not found")
51-
52-
return summary
53-
54-
55-
@router.delete("/{id}/", response_model=SummaryResponseSchema)
56-
async def delete_summary(id: int = Path(..., gt=0)) -> SummaryResponseSchema:
57-
summary = await crud.get(id)
58-
if not summary:
59-
raise HTTPException(status_code=404, detail="Summary not found")
60-
61-
await crud.delete(id)
62-
63-
return summary

project/app/main.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,4 @@ def create_application() -> FastAPI:
2020

2121
app = create_application()
2222

23-
24-
@app.on_event("startup")
25-
async def startup_event():
26-
log.info("Starting up...")
27-
init_db(app)
28-
29-
30-
@app.on_event("shutdown")
31-
async def shutdown_event():
32-
log.info("Shutting down...")
23+
init_db(app)

project/app/models/pydantic.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
from pydantic import AnyHttpUrl, BaseModel
1+
from pydantic import BaseModel
22

33

44
class SummaryPayloadSchema(BaseModel):
5-
url: AnyHttpUrl
5+
url: str
66

77

88
class SummaryResponseSchema(SummaryPayloadSchema):
99
id: int
10-
11-
12-
class SummaryUpdatePayloadSchema(SummaryPayloadSchema):
13-
summary: str

project/app/summarizer.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

project/db/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pull official base image
2-
FROM postgres:16
2+
FROM postgres:17
33

44
# run create.sql on init
55
ADD create.sql /docker-entrypoint-initdb.d

project/migrations/models/0_20240122171233_init.py renamed to project/migrations/models/0_20250421234125_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async def upgrade(db: BaseDBAsyncClient) -> str:
77
"id" SERIAL NOT NULL PRIMARY KEY,
88
"url" TEXT NOT NULL,
99
"summary" TEXT NOT NULL,
10-
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
10+
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
1111
);
1212
CREATE TABLE IF NOT EXISTS "aerich" (
1313
"id" SERIAL NOT NULL PRIMARY KEY,

project/requirements-dev.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)