From 320efb9cf1425f23eede9db10a46e342456975da Mon Sep 17 00:00:00 2001 From: Andrew4Coding <andrewdevitoaryo@gmail.com> Date: Fri, 14 Mar 2025 22:09:27 +0700 Subject: [PATCH] deploy: scripts and fly --- .dockerignore | 4 ++++ Dockerfile | 31 +++++++++++++++++++++++++++++++ education/views.py | 2 +- fly.toml | 35 +++++++++++++++++++++++++++++++++++ main/settings.py | 13 ++++++++++--- main/urls.py | 1 - requirements.txt | 4 ++++ startup.sh | 14 ++++++++++++++ 8 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 fly.toml create mode 100644 startup.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..052bd4b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +fly.toml +.git/ +*.sqlite3 +env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..52f73ed --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +ARG PYTHON_VERSION=3.11-slim + +FROM python:${PYTHON_VERSION} + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +RUN mkdir -p /code + +WORKDIR /code + +COPY requirements.txt /tmp/requirements.txt +RUN set -ex && \ + pip install --upgrade pip && \ + pip install -r /tmp/requirements.txt && \ + rm -rf /root/.cache/ +COPY . /code + +RUN apt-get update && apt-get install -y \ + sqlite3 \ + && rm -rf /var/lib/apt/lists/* + +RUN python manage.py collectstatic --noinput + +# Create data directory for SQLite +RUN mkdir -p /data +VOLUME /data + +RUN chmod +x startup.sh +EXPOSE 8000 +ENTRYPOINT ["./startup.sh"] diff --git a/education/views.py b/education/views.py index 3b44b07..0d9307a 100644 --- a/education/views.py +++ b/education/views.py @@ -14,7 +14,7 @@ def show_create(request: HttpRequest): form = EdukasiForm(request.POST) if form.is_valid(): form.save() - return redirect('show-all') # Redirect to a success page + return redirect('education-show-all') # Redirect to a success page else: form = EdukasiForm() diff --git a/fly.toml b/fly.toml new file mode 100644 index 0000000..3eb1af2 --- /dev/null +++ b/fly.toml @@ -0,0 +1,35 @@ +# fly.toml app configuration file generated for el-pekape on 2025-03-14T21:08:45+07:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = 'el-pekape' +primary_region = 'sin' +console_command = '/code/manage.py shell' + +[build] + +[env] + PORT = '8000' + +[mounts] + source="data" + destination="/data" + +[http_service] + internal_port = 8000 + force_https = true + auto_stop_machines = 'stop' + auto_start_machines = true + min_machines_running = 0 + processes = ['app'] + +[[vm]] + memory = '1gb' + cpu_kind = 'shared' + cpus = 1 + +[[statics]] + guest_path = '/code/static' + url_prefix = '/static/' + diff --git a/main/settings.py b/main/settings.py index 1fa4028..701a061 100644 --- a/main/settings.py +++ b/main/settings.py @@ -11,8 +11,6 @@ https://docs.djangoproject.com/en/5.1/ref/settings/ """ from pathlib import Path -from django.conf import settings -from django.conf.urls.static import static import os # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -30,6 +28,11 @@ DEBUG = True ALLOWED_HOSTS = ['*'] +CSRF_TRUSTED_ORIGINS = [ + "https://el-pekape.fly.dev", + "https://pkpl.andrew-devito.website", +] + # Application definition @@ -78,10 +81,14 @@ WSGI_APPLICATION = 'main.wsgi.application' # Database # https://docs.djangoproject.com/en/5.1/ref/settings/#databases +import dotenv + +env = dotenv.dotenv_values() + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', + 'NAME': os.path.join('/data', 'db.sqlite3'), # Store SQLite in /data } } diff --git a/main/urls.py b/main/urls.py index f1afc3c..c3e770c 100644 --- a/main/urls.py +++ b/main/urls.py @@ -23,5 +23,4 @@ urlpatterns = [ path("", show_home, name="home"), path('education/', include('education.urls')), path('finance/', include('finance.urls')), - path("__reload__/", include("django_browser_reload.urls")), ] diff --git a/requirements.txt b/requirements.txt index d3e4ba5..c8a11c6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,5 @@ django +dotenv +dj_database_url +gunicorn +config \ No newline at end of file diff --git a/startup.sh b/startup.sh new file mode 100644 index 0000000..ee4a015 --- /dev/null +++ b/startup.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +# Ensure volume is mounted +mkdir -p /data + +# Run migrations +python manage.py migrate + +# Enable WAL mode for better durability +sqlite3 /data/db.sqlite3 'PRAGMA journal_mode=WAL;' +sqlite3 /data/db.sqlite3 'PRAGMA synchronous=1;' + +# Start Gunicorn server +gunicorn --bind :8000 --workers 2 main.wsgi -- GitLab