From c9c5d0838029addb8d672e40cc362e9379bcfbe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Silva?= Date: Mon, 23 Sep 2024 15:55:48 +0100 Subject: [PATCH 1/9] Remove any .env files --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 076da4c..47e56ee 100644 --- a/.gitignore +++ b/.gitignore @@ -46,7 +46,7 @@ Thumbs.db # env files .envrc .direnv -.env +*.env # Database db.sqlite3 From f743dcba7ae0fadd2569d844d2cde7407f2bc9ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Silva?= Date: Mon, 23 Sep 2024 16:04:12 +0100 Subject: [PATCH 2/9] Move sample env to .sample.env --- .coverage | Bin 69632 -> 0 bytes .gitignore | 1 + .../docker/api.sample.env | 0 .../db.env => .sample.env/docker/db.sample.env | 0 .../docker/main.sample.env | 0 .../docker/nginx.sample.env | 0 .../api.env => .sample.env/local/api.sample.env | 0 .../terraform/dev/api.sample.env | 0 .../terraform/dev/app.sample.env | 0 .../terraform/dev/main.sample.env | 0 .../terraform/dev/nginx.sample.env | 0 .../terraform/main.sample.env | 0 12 files changed, 1 insertion(+) delete mode 100644 .coverage rename .env-sample/docker/api.env => .sample.env/docker/api.sample.env (100%) rename .env-sample/docker/db.env => .sample.env/docker/db.sample.env (100%) rename .env-sample/docker/main.env => .sample.env/docker/main.sample.env (100%) rename .env-sample/docker/nginx.env => .sample.env/docker/nginx.sample.env (100%) rename .env-sample/local/api.env => .sample.env/local/api.sample.env (100%) rename .env-sample/terraform/dev/api.env => .sample.env/terraform/dev/api.sample.env (100%) rename .env-sample/terraform/dev/app.env => .sample.env/terraform/dev/app.sample.env (100%) rename .env-sample/terraform/dev/main.env => .sample.env/terraform/dev/main.sample.env (100%) rename .env-sample/terraform/dev/nginx.env => .sample.env/terraform/dev/nginx.sample.env (100%) rename .env-sample/terraform/main.env => .sample.env/terraform/main.sample.env (100%) diff --git a/.coverage b/.coverage deleted file mode 100644 index cd3b3200c87bf7312506b24177919cb0f1d74f2e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 69632 zcmeI5e{dURddF9*)o<;t{8M%u+Z*SHe_J-WkjsVOq=`u~5RL=lBo3gg<+Yp_{cO^p-M z=iSvxYs*gJ)n=}wj}q(MpWo+sKkxIt`@Xw=y$^inp12|jqjDxGD#B_iL^&MPMnRw` z$`3yk@MC;Du)r8AkUC7u3oZJop44fUAEbirCn^3B_S<}S@OgGs;H_ZN_e9{NNA$&D z2Y!$M5@h_gPDn~dj7b^&o85f>z@`TWgaK)CS`Rm+TtVa z{`TWX{hn}DmE(k@HFptX-=XrF#&-j<0@@#J~V z%paaVc@&&W$p+TqiNrLVtofYEC5Hj|fm||83J1>64J-0OXD)QEBIh%E!(}s1Sr+p< zt2hnhq(t?za;f;ioKz_ME}?j4mzIr`+$E4qKm@D|xWntJ927*QbU1r30sXD0m{VkJ zJqm>%?Srr2cPqS}aC5Wc#6c}k2%hM0T*(#|e6xb4uz7*dA^R7O8&XSV2MMV~iG(1J zYE_1uE$D><(QK|W5d(&pAr(!K^9O>bzt?CZ&VmoZa6%rQ9cx;|WF@E|&=wenjpkA@ zQdP1f4>5UM%7|l9Pkyi7Ed0I$2|}mO=ME1vFBCe35Qz2_7X50y+Y@eRa2)e#VU#be zeBzlELPkl>(~KIsl9HIsWsHX8T8*S}MzNJN>PUVMWT)HX4sU8$C_DM%qJ8-Vuj*ku z;hGwUs@Dk8E|DMCtW1zC#VRo8LU7~mr$uGIQ0hC`434+D+~M^#3pq|)i1v|h_wl{7 z=JJVpjeZguee&a+i~;~A*3&<5*Q0D&AhBErGqu$)u0_$8}a zc#>;R(O+GlxNfkAquRB@p?cpZXR~p*tdh%C_FKkQp>@BQC9P1lRT!4#ge0c)YXh9I zJqtGDSwZum^s3Zf)W}W{jj{1o%_rmHLYnypq0$-k3l@?|F*TCEUXjbNt_p2Gshq@) z-iW}7LM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>@^<@_=j&AOUS_ZN#b+6V0x0*{;{B|C@7`w6U2lu77ALbh>kl@8 zc(*O$rf$ZrMo?W{mTIy4Vh(O`DF|=1Q@A9&hZ?}dx|?7E0(@i%Xj^E>OjUjkm-#|` zsNU`z6oY(k9cY&qjKv%nOGdAEAPnOC;?e89h{E&#ZM7g%V^zj*iag`lQUgk?RVCy3 z|6SFfQejmk-^PPC16AeVDLnswxDsUg7w^usg3vTB8ms{M@|wXy5txTAgQ2o@XFsRU zUoZFZpgT|&j@h~;Ey^AaEUdNP0)#}21?jtOl%B6W*%P$c|6+5pEdbIMs;VA~=I8$p z`9W!ett3oAz0(Kk<@MDHL1Ma2ZuNp}l~t!rLic+>sD1H5CF3uX=71YCEw{#n=l`2s zAQQ0aTK@U}R>m5rlIQ>1oFHT&;7XtWZ==gnGJ)=QfRc0ZgbL69$@rh<;rag$;R`=V z00|%gB!C2v01`j~NB{{S0VIF~kbp%3TY6xKZsA67{ Date: Mon, 23 Sep 2024 16:06:02 +0100 Subject: [PATCH 3/9] Fix populate script email restriction --- photo/tests/factories.py | 1 + 1 file changed, 1 insertion(+) diff --git a/photo/tests/factories.py b/photo/tests/factories.py index 8b16855..ce799c5 100644 --- a/photo/tests/factories.py +++ b/photo/tests/factories.py @@ -20,6 +20,7 @@ class UserFactory(factory.django.DjangoModelFactory): class Meta: model = User skip_postgeneration_save = True + django_get_or_create = ("email",) user_handle = factory.Faker("name") email = factory.Sequence(lambda n: "user{0}@email.com".format(n)) From 16967cb721e00c22ca95609efa15c47ffa2bcb01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Silva?= Date: Mon, 23 Sep 2024 16:06:10 +0100 Subject: [PATCH 4/9] Add terraform version --- .terraform-version | 1 + 1 file changed, 1 insertion(+) create mode 100644 .terraform-version diff --git a/.terraform-version b/.terraform-version new file mode 100644 index 0000000..7bc1c40 --- /dev/null +++ b/.terraform-version @@ -0,0 +1 @@ +1.9.6 From 0bf758d711bce43de4a0e1a7ccad5365ee06ca19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Silva?= Date: Mon, 23 Sep 2024 16:07:18 +0100 Subject: [PATCH 5/9] Add terraform makefile --- terraform/.sample.env | 21 +++++++++++++++++++++ terraform/Makefile | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 terraform/.sample.env create mode 100644 terraform/Makefile diff --git a/terraform/.sample.env b/terraform/.sample.env new file mode 100644 index 0000000..c70d81e --- /dev/null +++ b/terraform/.sample.env @@ -0,0 +1,21 @@ +# AWS settings +AWS_ACCOUNT_ID= # replace +AWS_ACCESS_KEY_ID= # replace +AWS_SECRET_ACCESS_KEY= # replace +AWS_DEFAULT_REGION=eu-west-1 +AWS_ECR_URL=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com + +# Terraform main settings +TF_VAR_region=${AWS_DEFAULT_REGION} +TF_VAR_docker_url_api=${AWS_ECR_URL}/revent-api:latest +TF_VAR_docker_url_app=${AWS_ECR_URL}/revent-app:latest +TF_VAR_docker_url_nginx=${AWS_ECR_URL}/revent-nginx:latest + +# Terraform PostgreSQL settings +TF_VAR_rds_db_name=revent +TF_VAR_rds_db_schema=public +TF_VAR_rds_username= # replace +TF_VAR_rds_password= # replace +TF_VAR_rds_port=5432 + +APP_PATH=../revent-app \ No newline at end of file diff --git a/terraform/Makefile b/terraform/Makefile new file mode 100644 index 0000000..3ece84f --- /dev/null +++ b/terraform/Makefile @@ -0,0 +1,32 @@ +# Makefile used to manage Terraform commands +.DEFAULT_GOAL := help + +include .env +export + +help: + @grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done + + +init: # Initialize Terraform's working directory + terraform init + +plan: # View the changes that are required by the current infrastructure's configuration + terraform plan + +json: # Export the plan into a JSON file that can be read by Rover + terraform plan -out plan.out + terraform show -json plan.out > plan.json + rm plan.out + +apply: # Create or update the infrastructure + terraform apply + +destroy: # Destroy previously-created infrastructure + terraform destroy + +build: # Build docker images and push them to ECR + aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | docker login --username AWS --password-stdin ${AWS_ECR_URL} + docker buildx build --platform=linux/arm64 -t ${TF_VAR_docker_url_api} . && docker push ${TF_VAR_docker_url_api} + cd ${APP_PATH} && docker buildx build --platform=linux/arm64 -t ${TF_VAR_docker_url_app} . && docker push ${TF_VAR_docker_url_app} + cd nginx && docker buildx build --platform=linux/arm64 -t ${TF_VAR_docker_url_nginx} . && docker push ${TF_VAR_docker_url_nginx} \ No newline at end of file From a8d847a34859d5390c485204701a23e064a74fab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Silva?= Date: Mon, 23 Sep 2024 16:08:45 +0100 Subject: [PATCH 6/9] Add local makefile --- local/.sample.env | 34 ++++++++++++++++++++++++++++++++ local/Makefile | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 local/.sample.env create mode 100644 local/Makefile diff --git a/local/.sample.env b/local/.sample.env new file mode 100644 index 0000000..b7f7c22 --- /dev/null +++ b/local/.sample.env @@ -0,0 +1,34 @@ +# Django settings +DEBUG=True +SECRET_KEY= # replace +ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0 +CSRF_TRUSTED_ORIGINS=http://localhost,http://localhost:3000,http://127.0.0.1:3000,http://127.0.0.1 +CORS_ALLOWED_ORIGINS=http://localhost,http://localhost:3000,http://127.0.0.1:3000,http://127.0.0.1,http://127.0.0.1:3001 +CORS_ORIGIN_WHITELIST=127.0.0.1 +MAX_PICTURE_SIZE=8000000 +BASE_BACKEND_URL=http://localhost:8000/ +BASE_APP_URL=http://localhost:3000/ + +# Google settings +GOOGLE_CLIENT_ID= # replace +GOOGLE_CLIENT_SECRET= # replace +ALLOWED_REDIRECT_URIS=http://localhost:3000,http://localhost/,http://127.0.0.1:3000 + +# PostgreSQL settings +POSTGRES_HOST=localhost +POSTGRES_PORT=5432 +POSTGRES_DB=revent +POSTGRES_SCHEMA=public +POSTGRES_USER= # replace +POSTGRES_PASSWORD= # replace + +# AWS settings +AWS_ACCOUNT_ID= # replace +AWS_ACCESS_KEY_ID= # replace +AWS_SECRET_ACCESS_KEY= # replace +AWS_DEFAULT_REGION=eu-west-1 +AWS_S3_BUCKET_NAME=revent-storage +AWS_S3_ENDPOINT_URL=http://localhost.localstack.cloud:4566/ +AWS_STORAGE_BUCKET_NAME=revent-media +AWS_QUERYSTRING_AUTH=False +AWS_S3_SIGNATURE_VERSION=s3v4 \ No newline at end of file diff --git a/local/Makefile b/local/Makefile new file mode 100644 index 0000000..4a44eaa --- /dev/null +++ b/local/Makefile @@ -0,0 +1,49 @@ +# Makefile to manage local development commands +.DEFAULT_GOAL := help + +CWD:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) +MOVE_TO_ROOT = cd "$(CWD)/.." +RUN_WITH_POETRY = $(MOVE_TO_ROOT) && poetry run +RUN_WITH_DJANGO = $(RUN_WITH_POETRY) python manage.py + +include .env +export + +help: + @grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done + +superuser: createsuperuser # Create admin user +run: runserver # Run the server +migrations: makemigrations # Create new migrations +migrate: # Migrate all new migrations +shell: # Launch python shell +urls: show_urls # Show server urls + +DJANGO_COMMANDS = createsuperuser\ + runserver\ + makemigrations\ + migrate\ + shell\ + show_urls + +$(DJANGO_COMMANDS): + $(RUN_WITH_DJANGO) $@ + +# Pass arguments to the test command +ifeq (test,$(firstword $(MAKECMDGOALS))) + TEST_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) + $(eval $(TEST_ARGS):;@:) +endif + +test: # Test all django apps OR pass testing target like "photo" or "photo.tests.test_mutations" + $(RUN_WITH_DJANGO) test --no-input $(TEST_ARGS) + +coverage: # Get test coverage + $(MOVE_TO_ROOT) && coverage erase && coverage run manage.py test && coverage html --skip-empty --skip-covered + open htmlcov/index.html + +populate: # Generate dummy data + $(RUN_WITH_POETRY) python launch.py + +pre: # Run pre-commit + $(MOVE_TO_ROOT) && pre-commit run --all-files From e120f16a93c52b30cd2b9ff75f7a72074a577cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Silva?= Date: Mon, 23 Sep 2024 16:12:49 +0100 Subject: [PATCH 7/9] Add move docker files to docker directory --- docker/.dockerignore | 19 ++++++++++++ docker/Dockerfile | 41 ++++++++++++++++++++++++ docker/api.sample.env | 34 ++++++++++++++++++++ docker/db.sample.env | 4 +++ docker/docker-compose.local.yml | 18 +++++++++++ docker/docker-compose.yml | 55 +++++++++++++++++++++++++++++++++ docker/main.sample.env | 1 + docker/nginx.sample.env | 5 +++ 8 files changed, 177 insertions(+) create mode 100644 docker/.dockerignore create mode 100644 docker/Dockerfile create mode 100644 docker/api.sample.env create mode 100644 docker/db.sample.env create mode 100644 docker/docker-compose.local.yml create mode 100644 docker/docker-compose.yml create mode 100644 docker/main.sample.env create mode 100644 docker/nginx.sample.env diff --git a/docker/.dockerignore b/docker/.dockerignore new file mode 100644 index 0000000..6e130b4 --- /dev/null +++ b/docker/.dockerignore @@ -0,0 +1,19 @@ +*.env* + +.git/ +.github/ +.pytest_cache/ +.vscode/ +.env-sample/ + +docs/ +nginx/ +terraform/ + +.flake8 +.gitignore +.pre-commit-config.yaml +docker-compose.yml +Dockerfile +Makefile +README.md diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..95e283d --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,41 @@ +FROM python:3.10.13 AS base + +# Configure volume/workdir +RUN mkdir /code +WORKDIR /code/ +COPY pyproject.toml /code/ +COPY poetry.lock /code/ + +FROM base as deps + +# set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED TRUE +ENV PYTHONPATH=/code/:/usr/lib/python3.10.2/site-packages + +# Configure volume/workdir +WORKDIR /code/ + +# Install poetry +RUN pip install --upgrade pip +RUN pip install --no-cache-dir poetry + +# Add and install requirements +RUN poetry config virtualenvs.create false && poetry install --only main --no-interaction --no-ansi + +FROM deps + +WORKDIR /code/ +COPY . /code/ + +RUN mkdir static +# RUN python manage.py collectstatic --noinput + +# Create a user with UID 1000 and GID 1000 +RUN groupadd -g 1000 appgroup && \ + useradd -r -u 1000 -g appgroup appuser +# Switch to this user +USER 1000:1000 + +# copy project +COPY . . diff --git a/docker/api.sample.env b/docker/api.sample.env new file mode 100644 index 0000000..320da68 --- /dev/null +++ b/docker/api.sample.env @@ -0,0 +1,34 @@ +# Django settings +DEBUG=True +SECRET_KEY= # replace +ALLOWED_HOSTS=localhost,127.0.0.1 +CSRF_TRUSTED_ORIGINS= +CORS_ALLOWED_ORIGINS= +CORS_ORIGIN_WHITELIST= +MAX_PICTURE_SIZE=8000000 +BASE_BACKEND_URL=http://localhost:8000/ +BASE_APP_URL=http://localhost:3000/ + +# Google settings +GOOGLE_CLIENT_ID= # replace +GOOGLE_CLIENT_SECRET= # replace +ALLOWED_REDIRECT_URIS=http://localhost + +# PostgreSQL settings +POSTGRES_HOST=revent-db +POSTGRES_PORT=5432 +POSTGRES_DB=revent +POSTGRES_SCHEMA=public +POSTGRES_USER= # replace +POSTGRES_PASSWORD= # replace + +# AWS settings +AWS_ACCOUNT_ID= # replace +AWS_ACCESS_KEY_ID= # replace +AWS_SECRET_ACCESS_KEY= # replace +AWS_DEFAULT_REGION=eu-west-1 +AWS_S3_BUCKET_NAME=revent-storage +AWS_S3_ENDPOINT_URL=http://localhost.localstack.cloud:4566/ +AWS_STORAGE_BUCKET_NAME=revent-media +AWS_QUERYSTRING_AUTH=False +AWS_S3_SIGNATURE_VERSION=s3v4 \ No newline at end of file diff --git a/docker/db.sample.env b/docker/db.sample.env new file mode 100644 index 0000000..defd84b --- /dev/null +++ b/docker/db.sample.env @@ -0,0 +1,4 @@ +# PostgreSQL settings +POSTGRES_DB=revent +POSTGRES_USER= # replace +POSTGRES_PASSWORD= # replace \ No newline at end of file diff --git a/docker/docker-compose.local.yml b/docker/docker-compose.local.yml new file mode 100644 index 0000000..f6af58f --- /dev/null +++ b/docker/docker-compose.local.yml @@ -0,0 +1,18 @@ +services: + revent-db: + container_name: revent-db + image: postgres:14.1-alpine + env_file: + - db.env + ports: + - "5432:5432" + + revent-s3: + container_name: revent-s3 + image: localstack/localstack:latest + ports: + - "4566:4566" + - "4510-4559:4510-4559" + +volumes: + postgres_data: diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml new file mode 100644 index 0000000..16a888c --- /dev/null +++ b/docker/docker-compose.yml @@ -0,0 +1,55 @@ +services: + revent-db: + container_name: revent-db + image: postgres:14.1-alpine + env_file: + - db.env + ports: + - "5432:5432" + + revent-s3: + container_name: revent-s3 + image: localstack/localstack:latest + ports: + - "4566:4566" + - "4510-4559:4510-4559" + + revent-api: + build: + context: .. + dockerfile: docker/Dockerfile + command: uvicorn config.asgi:application --host 0.0.0.0 --port 8000 + env_file: + - api.env + volumes: + - .:/appqware()|_m|^_--_------------------------------------------------------------------------- + - efs:/efs + user: root + ports: + - "8000:8000" + depends_on: + - revent-db + + revent-app: + build: ${APP_PATH} + command: node server.js + ports: + - "3000:3000" + depends_on: + - revent-api + + revent-nginx: + build: ../nginx + env_file: + - nginx.env + ports: + - "80:80" + volumes: + - efs:/efs + depends_on: + - revent-api + - revent-app + +volumes: + postgres_data: + efs: diff --git a/docker/main.sample.env b/docker/main.sample.env new file mode 100644 index 0000000..09c3362 --- /dev/null +++ b/docker/main.sample.env @@ -0,0 +1 @@ +APP_PATH=../revent-app \ No newline at end of file diff --git a/docker/nginx.sample.env b/docker/nginx.sample.env new file mode 100644 index 0000000..6797212 --- /dev/null +++ b/docker/nginx.sample.env @@ -0,0 +1,5 @@ +# NGINX settings +API_HOST=revent-api +API_PORT=8000 +APP_HOST=revent-app +APP_PORT=3000 \ No newline at end of file From 0c8c379c6a232d9950bdc371163f7b0c25e2514d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Silva?= Date: Mon, 23 Sep 2024 16:13:19 +0100 Subject: [PATCH 8/9] Add docker makefile --- docker/Makefile | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 docker/Makefile diff --git a/docker/Makefile b/docker/Makefile new file mode 100644 index 0000000..545c504 --- /dev/null +++ b/docker/Makefile @@ -0,0 +1,20 @@ +# Makefile to manage Docker containers +.DEFAULT_GOAL := help + +include main.env +export + +help: + @grep -E '^[a-zA-Z0-9 -]+:.*#' Makefile | sort | while read -r l; do printf "\033[1;32m$$(echo $$l | cut -f 1 -d':')\033[00m:$$(echo $$l | cut -f 2- -d'#')\n"; done + +up: # Start the docker containers + docker compose up -d + +down: # Stop the docker containers + docker compose down -t 1 + +up-local: # Start the docker containers locally + docker compose -f docker-compose.local.yml up -d + +down-local: # Stop the docker containers locally + docker compose -f docker-compose.local.yml down -t 1 From c9870fa3b07f408474fcdbe79f6a3c1a69b70733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Silva?= Date: Mon, 23 Sep 2024 16:51:02 +0100 Subject: [PATCH 9/9] Update Readme --- README.md | 245 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 146 insertions(+), 99 deletions(-) diff --git a/README.md b/README.md index 21460b9..ff2d6c2 100644 --- a/README.md +++ b/README.md @@ -11,156 +11,198 @@ Photo contest API is an API for the Runtime Revolution photo contest that takes - [PHOTO CONTEST API 📸](#photo-contest-api-) - [Description](#description) - [Table of Contents](#table-of-contents) + - [Guidelines](#guidelines) + - [Branch Naming](#branch-naming) + - [Who do I talk to?](#who-do-i-talk-to) - [Setup Time](#setup-time) - - [Prerequisites -](#prerequisites--) - - [Configuration - Setting your python version](#configuration---setting-your-python-version) - - [Configuration - Using poetry for package and dependency management](#configuration---using-poetry-for-package-and-dependency-management) + - [Prerequisites](#prerequisites) + - [Python](#install-the-correct-python-version) + - [Poetry](#install-poetry) + - [Poetry Basics](#poetry-basics) + - [Makefile](#makefile) - [Running the Project](#running-the-project) - [Running Tests](#running-tests) - [TLDR;](#tldr) - [Setting up localstack s3 to fake Aws](#setting-up-localstack-s3-to-fake-aws) - [Nice to Have](#nice-to-have) - [Direnv](#direnv) - - [Guidelines](#guidelines) - - [Branch Naming](#branch-naming) - - [Who do I talk to?](#who-do-i-talk-to) + - [Terraform](#terraform) - [Data model](#data-model) - [FAQ](#faq) - [Macos m1](#macos-m1) - [gdal related error](#gdal-related-error) +## Guidelines + +### Branch Naming + +Everytime we need to work on a new feature, bug or any type of task, we need to create a new branch. +Below will be a step by step guide on how we can create and start to work on a task: + +The nomenclature for someone named "João Gomes" that need to fix a new "Bug" on a ticket with the ID "1234" and name "Update salary" goes like this: + +`jg/bug/1234/Update-salary` + +- jg - First and last name initials + +- bug - Type of task to be performed + +- 1234 - Ticket ID + +- Update-salary - Ticket name + +## Who do I talk to? + +- You can always talk to your Team Leader or the repo owner for any question regarding this project. + ## Setup Time -### Prerequisites - +### Prerequisites + +#### Install the correct Python version -Install [pyenv](https://github.com/pyenv/pyenv) and [poetry](https://python-poetry.org/docs/#installation) +1 - Install pyenv -If you have this one already, you can jump to the next section [(Configuration - Setting your python version)](#configuration---setting-your-python-version). +```bash +brew install pyenv -### Configuration - Setting your python version +echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc +echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc +echo 'eval "$(pyenv init -)"' >> ~/.zshrc +``` -- Now that you have pyenv, install the version we want to use: +2 - Install the project's python version - ```bash - pyenv install -v 3.10.4 - ``` +This command will install the python version that is set in `.python-version` -- Check your list for the installed python version: +```bash +pyenv install +``` - ```bash - ls ~/.pyenv/versions/ - ``` +3 - Verify that you're using the correct Python version - or by using +```bash +pyenv versions +``` - ```bash - pyenv versions - ``` +#### Install Poetry -### Configuration - Using poetry for package and dependency management +```bash +curl -sSL https://install.python-poetry.org | python - +echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc +``` - *To use poetry, we will need to have ***python 3.5 or greater***.* +### Poetry Basics - Now let's start by always activating a virtual environment to work on when we open the project: - If you need to get out of this virtual environment just type **deactivate** + If you need to get out of this virtual environment just type **deactivate** - ```bash - poetry shell - ``` + ```bash + poetry shell + ``` - You should have a file called pyproject.toml, with it we can install everything that is needed! Just a second, make sure you are on your project folder! Let's go and install everything now: - ```bash - poetry install - ``` + ```bash + poetry install --no-root + ``` - This is great! Right now, we have our packages installed! + This is great! Right now, we have our packages installed! - One last thing! We still need to tell our IDE to use this new interpreter! -Alright, and how do we do this? Its easy, really. (This next steps will be for vscode) + Alright, and how do we do this? Its easy, really. (This next steps will be for vscode) + + 1. Copy the path from the command below: - 1. Copy the path from the command below: + ```bash + poetry env info --path + ``` - ```bash - poetry env info --path - ``` + 2. In your vscode, go to settings and search for python interpreter - 2. In your vscode, go to settings and search for python interpreter + 3. Now select to use a specific path and use the one you just copied + Press Enter and you'r done! Now vscode will use this one. - 3. Now select to use a specific path and use the one you just copied - Press Enter and you'r done! Now vscode will use this one. +### Makefile + +A lot of commands are available in the Makefile to simplify the setup and day to day tasks. +To get some info regarding these commands you can run: + +```bash +make +# OR +make help +``` ### Running the Project -*Now, how can we run the app?* +To start the docker containers run + +```bash +make up +``` -To start the docker container run - ```bash - make up - ``` After that run the migrations if needed - ```bash - make migrate - ``` + +```bash +make migrate +``` + Finally run the app - ```bash - make run - ``` +```bash +make run +``` Perfect! You are now running the project locally and you can now start coding! ## Running Tests To run the tests, inside your working environment run: - ```bash - make test - ``` -To add tests remember that the python automatic discover only works properly for files named "test_*.py". +```bash +make test +``` -If you want to use the vscode debugger for a particular test you can use the launch.json.example, rename it launch.json -and place it in the .vscode folder in the root of your working directory. +To add tests remember that the python automatic discover only works properly for files named "test\_\*.py". ## Dummy data + To generate dummy data ensure that the localstack is running and all the migrations are aplied and use the command: ```bash - make populate - ``` - -This command should only be used when the database is empty or it will probaly raise integraty errors related to duplicate -primary keys. +make populate +``` ### TLDR; Check what's available in the Makefile. Python 3.10.13 environment must exist and be active. Use poetry shell for this. - ```bash - make up - make migrate - make run - ``` + +```bash + make up + make migrate + make run +``` ### Setting up localstack s3 to fake Aws After you have installed Docker we will need to install AWS CLI. Even though we are simulating the AWS, we will require this to comunicate with the docker containers: - 1. Install [AWS CLI](https://aws.amazon.com/cli/) - 2. After it is installed we will need to supply credentials, even if the - credentials are dummies. Localstack requires this details to be present. For that run the command: - ```bash - aws configure - ``` + +1. Install [AWS CLI](https://aws.amazon.com/cli/) +2. After it is installed we will need to supply credentials, even if the + credentials are dummies. Localstack requires this details to be present. For that run the command: + ```bash + aws configure + ``` In order to create a bucket you can just run the tests to verify everithing is working properly and it will be created automatically. ### Nice to Have -Well, hello there! If you are looking for automation, you have come to the right place! - #### Direnv Looking for a way to start your venv by doing... nothing!? @@ -169,44 +211,49 @@ I know i know, it looks almost like magic. Let me tell you about direnv - direnv will load and unload a virtual environment everytime we enter or leave the folder How do we install it? Let's take a look. - 1. Let's start by creating an empty file called .envrc - - 2. Now we can download direnv [here](https://direnv.net/docs/installation.html) + 1. Let's start by creating an empty file called .envrc - 3. Hook it into your shell like [this](https://direnv.net/docs/hook.html) + 2. Now we can download direnv [here](https://direnv.net/docs/installation.html) - 4. Alright and finally add the following to your .envrc: + 3. Hook it into your shell like [this](https://direnv.net/docs/hook.html) - `layout pyenv 3.10.4` - Note: Everytime you edit your .envrc file direnv will ask you to give permission by typing: - `direnv allow` + 4. Alright and finally add the following to your .envrc: - 5. ***Important: Now that you changed for direnv replicate the python interpreter change that you did in poetry section*** + `layout pyenv 3.10.4` + Note: Everytime you edit your .envrc file direnv will ask you to give permission by typing: + `direnv allow` - 6. Reload your shell and you'r set! Now you can leave your folder and come back inside to test if .direnv will show up (thats your new venv). - Everytime you go into or out of your project folder .direnv will activate or deactivate! -## Guidelines + 5. **_Important: Now that you changed for direnv replicate the python interpreter change that you did in poetry section_** -### Branch Naming + 6. Reload your shell and you'r set! Now you can leave your folder and come back inside to test if .direnv will show up (thats your new venv). + Everytime you go into or out of your project folder .direnv will activate or deactivate! -Everytime we need to work on a new feature, bug or any type of task, we need to create a new branch. -Below will be a step by step guide on how we can create and start to work on a task: +## Terraform -The nomenclature for someone named "João Gomes" that need to fix a new "Bug" on a ticket with the ID "1234" and name "Update salary" goes like this: +This is the easiest way to install Terraform in ARM based CPUs. If you install Terraform directly with an ARM based CPU you might get some errors saying that some providers are not available for ARM. For Intel you can just install Terraform directly. -`jg/bug/1234/Update-salary` +### Install TFenv -- jg - First and last name initials +```bash +brew install tfenv +``` -- bug - Type of task to be performed +### Install Terraform -- 1234 - Ticket ID +This command will install the Terraform version that is set in `.terraform-version` -- Update-salary - Ticket name +```bash +tfenv use # If Intel CPU +TFENV_ARCH=amd64 tfenv use # If ARM CPU +``` -## Who do I talk to? +You can check your architecture by running the following command: -- You can always talk to your Team Leader or the repo owner for any question regarding this project. +```bash +uname -a +# output example for ARM CPU +> Darwin 23.6.0 Darwin Kernel Version 23.6.0: ; root:xnu-10063.141.2~1/RELEASE_ARM64_T8103 arm64 +``` ## Data model @@ -270,7 +317,7 @@ classDiagram +User votes } -`````` +``` ## FAQ