diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a6ca2fe..fca778f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,14 +16,14 @@ jobs: - isort --check app - flake8 app - mypy app - test: - runs-on: ubuntu-latest - name: Test - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 - with: - python-version: '3.11' - cache: 'pip' - - pip install -e .[dev] - - pytest + # test: + # runs-on: ubuntu-latest + # name: Test + # steps: + # - uses: actions/checkout@v3 + # - uses: actions/setup-python@v4 + # with: + # python-version: '3.11' + # cache: 'pip' + # - pip install -e .[test] + # - pytest diff --git a/Makefile b/Makefile index 32ca95e..04ae1b0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ dev: - docker-compose -f dev.docker-compose.yml up --build --remove-orphans + docker-compose -f docker/dev.docker-compose.yml build --progress tty + docker-compose -f docker/dev.docker-compose.yml up --remove-orphans fmt: black app diff --git a/alembic.ini b/alembic.ini index 2874da8..5194681 100644 --- a/alembic.ini +++ b/alembic.ini @@ -2,7 +2,7 @@ [alembic] # path to migration scripts -script_location = app/alembic +script_location = app/shared/db/alembic # template used to generate migration file names; The default value is %%(rev)s_%%(slug)s # Uncomment the line below if you want the files to be prepended with date and time diff --git a/app/db/__init__.py b/app/shared/__init__.py similarity index 100% rename from app/db/__init__.py rename to app/shared/__init__.py diff --git a/app/config.py b/app/shared/config.py similarity index 100% rename from app/config.py rename to app/shared/config.py diff --git a/app/utils/__init__.py b/app/shared/db/__init__.py similarity index 100% rename from app/utils/__init__.py rename to app/shared/db/__init__.py diff --git a/app/alembic/README b/app/shared/db/alembic/README similarity index 100% rename from app/alembic/README rename to app/shared/db/alembic/README diff --git a/app/alembic/env.py b/app/shared/db/alembic/env.py similarity index 96% rename from app/alembic/env.py rename to app/shared/db/alembic/env.py index 59c21c4..e3c8182 100644 --- a/app/alembic/env.py +++ b/app/shared/db/alembic/env.py @@ -3,8 +3,8 @@ from logging.config import fileConfig from alembic import context from sqlalchemy import engine_from_config, pool -from app.config import settings -from app.db.models import Base +from app.shared.config import settings +from app.shared.db.models import Base # this is the Alembic Config object, which provides # access to the values within the .ini file in use. diff --git a/app/alembic/script.py.mako b/app/shared/db/alembic/script.py.mako similarity index 100% rename from app/alembic/script.py.mako rename to app/shared/db/alembic/script.py.mako diff --git a/app/alembic/versions/c43a1ddae8b7_add_job_and_artifact_tables.py b/app/shared/db/alembic/versions/c43a1ddae8b7_add_job_and_artifact_tables.py similarity index 100% rename from app/alembic/versions/c43a1ddae8b7_add_job_and_artifact_tables.py rename to app/shared/db/alembic/versions/c43a1ddae8b7_add_job_and_artifact_tables.py diff --git a/app/db/base.py b/app/shared/db/base.py similarity index 92% rename from app/db/base.py rename to app/shared/db/base.py index 7c62f24..3b64fb2 100644 --- a/app/db/base.py +++ b/app/shared/db/base.py @@ -3,7 +3,7 @@ from typing import Generator from sqlalchemy import create_engine from sqlalchemy.orm import Session, sessionmaker -from app.config import settings +from app.shared.config import settings engine = create_engine(settings.DATABASE_URI) diff --git a/app/db/dtos.py b/app/shared/db/dtos.py similarity index 100% rename from app/db/dtos.py rename to app/shared/db/dtos.py diff --git a/app/db/models.py b/app/shared/db/models.py similarity index 96% rename from app/db/models.py rename to app/shared/db/models.py index da1472e..82f3214 100644 --- a/app/db/models.py +++ b/app/shared/db/models.py @@ -6,7 +6,7 @@ from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import Mapped, declarative_mixin, declared_attr -from app.db.dtos import ArtifactType, JobStatus, JobType +from .dtos import ArtifactType, JobStatus, JobType Base = declarative_base() diff --git a/app/tests/conftest.py b/app/tests/conftest.py index d2500a9..feb6762 100644 --- a/app/tests/conftest.py +++ b/app/tests/conftest.py @@ -4,9 +4,9 @@ import pytest from sqlalchemy.orm import Session from sqlalchemy_utils import create_database, database_exists, drop_database -from app.db.base import SessionLocal, engine, get_session -from app.db.models import Base -from app.main import app +from app.shared.db.base import SessionLocal, engine, get_session +from app.shared.db.models import Base +from app.web.main import app def pytest_configure() -> None: diff --git a/app/tests/test_auth.py b/app/tests/test_auth.py index 190a9b1..26caed7 100644 --- a/app/tests/test_auth.py +++ b/app/tests/test_auth.py @@ -2,8 +2,8 @@ from typing import Dict from fastapi.testclient import TestClient -from app.config import settings -from app.main import app +from app.shared.config import settings +from app.web.main import app client = TestClient(app) diff --git a/app/web/__init__.py b/app/web/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/main.py b/app/web/main.py similarity index 91% rename from app/main.py rename to app/web/main.py index c2e99af..8fe6b7c 100644 --- a/app/main.py +++ b/app/web/main.py @@ -5,10 +5,10 @@ from fastapi import APIRouter, Depends, FastAPI, HTTPException, Path from pydantic import AnyHttpUrl, BaseModel from sqlalchemy.orm import Session -import app.db.dtos as dtos -import app.db.models as models -from app.db.base import get_session -from app.utils.security import authenticate_api_key +import app.shared.db.dtos as dtos +import app.shared.db.models as models +from app.shared.db.base import get_session +from app.web.security import authenticate_api_key app = FastAPI() diff --git a/app/utils/security.py b/app/web/security.py similarity index 91% rename from app/utils/security.py rename to app/web/security.py index d45ae3d..636d67d 100644 --- a/app/utils/security.py +++ b/app/web/security.py @@ -3,7 +3,7 @@ from hmac import compare_digest from fastapi import Depends, HTTPException from fastapi.security import OAuth2PasswordBearer -from app.config import settings +from app.shared.config import settings def authenticate_api_key( diff --git a/app/start.sh b/app/web/start.sh similarity index 51% rename from app/start.sh rename to app/web/start.sh index 99ebd80..46b7684 100755 --- a/app/start.sh +++ b/app/web/start.sh @@ -6,4 +6,4 @@ set -e alembic upgrade head # start app -uvicorn app.main:app --reload --host ${HOST:-0.0.0.0} --port ${PORT:-80} +uvicorn app.web.main:app --reload --host ${HOST:-0.0.0.0} --port ${PORT:-80} diff --git a/app/worker/__init__.py b/app/worker/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/worker.py b/app/worker/main.py similarity index 71% rename from app/worker.py rename to app/worker/main.py index 7827237..e8b1a93 100644 --- a/app/worker.py +++ b/app/worker/main.py @@ -1,6 +1,6 @@ from celery import Celery -from .config import settings +from app.shared.config import settings celery = Celery(__name__) diff --git a/dev.Dockerfile b/dev.Dockerfile deleted file mode 100644 index 0fb117c..0000000 --- a/dev.Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM python:3.11 - -WORKDIR /code - -ENV PYTHONDONTWRITEBYTECODE 1 -ENV PYTHONUNBUFFERED 1 - -# Install dependencies -COPY pyproject.toml . -RUN pip install -U pip -RUN pip install .[test] - -# The source code is mounted as a volume at /code, no need to copy. diff --git a/docker/app.dev.Dockerfile b/docker/app.dev.Dockerfile new file mode 100644 index 0000000..47081d0 --- /dev/null +++ b/docker/app.dev.Dockerfile @@ -0,0 +1,16 @@ +FROM python:3.11 AS compile-image + +COPY pyproject.toml . +RUN pip install --user .[test] + +FROM python:3.11 AS build-image + +WORKDIR /code + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +COPY --from=compile-image /root/.local /root/.local +ENV PATH=/root/.local/bin:$PATH + +ENTRYPOINT ["bash", "./app/web/start.sh"] diff --git a/dev.docker-compose.yml b/docker/dev.docker-compose.yml similarity index 86% rename from dev.docker-compose.yml rename to docker/dev.docker-compose.yml index 590e2c2..26e7cdc 100644 --- a/dev.docker-compose.yml +++ b/docker/dev.docker-compose.yml @@ -37,16 +37,15 @@ services: app: container_name: whisper_api_app build: - context: . - dockerfile: dev.Dockerfile - command: bash ./app/start.sh + context: ../ + dockerfile: docker/app.dev.Dockerfile environment: *app-variables ports: - "8000:80" networks: - app volumes: - - ./:/code + - ../:/code depends_on: postgres: condition: service_healthy @@ -55,12 +54,11 @@ services: worker: build: - context: . - dockerfile: dev.Dockerfile + context: ../ + dockerfile: docker/worker.dev.Dockerfile container_name: whisper_api_worker - command: celery --app=app.worker.celery worker --loglevel=info volumes: - - ./:/code + - ../:/code environment: *app-variables depends_on: - app diff --git a/docker/worker.dev.Dockerfile b/docker/worker.dev.Dockerfile new file mode 100644 index 0000000..129e2f9 --- /dev/null +++ b/docker/worker.dev.Dockerfile @@ -0,0 +1,18 @@ +FROM python:3.10 AS compile-image + +COPY pyproject.toml . +RUN pip install --user .[test,worker] + +RUN apt-get update && apt-get clean && apt-get install -y --no-install-recommends ffmpeg + +FROM python:3.10 AS build-image + +WORKDIR /code + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +COPY --from=compile-image /root/.local /root/.local +ENV PATH=/root/.local/bin:$PATH + +ENTRYPOINT ["celery", "--app=app.worker.main.celery", "worker", "--loglevel=info"] diff --git a/pyproject.toml b/pyproject.toml index 0cf12a8..dc910b5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,11 @@ dependencies=[ ] [project.optional-dependencies] + +worker=[ + "whisper @ git+https://github.com/openai/whisper.git" +] + dev = [ # code formatting "black",