Add a generic dev uwsgi script and update readme
authorJelte Fennema-Nio <[email protected]>
Tue, 24 Sep 2024 20:27:01 +0000 (22:27 +0200)
committerMagnus Hagander <[email protected]>
Tue, 24 Sep 2024 20:27:01 +0000 (22:27 +0200)
.gitignore
README.md
dev_requirements.txt [new file with mode: 0644]
pgcommitfest/local_settings_example.py [new file with mode: 0644]
run_dev.py [new file with mode: 0755]
uwsgi_dev.ini [new file with mode: 0644]

index 0d20b6487c61e7d1bde93acf4a14b7a89083a16d..d0143b0964df4134d4ef6581bec7310afb4d3dcf 100644 (file)
@@ -1 +1,2 @@
 *.pyc
+/env/
index 7dd48bc0673e9ff39be4645083dba4f3c5eb4b2a..61537749fe44f8df57f6d9a5add5d0769c774eee 100644 (file)
--- a/README.md
+++ b/README.md
@@ -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 (file)
index 0000000..3b878a3
--- /dev/null
@@ -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 (file)
index 0000000..51740ff
--- /dev/null
@@ -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 (executable)
index 0000000..6c8189d
--- /dev/null
@@ -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 (file)
index 0000000..b1e52d8
--- /dev/null
@@ -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