Skip to content

Commit

Permalink
2024 refactor part 2/3: fastapi refactor (#800)
Browse files Browse the repository at this point in the history
* Create structure for fastapi based API

* Refactor aggregation and measurement API to fastapi

* Refactor aggregation and measurement API to fastapi

* Add github workflow

* Delete all unused imports

* Remove dead code

* Move unused variables into integ tests

* Rename dataapi to oonidataapi

* Move aggregation integration test into oonidataapi dir

* Integrate aggregation integration tests

* Add missing deps

* Fix pkg_name

* Port aggregation tests over to httpx

* Run build on new branch name

* Re-add parts lost in rebase

* More missing pieces

Thankyou linting
  • Loading branch information
hellais authored Feb 4, 2024
1 parent b493252 commit a3c2ffd
Show file tree
Hide file tree
Showing 29 changed files with 6,503 additions and 1,592 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/build_dataapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build and Deploy Docker Image for dataapi

on:
push:
branches:
- backend-v2024.2.0
#paths:
# - "api/fastapi/**"

env:
IMAGE_NAME: ooni/dataapi
DOCKERFILE_PATH: ./api/fastapi/

jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Format version number
id: version
run: |
DATE=$(date +'%Y%m%d')
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-4)
TAG="v${DATE}-${SHORT_SHA}"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
- name: Build and Push Docker Image
run: |
BUILD_LABEL=${{ steps.version.outputs.tag }}
TAG_LATEST=$IMAGE_NAME:latest
TAG_ENVIRONMENT=$IMAGE_NAME:production
TAG_VERSION=$IMAGE_NAME:$BUILD_LABEL
# Build Docker image with multiple tags
docker build --build-arg BUILD_LABEL=$BUILD_LABEL -t $TAG_LATEST -t $TAG_VERSION -t $TAG_ENVIRONMENT $DOCKERFILE_PATH
# Push all tags
docker push $TAG_LATEST
docker push $TAG_VERSION
docker push $TAG_ENVIRONMENT
30 changes: 30 additions & 0 deletions api/fastapi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Python builder
# Produced a virtualenv with all the deps good to go

# TODO(art): upgrade to bookworm (blocked by this
# https://github.com/actions/setup-python/issues/721) or stop using debian to
# have more recent base OS
FROM python:3.11-bullseye as py-build
ARG BUILD_LABEL=docker

RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends python3 python3-dev

RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python3 -

COPY . /app
WORKDIR /app
ENV PATH=/opt/poetry/bin:$PATH
RUN echo "$BUILD_LABEL" > /app/dataapi/BUILD_LABEL
RUN poetry config virtualenvs.in-project true && poetry install --no-interaction --no-ansi

### Actual image running on the host
FROM python:3.11-bullseye

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY --from=py-build /app /app
WORKDIR /app
CMD ["/app/.venv/bin/uvicorn", "dataapi.main:app", "--host", "0.0.0.0", "--port", "80"]
EXPOSE 80
6 changes: 6 additions & 0 deletions api/fastapi/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Running:

```
poetry install
poetry run uvicorn dataapi.main:app
```
Loading

0 comments on commit a3c2ffd

Please sign in to comment.