diff options
author | Jelte Fennema-Nio | 2024-09-24 20:27:01 +0000 |
---|---|---|
committer | Magnus Hagander | 2024-09-24 20:27:01 +0000 |
commit | f8fd32193560e0f4053873fe13df820b9dd79ff4 (patch) | |
tree | daecbe1cce3782faca32682d9f4fb0f80a920416 | |
parent | 432a7cafe83e1ebe8ac1311d8ef0409ac6776a12 (diff) |
Add a generic dev uwsgi script and update readme
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README.md | 72 | ||||
-rw-r--r-- | dev_requirements.txt | 2 | ||||
-rw-r--r-- | pgcommitfest/local_settings_example.py | 19 | ||||
-rwxr-xr-x | run_dev.py | 34 | ||||
-rw-r--r-- | uwsgi_dev.ini | 10 |
6 files changed, 89 insertions, 49 deletions
@@ -1 +1,2 @@ *.pyc +/env/ @@ -14,74 +14,48 @@ This is a Django 3.2 application backed by PostgreSQL and running on Python 3.x. First, prepare your development environment by installing pip, virtualenv, and postgresql-server-dev-X.Y. -``` -$ sudo apt install python-pip postgresql-server-dev-14 - -$ pip install virtualenv +```bash +sudo apt install python-pip postgresql-server-dev-14 ``` Next, configure your local environment with virtualenv and install local dependencies. +```bash +python3 -m venv env +source env/bin/activate +pip install -r dev_requirements.txt ``` -$ virtualenv env -$ source env/bin/activate -$ pip install -r requirements.txt -``` - -Now prepare the application to run locally. -Configure the app to match your local installation by creating a -`local_settings.py` with the following content in the `pgcommitfest` directory. -Change the values for the database connection adequately. +Create a database for the application: -``` -# Enable more debugging information -DEBUG = True -# Prevent logging to try to send emails to postgresql.org admins. -# Use the default Django logging settings instead. -LOGGING = None - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql_psycopg2', - 'NAME': 'pgcommitfest', - 'USER': 'postgres', - 'PASSWORD': 'postgres', - 'HOST': '0.0.0.0', - } -} - -# Disables the PostgreSQL.ORG authentication. -# Use the default built-in Django authentication module. -AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend'] +```bash +createdb pgcommitfest ``` -Provided that you created a database matching the above settings, you can -now create the required tables. Note that a password must be provided. +Create a local settings file (feel free to edit it): -``` -$ python manage.py migrate +```bash +cp pgcommitfest/local_settings_example.py pgcommitfest/local_settings.py ``` -You'll need either a database dump of the actual server's data or else to create a superuser: +Now you can now create the required tables. Note that a password might need to +be provided. -``` -$ python manage.py createsuperuser +```bash +./manage.py migrate ``` -Finally, you're ready to start the application: +You'll need either a database dump of the actual server's data or else to create a superuser: -``` -$ python manage.py runserver +```bash +./manage.py createsuperuser ``` -To authenticate you'll first have to remove the customized login template. -Remember not to commit this modification. +Finally, you're ready to start the application: -``` -$ find . -type f -name login.html -$ rm -f global_templates/admin/login.html +```bash +./run_dev.py ``` -Then open http://localhost:8000/admin to log in. Once redirected to the Django +Then open http://localhost:8007/admin to log in. Once redirected to the Django admin interface, go back to the main interface. You're now logged in. diff --git a/dev_requirements.txt b/dev_requirements.txt new file mode 100644 index 0000000..3b878a3 --- /dev/null +++ b/dev_requirements.txt @@ -0,0 +1,2 @@ +-r requirements.txt +uwsgi diff --git a/pgcommitfest/local_settings_example.py b/pgcommitfest/local_settings_example.py new file mode 100644 index 0000000..51740ff --- /dev/null +++ b/pgcommitfest/local_settings_example.py @@ -0,0 +1,19 @@ +# Enable more debugging information +DEBUG = True +# Prevent logging to try to send emails to postgresql.org admins. +# Use the default Django logging settings instead. +LOGGING = None + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'pgcommitfest', + 'USER': 'postgres', + 'PASSWORD': 'postgres', + 'HOST': '0.0.0.0', + } +} + +# Disables the PostgreSQL.ORG authentication. +# Use the default built-in Django authentication module. +AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend'] diff --git a/run_dev.py b/run_dev.py new file mode 100755 index 0000000..6c8189d --- /dev/null +++ b/run_dev.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +"""Run uWSGI with Django static files mapping. + +The reason we don't hardcode the path to the static admin directory in the +uwsgi_dev.ini file is because the path contains the python version, something +like: + +env/lib/python3.12/site-packages/django/... + +Requiring everyone to use the same python version is not practical, so instead +we have this tiny script that will find the path to the Django admin static +files and run uWSGI with the correct path. +""" +from importlib.machinery import PathFinder +import subprocess +import sys + +django_path = PathFinder().find_spec("django").submodule_search_locations[0] + +django_admin_path = django_path + "/contrib/admin/static/admin" + +if len(sys.argv) > 1: + ini_file = sys.argv[1] +else: + ini_file = "uwsgi_dev.ini" + +subprocess.run( + [ + "uwsgi", + "--static-map", + f"/media/admin={django_path}/contrib/admin/static/admin", + ini_file, + ] +) diff --git a/uwsgi_dev.ini b/uwsgi_dev.ini new file mode 100644 index 0000000..b1e52d8 --- /dev/null +++ b/uwsgi_dev.ini @@ -0,0 +1,10 @@ +[uwsgi] +threads=1 +env=DJANGO_SETTINGS_MODULE=pgcommitfest.settings +module=pgcommitfest.wsgi:application +py-autoreload=1 +touch-reload = pgcommitfest/local_settings.py +touch-reload = pgcommitfest/settings.py +touch-reload = uwsgi_dev.ini +http=127.0.0.1:8007 +static-map=/media=media |