From e4946846989977996ce523c5a1dc38a605cd2998 Mon Sep 17 00:00:00 2001
From: Leonidas Spyropoulos <artafinde@archlinux.org>
Date: Sat, 15 Oct 2022 14:19:40 +0300
Subject: [PATCH] aurweb: setup goaurrpc

Expose aurweb RPC using goaurrpc to reduce the load on the server.
Additionally we can now geo-serve this ro reduce load and bandwidth.

Signed-off-by: Leonidas Spyropoulos <artafinde@archlinux.org>
---
 roles/aurweb/tasks/main.yml             |  5 +++++
 roles/aurweb/templates/goaurrpc.conf.j2 | 20 ++++++++++++++++++++
 roles/aurweb/templates/nginx.d.conf.j2  |  6 ++++++
 3 files changed, 31 insertions(+)
 create mode 100644 roles/aurweb/templates/goaurrpc.conf.j2

diff --git a/roles/aurweb/tasks/main.yml b/roles/aurweb/tasks/main.yml
index 300cdb1f0..588c83d02 100644
--- a/roles/aurweb/tasks/main.yml
+++ b/roles/aurweb/tasks/main.yml
@@ -10,6 +10,7 @@
       - python-poetry
       - gcc
       - pkg-config
+      - goaurrpc
 
 - name: Install the cgit package
   pacman:
@@ -132,6 +133,9 @@
 - name: Copy aurweb configuration file
   copy: src={{ aurweb_dir }}/conf/config.defaults dest={{ aurweb_conf_dir }}/config.defaults remote_src=yes owner=root group=root mode=0644
 
+- name: Install goaurrpc configuration
+  template: src=goaurrpc.conf.j2 dest=/etc/goaurrpc.conf owner=root group=root mode=0644
+
 # Note: initdb needs the config
 - name: Install custom aurweb configuration
   template: src=config.j2 dest={{ aurweb_conf_dir }}/config owner=root group=root mode=0644
@@ -321,5 +325,6 @@
     - aurweb-tuvotereminder.timer
     - aurweb-usermaint.timer
     - aurweb.service
+    - goaurrpc.service
     - aurweb-github-mirror.timer
   when: release.changed
diff --git a/roles/aurweb/templates/goaurrpc.conf.j2 b/roles/aurweb/templates/goaurrpc.conf.j2
new file mode 100644
index 000000000..a36e5f951
--- /dev/null
+++ b/roles/aurweb/templates/goaurrpc.conf.j2
@@ -0,0 +1,20 @@
+{
+	"Port": 10666,
+	"AurFileLocation": "{{ aurweb_dir }}/web/html/packages-meta-ext-v1.json.gz",
+	"MaxResults": 5000,
+	"RefreshInterval": 150,
+	"RateLimit": 4000,
+	"LoadFromFile": true,
+	"RateLimitCleanupInterval": 600,
+	"RateLimitTimeWindow": 86400,
+	"TrustedReverseProxies": [
+		"127.0.0.1",
+		"::1"
+	],
+	"EnableSSL": false,
+	"CertFile": "",
+	"KeyFile": "",
+	"EnableSearchCache": true,
+	"CacheCleanupInterval": 60,
+	"CacheExpirationTime": 180
+}
diff --git a/roles/aurweb/templates/nginx.d.conf.j2 b/roles/aurweb/templates/nginx.d.conf.j2
index 6369d5db1..0ddfdea2a 100644
--- a/roles/aurweb/templates/nginx.d.conf.j2
+++ b/roles/aurweb/templates/nginx.d.conf.j2
@@ -112,6 +112,12 @@ server {
         add_header Cache-Control "public, must-revalidate, proxy-revalidate";
     }
 
+    location /rpc {
+        rewrite ^/rpc.php /rpc last;
+        proxy_pass http://127.0.0.1:10666/rpc;
+        proxy_set_header X-Forwarded-For $remote_addr;
+    }
+
     location / {
         # Proxy over to aurweb's ASGI application.
         proxy_pass http://{{ aurweb_asgi_bind }};
-- 
GitLab