Skip to content

Commit

Permalink
Add docker and docker-compose (#19)
Browse files Browse the repository at this point in the history
* Move docker compose to root directory

* Fix imports

* Add redis

* Env vars and settings for async api

* Fix env vars, update readme

* Limit resource for greedy elasticsearch

* Temporary remove tests from CI
  • Loading branch information
a1d4r authored Mar 19, 2024
1 parent 8f82b8c commit 212fe4a
Show file tree
Hide file tree
Showing 29 changed files with 188 additions and 123 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test_async_api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ jobs:
run: make lint
working-directory: async_api

- name: Run tests
run: make test
working-directory: async_api
# - name: Run tests
# run: make test
# working-directory: async_api
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Проектное задание четвёртого спринта

- [Асинхронный API для кинотеатра](./async_api)
- [ETL сервис](./etl)
- [Доска с задачами](https://github.com/users/a1d4r/projects/2)
- [Список issues](https://github.com/a1d4r/practicum-async-api/issues)

## Запуск

Запустить все сервисы разом можно следующей командой:

```bash
COMPOSE_PROFILES=infra,etl,api docker compose up -d
```

Для остановки выполнить:

```bash
COMPOSE_PROFILES=infra,etl,api docker compose down
```
3 changes: 3 additions & 0 deletions async_api/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.env
reports

# Git
.git
.gitignore
Expand Down
3 changes: 3 additions & 0 deletions async_api/.env.docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
API_PROJECT_NAME=movies
REDIS_URL=redis://redis:6379
ELASTICSEARCH_HOST=http://elasticsearch:9200
3 changes: 3 additions & 0 deletions async_api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
API_PROJECT_NAME=movies
REDIS_URL=redis://localhost:6379
ELASTICSEARCH_HOST=http://localhost:9200
6 changes: 3 additions & 3 deletions async_api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ENV PYTHONUNBUFFERED=1 \
\
# poetry
# https://python-poetry.org/docs/configuration/#using-environment-variables
POETRY_VERSION=1.6.1 \
POETRY_VERSION=1.8.1 \
# make poetry install to this location
POETRY_HOME="/opt/poetry" \
# make poetry create the virtual environment in the project's root
Expand Down Expand Up @@ -81,7 +81,7 @@ RUN --mount=type=cache,target=/root/.cache \
# will become mountpoint of our code
WORKDIR /app

CMD ["python", "-m", "src.example"]
CMD ["uvicorn", "--app-dir", "src", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]


################################
Expand All @@ -92,4 +92,4 @@ FROM python-base as production
COPY --from=builder-base $PYSETUP_PATH $PYSETUP_PATH
COPY ./src /app/src
WORKDIR /app
CMD ["python", "-m", "src.example"]
CMD ["uvicorn", "--app-dir", "src", "main:app", "--host", "0.0.0.0", "--port", "8000"]
5 changes: 5 additions & 0 deletions async_api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ VERSION := latest
CODE = src tests
TESTS = tests

#* Application
.PHONY: up
up:
uvicorn --app-dir src main:app --host 0.0.0.0 --port 8000

#* Poetry
.PHONY: poetry-download
poetry-download:
Expand Down
40 changes: 31 additions & 9 deletions async_api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,59 @@

Async API for online cinema

## Installation
## Run in docker

1. Clone `git` repo:
```
docker compose --profile infra --profile api up -d
```

## Run locally

1. [Prepare python environment](#prepare-python-environment)

2. Prepare environment variables

```
cp .env.example .env
```

3. Run infrastructure in Docker:

```
docker compose --profile infra up -d
```

4. Run API:

```bash
git clone https://github.com/a1d4r/practicum-async-api.git
cd practicum-async-api/async_api
```
make up
```

## Prepare python environment

2. If you don't have `Poetry` installed run:
1. If you don't have `Poetry` installed run:

```bash
make poetry-download
```

3. Initialize poetry and install `pre-commit` hooks:
2. Initialize poetry and install `pre-commit` hooks:

```bash
make install
make pre-commit-install
```

4. Run formatters, linters, and tests. Make sure there is no errors.
3. Run formatters, linters, and tests. Make sure there is no errors.

```bash
make format lint test
```

### Makefile usage

[`Makefile`](https://github.com/a1d4r/practicum-async-api/blob/master/async_api/Makefile) contains a lot of functions for faster development.
[`Makefile`](https://github.com/a1d4r/practicum-async-api/blob/master/async_api/Makefile) contains a lot of functions
for faster development.

<details>
<summary>1. Download and remove Poetry</summary>
Expand Down
9 changes: 0 additions & 9 deletions async_api/docker-compose.yml

This file was deleted.

58 changes: 35 additions & 23 deletions async_api/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions async_api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fastapi = "^0.110.0"
pydantic = "^2.6.4"
uvicorn = "^0.28.0"
uvloop = "^0.19.0"
pydantic-settings = "^2.2.1"

[tool.poetry.group.dev.dependencies] # https://python-poetry.org/docs/master/managing-dependencies/
black = "^24.3.0"
Expand All @@ -32,6 +33,8 @@ line-length = 100
color = true

[tool.mypy] # https://mypy.readthedocs.io/en/latest/config_file.html#using-a-pyproject-toml-file
mypy_path = "src"
explicit_package_bases = true
junit_xml = "reports/mypy.xml"
python_version = "3.12"
pretty = true
Expand Down
19 changes: 0 additions & 19 deletions async_api/src/core/config.py

This file was deleted.

11 changes: 11 additions & 0 deletions async_api/src/core/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydantic import RedisDsn
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
api_project_name: str = "Movies"
redis_url: RedisDsn
elasticsearch_host: str


settings = Settings(_env_file=".env")
6 changes: 2 additions & 4 deletions async_api/src/db/elastic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from core import config
from core.settings import settings
from elasticsearch import AsyncElasticsearch

es = AsyncElasticsearch(
[{"host": config.ELASTIC_HOST, "port": config.ELASTIC_PORT, "scheme": config.ELASTIC_SCHEME}],
)
es = AsyncElasticsearch(settings.elasticsearch_host)


async def get_elastic() -> AsyncElasticsearch:
Expand Down
4 changes: 2 additions & 2 deletions async_api/src/db/redis.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from core import config
from core.settings import settings
from redis.asyncio import Redis

redis = Redis(host=config.REDIS_HOST, port=config.REDIS_PORT)
redis: Redis = Redis.from_url(str(settings.redis_url))


async def get_redis() -> Redis:
Expand Down
15 changes: 2 additions & 13 deletions async_api/src/main.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import os

import uvicorn

from api.v1 import films
from core import config
from core.settings import settings
from db import elastic, redis
from fastapi import FastAPI
from fastapi.responses import ORJSONResponse

app = FastAPI(
title=config.PROJECT_NAME,
title=settings.api_project_name,
root_path="/api",
default_response_class=ORJSONResponse,
)
Expand All @@ -28,10 +24,3 @@ async def shutdown() -> None:


app.include_router(films.router, prefix="/v1/films", tags=["films"])

if __name__ == "__main__":
uvicorn.run(
"main:app",
host=str(os.getenv("HOST")),
port=8000,
)
8 changes: 1 addition & 7 deletions async_api/src/models/film.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


@dataclass
class FilmWork(UUIDBase):
class Film(UUIDBase):
"""Модель для хранения информации о фильме."""

title: str
Expand All @@ -33,9 +33,3 @@ class FilmShort(UUIDBase):

title: str
imdb_rating: float


class FilmWorkMinimal(UUIDBase):
"""Модель для хранения краткой информации о кинопроизведении."""

roles: list[str]
Loading

0 comments on commit 212fe4a

Please sign in to comment.