-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from kiwix/dev
Improve basic repo architecture
- Loading branch information
Showing
37 changed files
with
405 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Publish backend Docker image | ||
|
||
on: | ||
push: | ||
paths: | ||
- 'backend/**' | ||
branches: | ||
- main | ||
|
||
jobs: | ||
|
||
publish-backend: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: Retrieve source code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Build and publish Docker Image | ||
uses: openzim/docker-publish-action@v10 | ||
with: | ||
image-name: openzim/mirrors-qa-backend | ||
tag-pattern: /^v([0-9.]+)$/ | ||
latest-on-tag: true | ||
restrict-to: openzim/mirrors-qa | ||
context: backend | ||
registries: ghcr.io | ||
credentials: | ||
GHCRIO_USERNAME=${{ secrets.GHCR_USERNAME }} | ||
GHCRIO_TOKEN=${{ secrets.GHCR_TOKEN }} | ||
repo_description: auto | ||
repo_overview: auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Publish worker-manager Docker image | ||
|
||
on: | ||
push: | ||
paths: | ||
- 'worker/**' | ||
branches: | ||
- main | ||
|
||
jobs: | ||
|
||
publish-worker-manager: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: Retrieve source code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Build and publish Docker Image | ||
uses: openzim/docker-publish-action@v10 | ||
with: | ||
image-name: openzim/mirrors-qa-worker-manager | ||
latest-on-tag: true | ||
tag-pattern: /^v([0-9.]+)$/ | ||
restrict-to: openzim/mirrors-qa | ||
context: worker | ||
dockerfile: manager.Dockerfile | ||
registries: ghcr.io | ||
credentials: | ||
GHCRIO_USERNAME=${{ secrets.GHCR_USERNAME }} | ||
GHCRIO_TOKEN=${{ secrets.GHCR_TOKEN }} | ||
repo_description: auto | ||
repo_overview: auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM python:3.11-slim | ||
LABEL org.opencontainers.image.source=https://github.com/kiwix/mirrors-qa | ||
# Copy code | ||
COPY src /src/src | ||
# Copy pyproject.toml and its dependencies | ||
COPY pyproject.toml README.md /src/ | ||
|
||
# Install + cleanup | ||
RUN pip install --no-cache-dir /src \ | ||
&& rm -rf /src | ||
|
||
EXPOSE 80 | ||
|
||
CMD ["uvicorn", "mirrors_qa_backend.main:app", "--host", "0.0.0.0", "--port", "80"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import logging | ||
import os | ||
|
||
logger = logging.getLogger("backend") | ||
|
||
if not logger.hasHandlers(): | ||
logger.setLevel(logging.DEBUG if bool(os.getenv("DEBUG")) else logging.INFO) | ||
handler = logging.StreamHandler() | ||
handler.setFormatter(logging.Formatter("[%(asctime)s: %(levelname)s] %(message)s")) | ||
logger.addHandler(handler) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import subprocess | ||
from collections.abc import Generator | ||
from pathlib import Path | ||
|
||
from sqlalchemy import SelectBase, create_engine, func, select | ||
from sqlalchemy.orm import Session as OrmSession | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from mirrors_qa_backend import logger | ||
from mirrors_qa_backend.db.models import Mirror | ||
from mirrors_qa_backend.settings import Settings | ||
|
||
Session = sessionmaker( | ||
bind=create_engine(url=Settings.database_url, echo=False), | ||
expire_on_commit=False, | ||
) | ||
|
||
|
||
def gen_dbsession() -> Generator[OrmSession, None, None]: | ||
"""FastAPI's Depends() compatible helper to provide a begin DB Session""" | ||
with Session.begin() as session: | ||
yield session | ||
|
||
|
||
def upgrade_db_schema(): | ||
"""Checks if Alembic schema has been applied to the DB""" | ||
src_dir = Path(__file__).parent.parent | ||
logger.info(f"Upgrading database schema with config in {src_dir}") | ||
subprocess.check_output(args=["alembic", "upgrade", "head"], cwd=src_dir) | ||
|
||
|
||
def count_from_stmt(session: OrmSession, stmt: SelectBase) -> int: | ||
"""Count all records returned by any statement `stmt` passed as parameter""" | ||
return session.execute( | ||
select(func.count()).select_from(stmt.subquery()) | ||
).scalar_one() | ||
|
||
|
||
def initialize_mirrors() -> None: | ||
with Session.begin() as session: | ||
count = count_from_stmt(session, select(Mirror)) | ||
if count == 0: | ||
logger.info("No mirrors exist in database.") | ||
# TODO: update mirrors from https://download.kiwix.org/mirrors.html | ||
else: | ||
logger.info(f"Found {count} mirrors in database.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from contextlib import asynccontextmanager | ||
|
||
from fastapi import FastAPI | ||
|
||
from mirrors_qa_backend import db | ||
from mirrors_qa_backend.routes import auth, tests | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(_: FastAPI): | ||
db.upgrade_db_schema() | ||
db.initialize_mirrors() | ||
yield | ||
|
||
|
||
def create_app(*, debug: bool = True): | ||
app = FastAPI(debug=debug, docs_url="/", lifespan=lifespan) | ||
app.include_router(router=tests.router) | ||
app.include_router(router=auth.router) | ||
return app | ||
|
||
|
||
app = create_app() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends | ||
from sqlalchemy.orm import Session | ||
|
||
from mirrors_qa_backend.db import gen_dbsession | ||
|
||
DbSession = Annotated[Session, Depends(gen_dbsession)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from fastapi import APIRouter, Response, status | ||
from fastapi.responses import JSONResponse | ||
|
||
router = APIRouter(prefix="/auth", tags=["auth"]) | ||
|
||
|
||
@router.post("/authenticate") | ||
def authenticate_user() -> Response: | ||
return JSONResponse(content={"token": "token"}, status_code=status.HTTP_200_OK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from fastapi import APIRouter, Response, status | ||
from fastapi.responses import JSONResponse | ||
|
||
router = APIRouter(prefix="/tests", tags=["tests"]) | ||
|
||
|
||
@router.get( | ||
"", | ||
status_code=status.HTTP_200_OK, | ||
responses={ | ||
status.HTTP_200_OK: {"description": "Returns the list of tests."}, | ||
}, | ||
) | ||
def list_tests() -> Response: | ||
return JSONResponse( | ||
content={ | ||
"tests": [], | ||
"metadata": { | ||
"currentPage": None, | ||
"pageSize": 10, | ||
"firstPage": None, | ||
"lastPage": None, | ||
"nextPage": None, | ||
"totalRecords": 20, | ||
}, | ||
}, | ||
status_code=status.HTTP_200_OK, | ||
) | ||
|
||
|
||
@router.get( | ||
"/{test_id}", | ||
status_code=status.HTTP_200_OK, | ||
responses={ | ||
status.HTTP_200_OK: {"description": "Returns the details of a test."}, | ||
status.HTTP_404_NOT_FOUND: {"description": "Test with id does not exist."}, | ||
}, | ||
) | ||
def get_test(test_id: str) -> Response: | ||
return JSONResponse( | ||
content={"id": test_id}, | ||
status_code=status.HTTP_200_OK, | ||
) | ||
|
||
|
||
@router.patch( | ||
"/{test_id}", | ||
status_code=status.HTTP_200_OK, | ||
responses={ | ||
status.HTTP_200_OK: {"description": "Update the details of a test."}, | ||
}, | ||
) | ||
def update_test(test_id: str) -> Response: | ||
return JSONResponse( | ||
content={"id": test_id}, | ||
status_code=status.HTTP_200_OK, | ||
) |
Oops, something went wrong.