diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 000000000..1e3b946ff --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,31 @@ +ARG PYTHON_VERSION=3.12-bullseye +FROM mcr.microsoft.com/vscode/devcontainers/python:${PYTHON_VERSION} + +ENV PYTHONUNBUFFERED=1 +ENV DJANGO_SETTINGS_MODULE='config.docker-compose' + +RUN mkdir /code +WORKDIR /code + +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + libpq-dev \ + build-essential \ + rustc \ + cargo && \ + apt-get clean && rm -rf /var/lib/apt/lists/* + +RUN addgroup --gid 1001 --system pokeapi && \ + adduser --uid 1001 --system --ingroup pokeapi --shell /bin/bash pokeapi + +RUN chown -R pokeapi:pokeapi /code + +USER pokeapi + +ADD requirements.txt /code/ +ADD test-requirements.txt /code/ +RUN python3 -m pip install -r test-requirements.txt --no-cache-dir + +ADD . /code/ + +CMD ["sleep", "infinity"] \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 000000000..f15599fbe --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,81 @@ +{ + "name": "PokeAPI", + + // Use multiple docker-compose files for development and overrides + "dockerComposeFile": [ + "../docker-compose.yml", + "./docker-compose.override.yml" + ], + + // Target container service to attach VS Code workspace + "service": "app", + + // Folder inside container where workspace is mounted + "workspaceFolder": "/code", + + "customizations": { + "vscode": { + "settings": { + "python.defaultInterpreterPath": "/usr/local/bin/python", + + // Use bash as the default shell in the integrated terminal + "terminal.integrated.profiles.linux": { + "bash": { + "path": "/bin/bash" + } + }, + "terminal.integrated.defaultProfile.linux": "bash", + + // Enable automatic formatting on save with Black for Python files + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter", + "editor.formatOnSave": true + } + }, + + // Extensions to install automatically in the container's VS Code instance + "extensions": [ + "ms-python.python", + "ms-python.black-formatter" + ] + } + }, + + "features": { + "ghcr.io/devcontainers/features/common-utils:2": { + "installZsh": "false", + "installOhMyZsh": "false", + "configureZshAsDefaultShell": "false" + }, + "ghcr.io/devcontainers/features/git:1": {}, + "ghcr.io/devcontainers/features/docker-outside-of-docker": {} + }, + + // Post-create commands: + "postCreateCommand": "git config --global core.autocrlf input && git config --global --add safe.directory /code", + + "remoteUser": "pokeapi", + + // Ports to forward from container to host for easy access + "forwardPorts": [80, 443, 8000, 8080], + + // Custom port labels and auto-forward notification settings + "portsAttributes": { + "8000": { + "label": "App", + "onAutoForward": "notify" + }, + "80": { + "label": "Web HTTP", + "onAutoForward": "notify" + }, + "443": { + "label": "Web HTTPS", + "onAutoForward": "notify" + }, + "8080": { + "label": "GraphQL", + "onAutoForward": "notify" + } + } +} diff --git a/.devcontainer/docker-compose.override.yml b/.devcontainer/docker-compose.override.yml new file mode 100644 index 000000000..861bdb289 --- /dev/null +++ b/.devcontainer/docker-compose.override.yml @@ -0,0 +1,8 @@ +version: '2.4' + +services: + app: + build: + context: . + dockerfile: ./.devcontainer/Dockerfile + user: pokeapi \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0d1efe63b..05bd41b52 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -26,6 +26,37 @@ git checkout -b my_new_branch Simple! +## Development Environment +Want to get started quickly? Use [Dev Containers](https://code.visualstudio.com/docs/devcontainers/containers). They provide a consistent, pre-configured development environment, ensuring you're ready to go without lengthy setup. + +### Prerequisites: + +* [**Docker**](https://www.docker.com/) (ensure Docker Desktop or your Docker daemon is running) +* [**Visual Studio Code**](https://code.visualstudio.com/) +* [**VS Code Dev Containers Extension**]() + +### Getting Started: + +1. **Open in Dev Container:** Open the project folder in VS Code. Click "Reopen in Container" when prompted (or use the Command Palette). + +2. **Initial Setup:** Once the container is ready, open a terminal and run: + ```bash + make devcontainer-setup + ``` + This command installs requirements, runs migrations, and populates the database. + +### Running Server (Inside the Dev Container): + +* Run Development Server (Django's built-in): + ```bash + make serve + ``` + +* Run Development Server (Gunicorn): + ```bash + make serve-gunicorn + ``` + ## Financial contributions We also welcome financial contributions in full transparency on our [open collective](https://opencollective.com/pokeapi). diff --git a/Makefile b/Makefile index 2beb3553a..7fe8b3b2a 100755 --- a/Makefile +++ b/Makefile @@ -27,6 +27,9 @@ wipe-sqlite-db: # Delete's the project database serve: # Run the project locally python manage.py runserver ${local_config} +serve-gunicorn: # Run the project using Gunicorn + gunicorn config.wsgi:application -c gunicorn.conf.py + test: # Run tests python manage.py test ${local_config} @@ -81,6 +84,23 @@ docker-prod: docker-setup: docker-up docker-migrate docker-build-db # (Docker) Start services, prepare the latest DB schema, populate the DB +devcontainer-migrate: # (Dev Container) Run any pending migrations + python manage.py migrate ${docker_config} + +devcontainer-build-db: # (Dev Container) Build the database + sh -c 'echo "from data.v2.build import build_all; build_all()" | python manage.py shell ${docker_config}' + +devcontainer-make-migrations: # (Dev Container) Create migrations files if schema has changed + python manage.py makemigrations ${docker_config} + +devcontainer-flush-db: # (Dev Container) Removes all the data present in the database but preserves tables and migrations + python manage.py flush --no-input ${docker_config} + +devcontainer-shell: # (Dev Container) Launch an interative Django shell for the pokeapi app + python manage.py shell ${docker_config} + +devcontainer-setup: dev-install devcontainer-migrate devcontainer-build-db # (Dev Container) Install requirements, prepare the latest DB schema, populate the DB + format: # Format the source code black .