diff --git a/.devcontainer/.env b/.devcontainer/.env new file mode 100644 index 0000000..3b5b363 --- /dev/null +++ b/.devcontainer/.env @@ -0,0 +1,21 @@ +PUBLIC_SERVER_URL='http://127.0.0.1:8000' +ORIGIN='http://127.0.0.1:5173' +__VITE_ADDITIONAL_SERVER_ALLOWED_HOSTS='127.0.0.1' +BODY_SIZE_LIMIT=Infinity +POSTGRES_DB=database +POSTGRES_USER=adventure +POSTGRES_PASSWORD=changeme123 +PGHOST=localhost +PGDATABASE=database +PGUSER=adventure +PGPASSWORD=changeme123 +SECRET_KEY=changeme123 +DJANGO_ADMIN_USERNAME=admin +DJANGO_ADMIN_PASSWORD=admin +DJANGO_ADMIN_EMAIL='admin@example.com' +PUBLIC_URL='http://127.0.0.1:8000' +CSRF_TRUSTED_ORIGINS='http://127.0.0.1:8000,http://127.0.0.1:5173' +DEBUG=True +FRONTEND_URL='http://127.0.0.1:5173' +EMAIL_BACKEND=console +SECRET_KEY='pleasechangethisbecauseifyoudontitwillbeverybadandyouwillgethackedinlessthanaminuteguaranteed' \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 7b2568a..f3a5f12 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -8,19 +8,28 @@ "ghcr.io/devcontainers/features/docker-in-docker:2": {}, "ghcr.io/devcontainers/features/node:1": {}, "ghcr.io/devcontainers/features/python:1": {} - } + }, // Features to add to the dev container. More info: https://containers.dev/features. // "features": {}, // Use 'forwardPorts' to make a list of ports inside the container available locally. - // "forwardPorts": [], + "forwardPorts": [5173,8000], // Use 'postCreateCommand' to run commands after the container is created. - // "postCreateCommand": "uname -a", + "postCreateCommand": "./.devcontainer/postcreate.sh", + + "postStartCommand": "./.devcontainer/poststart.sh", // Configure tool-specific properties. - // "customizations": {}, + "customizations": { + "vscode": { + "extensions": [ + "svelte.svelte-vscode", + "Lokalise.i18n-ally" + ] + } + } // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. // "remoteUser": "root" diff --git a/.devcontainer/postcreate.sh b/.devcontainer/postcreate.sh new file mode 100755 index 0000000..a7e9731 --- /dev/null +++ b/.devcontainer/postcreate.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Install dependencies for backend and poststart.sh +sudo apt update && sudo apt install -y python3-gdal postgresql-client +pip install -r ./backend/server/requirements.txt +cd ./backend/server +# Create static files on backend +python manage.py collectstatic --noinput +cd ../../frontend +# Install frontend dependencies +npm install \ No newline at end of file diff --git a/.devcontainer/poststart.sh b/.devcontainer/poststart.sh new file mode 100755 index 0000000..d3159e2 --- /dev/null +++ b/.devcontainer/poststart.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +set -a # Make sure to export vars to the whole environment +source ./.devcontainer/.env +set +a + +# Run PostGIS with "docker in docker" on port 5432 +docker run --rm --name adventurelog-devdb -e POSTGRES_USER -e POSTGRES_PASSWORD -e POSTGRES_DB -p 5432:5432 -d postgis/postgis:15-3.3 + +cd ./backend/server + +# Function to check PostgreSQL availability +check_postgres() { + psql -c '\q' >/dev/null 2>&1 +} + +# Wait for PostgreSQL to become available +until check_postgres; do + >&2 echo "PostgreSQL is unavailable - sleeping" + sleep 1 +done + +>&2 echo "PostgreSQL is up - continuing..." + +# Apply Django migrations +python manage.py migrate + +# Create superuser if environment variables are set and there are no users present at all. +if [ -n "$DJANGO_ADMIN_USERNAME" ] && [ -n "$DJANGO_ADMIN_PASSWORD" ] && [ -n "$DJANGO_ADMIN_EMAIL" ]; then + echo "Creating superuser..." + python manage.py shell << EOF +from django.contrib.auth import get_user_model +from allauth.account.models import EmailAddress +User = get_user_model() +# Check if the user already exists +if not User.objects.filter(username='$DJANGO_ADMIN_USERNAME').exists(): + # Create the superuser + superuser = User.objects.create_superuser( + username='$DJANGO_ADMIN_USERNAME', + email='$DJANGO_ADMIN_EMAIL', + password='$DJANGO_ADMIN_PASSWORD' + ) + print("Superuser created successfully.") + # Create the EmailAddress object for AllAuth + EmailAddress.objects.create( + user=superuser, + email='$DJANGO_ADMIN_EMAIL', + verified=True, + primary=True + ) + print("EmailAddress object created successfully for AllAuth.") +else: + print("Superuser already exists.") +EOF +fi + +# Sync the countries and world travel regions +mkdir media +python manage.py download-countries +if [ $? -eq 137 ]; then + >&2 echo "WARNING: The download-countries command was interrupted. This is likely due to lack of memory allocated to the container or the host. Please try again with more memory." + exit 1 +fi + +# Run backend in dev mode +nohup $SHELL -c "python manage.py runserver 2>&1 | tee -a /tmp/servers.log >> /tmp/backend.log" > /dev/null & + +cd ../../frontend + +# Run frontend in dev mode +nohup $SHELL -c "npm run dev 2>&1 | tee -a /tmp/servers.log >> /tmp/frontend.log" > /dev/null & diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 7b0edb3..b9967f0 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -9,5 +9,10 @@ export default defineConfig({ Icons({ compiler: 'svelte' }) - ] + ], + server: { + watch: { + usePolling: true + } + } });