Skip to content

Commit 1c1a967

Browse files
committed
updates
1 parent ec51c24 commit 1c1a967

15 files changed

Lines changed: 77 additions & 631 deletions

File tree

.github/workflows/main.yml

Lines changed: 13 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,19 @@ jobs:
1717
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
1818
env:
1919
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20-
- name: Pull images
20+
- name: Pull image
2121
run: |
22-
docker pull ${{ env.IMAGE }}-builder:latest || true
23-
docker pull ${{ env.IMAGE }}-final:latest || true
24-
- name: Build images
22+
docker pull ${{ env.IMAGE }}:latest || true
23+
- name: Build image
2524
run: |
2625
docker build \
27-
--target builder \
28-
--cache-from ${{ env.IMAGE }}-builder:latest \
29-
--tag ${{ env.IMAGE }}-builder:latest \
30-
--file ./project/Dockerfile.prod \
31-
"./project"
32-
docker build \
33-
--cache-from ${{ env.IMAGE }}-final:latest \
34-
--tag ${{ env.IMAGE }}-final:latest \
26+
--cache-from ${{ env.IMAGE }}:latest \
27+
--tag ${{ env.IMAGE }}:latest \
3528
--file ./project/Dockerfile.prod \
3629
"./project"
37-
- name: Push images
30+
- name: Push image
3831
run: |
39-
docker push ${{ env.IMAGE }}-builder:latest
40-
docker push ${{ env.IMAGE }}-final:latest
32+
docker push ${{ env.IMAGE }}:latest
4133
4234
test:
4335
name: Test Docker Image
@@ -50,21 +42,14 @@ jobs:
5042
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
5143
env:
5244
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
53-
- name: Pull images
45+
- name: Pull image
5446
run: |
55-
docker pull ${{ env.IMAGE }}-builder:latest || true
56-
docker pull ${{ env.IMAGE }}-final:latest || true
57-
- name: Build images
47+
docker pull ${{ env.IMAGE }}:latest || true
48+
- name: Build image
5849
run: |
5950
docker build \
60-
--target builder \
61-
--cache-from ${{ env.IMAGE }}-builder:latest \
62-
--tag ${{ env.IMAGE }}-builder:latest \
63-
--file ./project/Dockerfile.prod \
64-
"./project"
65-
docker build \
66-
--cache-from ${{ env.IMAGE }}-final:latest \
67-
--tag ${{ env.IMAGE }}-final:latest \
51+
--cache-from ${{ env.IMAGE }}:latest \
52+
--tag ${{ env.IMAGE }}:latest \
6853
--file ./project/Dockerfile.prod \
6954
"./project"
7055
- name: Run container
@@ -76,9 +61,7 @@ jobs:
7661
-e ENVIRONMENT=dev \
7762
-e DATABASE_TEST_URL=sqlite://sqlite.db \
7863
-p 5003:8765 \
79-
${{ env.IMAGE }}-final:latest
80-
- name: Install requirements
81-
run: docker exec fastapi-tdd pip install black==20.8b1 flake8===3.8.4 isort==5.6.4 pytest==6.2.0
64+
${{ env.IMAGE }}:latest
8265
- name: Pytest
8366
run: docker exec fastapi-tdd python -m pytest .
8467
- name: Flake8
@@ -87,50 +70,3 @@ jobs:
8770
run: docker exec fastapi-tdd python -m black . --check
8871
- name: isort
8972
run: docker exec fastapi-tdd python -m isort . --check-only
90-
91-
deploy:
92-
name: Deploy to Heroku
93-
runs-on: ubuntu-latest
94-
needs: [build, test]
95-
env:
96-
HEROKU_APP_NAME: secret-crag-02876
97-
HEROKU_REGISTRY_IMAGE: registry.heroku.com/${HEROKU_APP_NAME}/summarizer
98-
steps:
99-
- name: Checkout master
100-
uses: actions/checkout@v2.3.4
101-
- name: Log in to GitHub Packages
102-
run: echo ${GITHUB_TOKEN} | docker login -u ${GITHUB_ACTOR} --password-stdin docker.pkg.github.com
103-
env:
104-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
105-
- name: Pull images
106-
run: |
107-
docker pull ${{ env.IMAGE }}-builder:latest || true
108-
docker pull ${{ env.IMAGE }}-final:latest || true
109-
- name: Build images
110-
run: |
111-
docker build \
112-
--target builder \
113-
--cache-from ${{ env.IMAGE }}-builder:latest \
114-
--tag ${{ env.IMAGE }}-builder:latest \
115-
--file ./project/Dockerfile.prod \
116-
"./project"
117-
docker build \
118-
--cache-from ${{ env.IMAGE }}-final:latest \
119-
--tag ${{ env.IMAGE }}:latest \
120-
--tag ${{ env.HEROKU_REGISTRY_IMAGE }}:latest \
121-
--file ./project/Dockerfile.prod \
122-
"./project"
123-
- name: Log in to the Heroku Container Registry
124-
run: docker login -u _ -p ${HEROKU_AUTH_TOKEN} registry.heroku.com
125-
env:
126-
HEROKU_AUTH_TOKEN: ${{ secrets.HEROKU_AUTH_TOKEN }}
127-
- name: Push to the registry
128-
run: docker push ${{ env.HEROKU_REGISTRY_IMAGE }}:latest
129-
- name: Set environment variables
130-
run: |
131-
echo "HEROKU_REGISTRY_IMAGE=${{ env.HEROKU_REGISTRY_IMAGE }}" >> $GITHUB_ENV
132-
echo "HEROKU_AUTH_TOKEN=${{ secrets.HEROKU_AUTH_TOKEN }}" >> $GITHUB_ENV
133-
- name: Release
134-
run: |
135-
chmod +x ./release.sh
136-
./release.sh

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.9.2-slim-buster
2+
FROM python:3.9.6-slim-buster
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-
51
# pull official base image
6-
FROM python:3.8.8-slim-buster as builder
7-
8-
# install system dependencies
9-
RUN apt-get update \
10-
&& apt-get -y install 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==20.8b1 flake8===3.8.4 isort==5.7.0
28-
RUN flake8 .
29-
RUN black --exclude=migrations .
30-
RUN isort .
31-
32-
33-
#########
34-
# FINAL #
35-
#########
36-
37-
# pull official base image
38-
FROM python:3.8.8-slim-buster
2+
FROM python:3.8.11-slim-buster
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.13.4"
29+
COPY ./requirements.txt .
30+
RUN pip install -r requirements.txt
31+
RUN pip install "uvicorn[standard]==0.14.0"
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/aerich.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[aerich]
2+
tortoise_orm = app.db.TORTOISE_ORM
3+
location = ./migrations

project/app/api/crud.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99

1010
async def post(payload: SummaryPayloadSchema) -> int:
11-
summary = TextSummary(url=payload.url, summary="")
11+
summary = TextSummary(
12+
url=payload.url,
13+
summary="dummy summary",
14+
)
1215
await summary.save()
1316
return summary.id
1417

@@ -23,18 +26,3 @@ async def get(id: int) -> Union[dict, None]:
2326
async def get_all() -> List:
2427
summaries = await TextSummary.all().values()
2528
return summaries
26-
27-
28-
async def delete(id: int) -> int:
29-
summary = await TextSummary.filter(id=id).first().delete()
30-
return summary
31-
32-
33-
async def put(id: int, payload: SummaryPayloadSchema) -> Union[dict, None]:
34-
summary = await TextSummary.filter(id=id).update(
35-
url=payload.url, summary=payload.summary
36-
)
37-
if summary:
38-
updated_summary = await TextSummary.filter(id=id).first().values()
39-
return updated_summary[0]
40-
return None

project/app/api/summaries.py

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,27 @@
1+
# project/app/api/summaries.py
2+
3+
14
from typing import List
25

3-
from fastapi import APIRouter, BackgroundTasks, HTTPException, Path
6+
from fastapi import APIRouter, HTTPException
47

58
from app.api import crud
9+
from app.models.pydantic import SummaryPayloadSchema, SummaryResponseSchema
610
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-
)
14-
1511

1612
router = APIRouter()
1713

1814

1915
@router.post("/", response_model=SummaryResponseSchema, status_code=201)
20-
async def create_summary(
21-
payload: SummaryPayloadSchema, background_tasks: BackgroundTasks
22-
) -> SummaryResponseSchema:
16+
async def create_summary(payload: SummaryPayloadSchema) -> SummaryResponseSchema:
2317
summary_id = await crud.post(payload)
2418

25-
background_tasks.add_task(generate_summary, summary_id, payload.url)
26-
2719
response_object = {"id": summary_id, "url": payload.url}
2820
return response_object
2921

3022

3123
@router.get("/{id}/", response_model=SummarySchema)
32-
async def read_summary(id: int = Path(..., gt=0)) -> SummarySchema:
24+
async def read_summary(id: int) -> SummarySchema:
3325
summary = await crud.get(id)
3426
if not summary:
3527
raise HTTPException(status_code=404, detail="Summary not found")
@@ -40,25 +32,3 @@ async def read_summary(id: int = Path(..., gt=0)) -> SummarySchema:
4032
@router.get("/", response_model=List[SummarySchema])
4133
async def read_all_summaries() -> List[SummarySchema]:
4234
return await crud.get_all()
43-
44-
45-
@router.delete("/{id}/", response_model=SummaryResponseSchema)
46-
async def delete_summary(id: int = Path(..., gt=0)) -> SummaryResponseSchema:
47-
summary = await crud.get(id)
48-
if not summary:
49-
raise HTTPException(status_code=404, detail="Summary not found")
50-
51-
await crud.delete(id)
52-
53-
return summary
54-
55-
56-
@router.put("/{id}/", response_model=SummarySchema)
57-
async def update_summary(
58-
payload: SummaryUpdatePayloadSchema, id: int = Path(..., gt=0)
59-
) -> SummarySchema:
60-
summary = await crud.put(id, payload)
61-
if not summary:
62-
raise HTTPException(status_code=404, detail="Summary not found")
63-
64-
return summary

project/app/db.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
log = logging.getLogger("uvicorn")
1212

1313

14+
TORTOISE_ORM = {
15+
"connections": {"default": os.environ.get("DATABASE_URL")},
16+
"apps": {
17+
"models": {
18+
"models": ["app.models.tortoise", "aerich.models"],
19+
"default_connection": "default",
20+
},
21+
},
22+
}
23+
24+
1425
def init_db(app: FastAPI) -> None:
1526
register_tortoise(
1627
app,

project/app/models/pydantic.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
# project/app/models/pydantic.py
22

33

4-
from pydantic import AnyHttpUrl, BaseModel
4+
from pydantic import BaseModel
55

66

77
class SummaryPayloadSchema(BaseModel):
8-
url: AnyHttpUrl
8+
url: str
99

1010

1111
class SummaryResponseSchema(SummaryPayloadSchema):
1212
id: int
13-
14-
15-
class SummaryUpdatePayloadSchema(SummaryPayloadSchema):
16-
summary: str

project/app/summarizer.py

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- upgrade --
2+
CREATE TABLE IF NOT EXISTS "textsummary" (
3+
"id" SERIAL NOT NULL PRIMARY KEY,
4+
"url" TEXT NOT NULL,
5+
"summary" TEXT NOT NULL,
6+
"created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
7+
);
8+
CREATE TABLE IF NOT EXISTS "aerich" (
9+
"id" SERIAL NOT NULL PRIMARY KEY,
10+
"version" VARCHAR(255) NOT NULL,
11+
"app" VARCHAR(20) NOT NULL,
12+
"content" JSONB NOT NULL
13+
);

0 commit comments

Comments
 (0)