From 41df76eed0fa400e21cfbc4c1009e093efae993a Mon Sep 17 00:00:00 2001
From: Jonas L <jooola@users.noreply.github.com>
Date: Mon, 25 Apr 2022 16:09:19 +0200
Subject: [PATCH] feat(api): replace uwsgi with gunicorn (#1785)

Related to #1719

BREAKING: The uwsgi packages and configuration files (`/etc/airtime/libretime-api.ini`) should be removed from the system.
---
 api/install/systemd/libretime-api.service | 11 ++++++++---
 api/install/uwsgi/libretime-api.ini       | 10 ----------
 api/packages.ini                          |  6 ++----
 docs/releases/unreleased.md               | 14 ++++++++++++++
 install                                   |  4 ----
 5 files changed, 24 insertions(+), 21 deletions(-)
 delete mode 100644 api/install/uwsgi/libretime-api.ini

diff --git a/api/install/systemd/libretime-api.service b/api/install/systemd/libretime-api.service
index e036aa985..863bfd1d9 100644
--- a/api/install/systemd/libretime-api.service
+++ b/api/install/systemd/libretime-api.service
@@ -3,13 +3,18 @@ Description=LibreTime API Service
 
 [Service]
 Type=notify
-NotifyAccess=all
-KillSignal=SIGQUIT
+KillMode=mixed
+PrivateTmp=true
 
 Environment=LIBRETIME_LOG_FILEPATH=/var/log/libretime/api.log
 Environment=LIBRETIME_CONFIG_FILEPATH=/etc/airtime/airtime.conf
 
-ExecStart=/usr/bin/uwsgi /etc/airtime/libretime-api.ini
+ExecStart=/usr/bin/gunicorn \
+        --workers 4 \
+        --log-file - \
+        --bind 127.0.0.1:8081 \
+        libretime_api.wsgi
+ExecReload=/bin/kill -s HUP $MAINPID
 User=libretime-api
 Group=libretime-api
 Restart=always
diff --git a/api/install/uwsgi/libretime-api.ini b/api/install/uwsgi/libretime-api.ini
deleted file mode 100644
index 24d0b4f0b..000000000
--- a/api/install/uwsgi/libretime-api.ini
+++ /dev/null
@@ -1,10 +0,0 @@
-[uwsgi]
-module = libretime_api.wsgi
-
-master = true
-plugin = python3
-processes = 10
-vacuum = true
-http-socket = :8081
-uid = WEB_USER
-gid = WEB_USER
diff --git a/api/packages.ini b/api/packages.ini
index e9489c7ee..cca5bd60e 100644
--- a/api/packages.ini
+++ b/api/packages.ini
@@ -2,10 +2,8 @@
 [python]
 python3 = buster, bullseye, bionic, focal
 python3-pip = buster, bullseye, bionic, focal
-
-[django]
-uwsgi = buster, bullseye, bionic, focal
-uwsgi-plugin-python3 = buster, bullseye, bionic, focal
+gunicorn = buster, bullseye, bionic, focal
+python3-gunicorn = buster, bullseye, bionic, focal
 
 [psycopg2]
 # See https://www.psycopg.org/docs/install.html#install-from-source
diff --git a/docs/releases/unreleased.md b/docs/releases/unreleased.md
index a53773a8b..a1b6acd87 100644
--- a/docs/releases/unreleased.md
+++ b/docs/releases/unreleased.md
@@ -32,6 +32,20 @@ sudo rm -f /etc/httpd/conf.d/airtime*
 sudo rm -f /etc/php.d/airtime.ini
 ```
 
+### Replaced uWSGI with Gunicorn
+
+[uWSGI](https://uwsgi-docs.readthedocs.io) was replaced by [Gunicorn](https://gunicorn.org/), the packages and configuration file should be removed from the system:
+
+```bash
+# Remove the configuration file
+sudo rm -f /etc/airtime/libretime-api.ini
+
+# Remove the packages
+sudo apt purge \
+    uwsgi \
+    uwsgi-plugin-python3
+```
+
 ## :warning: Known issues
 
 The following issues may need a workaround for the time being. Please search the [issues](https://github.com/libretime/libretime/issues) before reporting problems not listed below.
diff --git a/install b/install
index 66d6da29c..499683db3 100755
--- a/install
+++ b/install
@@ -1046,10 +1046,6 @@ verbose "...Done"
 verbose "\n * Installing API..."
 loudCmd "$pip_install ${AIRTIMEROOT}/api[prod]"
 systemInitInstall libretime-api "$web_user"
-mkdir -p /etc/airtime
-sed -e "s@WEB_USER@${web_user}@g" \
-  -e "s@WEB_ROOT@${web_root}@g" \
-  "${AIRTIMEROOT}/api/install/uwsgi/libretime-api.ini" > /etc/airtime/libretime-api.ini
 verbose "...Done"
 
 verbose "\n * Setting permissions on /var/log/libretime..."