From: Jakub Czajka Date: Sun, 11 Feb 2024 12:39:35 +0000 (+0100) Subject: Add an endpoint for reading the number of views for a page. X-Git-Url: https://git.ekhem.eu.org/?a=commitdiff_plain;h=448592a21acc6473cccf2d5b13b6b46411c89ace;p=website.git Add an endpoint for reading the number of views for a page. --- diff --git a/create.sql b/create.sql index 59d7312..c29b390 100644 --- a/create.sql +++ b/create.sql @@ -22,3 +22,20 @@ BEGIN TO rsyslog', current_database()); END IF; END$$; + +DO $$ +BEGIN + IF NOT EXISTS (SELECT * FROM pg_user WHERE usename = 'analytics_reader') + THEN + CREATE ROLE analytics_reader LOGIN; + + GRANT SELECT + ON access_logs + TO analytics_reader; + + /* Execute for the current database. */ + EXECUTE FORMAT('GRANT CONNECT + ON DATABASE %I + TO analytics_reader', current_database()); + END IF; +END$$; diff --git a/drop.sql b/drop.sql index 55adc27..7a44707 100644 --- a/drop.sql +++ b/drop.sql @@ -1,6 +1,23 @@ /* Copyright (c) 2024 Jakub Czajka License: GPL-3.0 or later. */ +DO $$ +BEGIN + IF EXISTS (SELECT * FROM pg_user WHERE usename = 'analytics_reader') + THEN + REVOKE SELECT + ON access_logs + FROM analytics_reader; + + EXECUTE + FORMAT('REVOKE CONNECT + ON DATABASE %I + FROM analytics_reader;', current_database()); + + DROP ROLE analytics_reader; + END IF; +END$$; + DO $$ BEGIN IF EXISTS (SELECT * FROM pg_user WHERE usename = 'rsyslog') diff --git a/views.sh b/views.sh new file mode 100755 index 0000000..915493e --- /dev/null +++ b/views.sh @@ -0,0 +1,17 @@ +#!/bin/sh +# Copyright (c) 2024 Jakub Czajka +# License: GPL-3.0 or later. + +. /etc/environment + +_querypath="/${FILENAME}" + +echo "HTTP/1.1 200 OK" +echo "Content-Type: text/html" +echo "" + +/usr/bin/psql --user=analytics_reader --dbname="${analytics_db}" --tuples-only \ + --no-align --command="\ +SELECT COUNT(*) +FROM access_logs +WHERE access_log::json->>'path' = '${_querypath}'" diff --git a/website.conf b/website.conf index 6858ad5..393297d 100644 --- a/website.conf +++ b/website.conf @@ -28,6 +28,15 @@ server { userid_expires 365d; access_log ${website_log_file} as_json; + root ${prod_dir}/www; + + location ~ /views/(.*)$ { + include fastcgi_params; + fastcgi_pass unix:/var/run/fcgiwrap.socket; + + fastcgi_param URL ${dollar}uri; + fastcgi_param SCRIPT_FILENAME ${dollar}document_root/views.sh; + } location /cv { root ${prod_dir}/cv; @@ -36,7 +45,6 @@ server { } location / { - root ${prod_dir}/www; index index.html; } }