Skip to content

Commit 876aff2

Browse files
author
federicoVallcorbaX
committed
WIP
1 parent 6b56927 commit 876aff2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+786
-436
lines changed

.dockerignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# repo and project folders
2+
.git
3+
.gitignore
4+
.gitmodules
5+
.idea
6+
.vscode
7+
8+
# docker
9+
Dockerfile
10+
.dockerignore
11+
docker-compose.yaml
12+
13+
# python related
14+
*.pyc
15+
*.pyo
16+
*.pyd

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres
2+
ACCESS_TOKEN_EXPIRE_MINUTES=15
3+
JWT_SIGNING_KEY=

.flake8

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[flake8]
2+
max-line-length = 130
3+
per-file-ignores =
4+
__init__.py: F401
5+
exclude =
6+
.git,__pycache__,src/alembic/versions

.gitignore

100644100755
+24-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
.idea
2-
.ipynb_checkpoints
3-
.mypy_cache
4-
.vscode
5-
__pycache__
6-
.pytest_cache
7-
.coverage
8-
coverage.xml
9-
test.db
10-
log.txt
11-
src/.env
12-
venv
13-
.DS_Store
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
5+
# C extensions
6+
*.so
7+
*.http
8+
9+
# Distribution / packaging
10+
bin/
11+
build/
12+
develop-eggs/
13+
dist/
14+
eggs/
15+
lib/
16+
lib64/
17+
parts/
18+
sdist/
19+
var/
20+
*.egg-info/
21+
.installed.cfg
22+
*.egg
23+
24+
.env

.isort.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[settings]
2+
profile=black
3+
skip=alembic

Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM python:3.11
2+
3+
RUN apt-get update && apt-get install -y postgresql
4+
5+
WORKDIR /backend
6+
7+
COPY requirements.txt .
8+
9+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
10+
11+
COPY . .
12+
13+
ENV PYTHONPATH=/backend
14+
15+
CMD ["uvicorn", "src.main:app", "--reload", "--host", "0.0.0.0", "--port", "8000"]

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Recsys Backend
2+
3+
## Components
4+
- Rest API built with FastAPI and SQLAlchemy
5+
- PostgreSQL database
6+
7+
## Project setup
8+
You only need to install [Docker](https://docs.docker.com/engine/install/) and [Docker Compose](https://docs.docker.com/compose/install/).
9+
To start the containers, just run `docker-compose up` (or `docker-compose up -d` if you want to run the containers in background); or `docker-compose create` and `docker-compose start` if you don't want to see the logs.
10+
When the containers are running, you can go to `http://localhost:8000/docs` to see the automatic interactive API documentation.
11+
12+
## Migrations
13+
We use Alembic as database migration tool. To run its commands you can open an interactive shell inside the backend container, or use the following shortcuts under the `/scripts` directory:
14+
- `./exec.sh migrate` -> runs all the migrations
15+
- `./exec.sh makemigrations` -> compares the actual status of the DB against the table metadata, and generates the migrations based on the comparison
16+
17+
## Database initial load
18+
1. Download the `movie_features.parquet` file from [this link](https://drive.google.com/file/d/1hM_j-UL8UGRZyNZntZG0tRXLJOluFpqc/view?usp=sharing)
19+
2. Store the file on the following directory: `ml/data/preprocessed`
20+
3. Initiate the backend database service
21+
4. Run database migrations
22+
5. Run the initial load: `./exec.sh load` in the `/scripts` directory

docker-compose.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: '3.8'
2+
3+
services:
4+
backend:
5+
build: .
6+
ports:
7+
- '8000:8000'
8+
volumes:
9+
- .:/backend
10+
env_file: .env
11+
stdin_open: true
12+
tty: true
13+
depends_on:
14+
- db
15+
db:
16+
image: postgres:15.3
17+
ports:
18+
- 127.0.0.1:5432:5432
19+
volumes:
20+
- postgres-data:/var/lib/postgresql/data/
21+
environment:
22+
POSTGRES_USER: postgres
23+
POSTGRES_PASSWORD: postgres
24+
POSTGRES_DB: postgres
25+
26+
volumes:
27+
postgres-data:

docker-compose.yml

-26
This file was deleted.

mypy.ini

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[mypy]
2+
plugins = pydantic.mypy, sqlalchemy.ext.mypy.plugin
3+
disallow_untyped_defs = True
4+
exclude = src/alembic/
5+
ignore_missing_imports = True

requirements.txt

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
alembic==1.11.0
2+
anyio==3.7.1
3+
bcrypt==4.0.1
4+
black==23.3.0
5+
certifi==2023.5.7
6+
click==8.1.3
7+
dnspython==2.3.0
8+
ecdsa==0.18.0
9+
email-validator==2.0.0.post2
10+
fastapi==0.95.2
11+
fastapi-pagination==0.12.4
12+
flake8==6.0.0
13+
greenlet==2.0.2
14+
h11==0.14.0
15+
httpcore==0.17.3
16+
httpx==0.24.1
17+
idna==3.4
18+
isort==5.12.0
19+
itsdangerous==2.1.2
20+
Jinja2==3.1.2
21+
libcst==0.4.10
22+
Mako==1.2.4
23+
MarkupSafe==2.1.3
24+
mccabe==0.7.0
25+
mypy==1.3.0
26+
mypy-extensions==1.0.0
27+
packaging==23.1
28+
passlib==1.7.4
29+
pathspec==0.10.3
30+
platformdirs==3.8.0
31+
psycopg2==2.9.6
32+
pyasn1==0.5.0
33+
pycln==2.1.3
34+
pycodestyle==2.10.0
35+
pydantic==1.10.7
36+
pyflakes==3.0.1
37+
python-jose==3.3.0
38+
python-multipart==0.0.6
39+
PyYAML==6.0
40+
rsa==4.9
41+
six==1.16.0
42+
sniffio==1.3.0
43+
sqladmin==0.10.3
44+
SQLAlchemy==2.0.13
45+
starlette==0.27.0
46+
tomlkit==0.11.8
47+
typer==0.7.0
48+
types-passlib==1.7.7.12
49+
types-pyasn1==0.4.0.5
50+
types-python-jose==3.3.4.7
51+
typing-inspect==0.9.0
52+
typing_extensions==4.7.1
53+
uvicorn==0.22.0
54+
WTForms==3.0.1

scripts/exec.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
DOCKER_COMPOSE_FILE_PATH="${PWD%/*}/docker-compose.yaml"
4+
case "$1" in
5+
format)
6+
docker-compose -f $DOCKER_COMPOSE_FILE_PATH run -T backend bash < format.sh
7+
;;
8+
makemigrations)
9+
docker-compose -f $DOCKER_COMPOSE_FILE_PATH run -T backend bash < makemigrations.sh
10+
;;
11+
migrate)
12+
docker-compose -f $DOCKER_COMPOSE_FILE_PATH run -T backend bash < migrate.sh
13+
;;
14+
shell)
15+
docker-compose -f $DOCKER_COMPOSE_FILE_PATH run backend bash
16+
;;
17+
esac

scripts/format.sh

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
printf "Runing pycln...\n"
4+
pycln src --exclude __init__.py --all
5+
printf "\nRunning isort...\n"
6+
isort src
7+
printf "\nRunning flake8...\n"
8+
flake8
9+
10+
printf "\nRunning mypy...\n"
11+
mypy src
12+
13+
printf "\nRunning black...\n"
14+
black src --exclude alembic

scripts/makemigrations.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
cd src
4+
alembic revision --autogenerate

scripts/migrate.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
cd src
4+
alembic upgrade head

scripts/test.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TODO

src/.env.example

-7
This file was deleted.

src/Dockerfile

-24
This file was deleted.
File renamed without changes.

src/alembic.ini

100644100755
+33-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,40 @@
22

33
[alembic]
44
# path to migration scripts
5-
script_location = migrations
6-
prepend_sys_path = .
7-
sqlalchemy.url = driver://user:pass@localhost/dbname
5+
script_location = alembic
86

7+
# template used to generate migration files
8+
file_template = %%(year)d-%%(month).2d-%%(day).2d-%%(rev)s_%%(slug)s
9+
10+
# timezone to use when rendering the date
11+
# within the migration file as well as the filename.
12+
# string value is passed to dateutil.tz.gettz()
13+
# leave blank for localtime
14+
# timezone =
15+
16+
# max length of characters to apply to the
17+
# "slug" field
18+
#truncate_slug_length = 40
19+
20+
# set to 'true' to run the environment during
21+
# the 'revision' command, regardless of autogenerate
22+
# revision_environment = false
23+
24+
# set to 'true' to allow .pyc and .pyo files without
25+
# a source .py file to be detected as revisions in the
26+
# versions/ directory
27+
# sourceless = false
28+
29+
# version location specification; this defaults
30+
# to alembic/versions. When using multiple version
31+
# directories, initial revisions must be specified with --version-path
32+
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
33+
34+
# the output encoding used when revision files
35+
# are written from script.py.mako
36+
# output_encoding = utf-8
37+
38+
# Logging configuration
939
[loggers]
1040
keys = root,sqlalchemy,alembic
1141

0 commit comments

Comments
 (0)