Fast-api webhook consumer for testing purposes only
2025-10-13 19:20:21 +02:00
logs first commit 2025-10-13 17:23:07 +02:00
.gitignore first commit 2025-10-13 17:23:07 +02:00
dotenv.example first commit 2025-10-13 17:23:07 +02:00
gunicorn.sh.example changes gunicorn socket location 2025-10-13 19:20:21 +02:00
LICENSE first commit 2025-10-13 17:23:07 +02:00
main.py removes unused import 2025-10-13 17:37:05 +02:00
README.md changes gunicorn socket location 2025-10-13 19:20:21 +02:00

Webhook consumer

This is an example consumer ONLY.

It is built to test webhook POSTs made by a LiberaForms server.

Useful for development or as a starting place to build upon.

This fast-api script does this:

  • Listens for POST requests on http(s)://yourdomain.com/consume
  • Checks for a valid X-Signature-256
  • Emails you with the request's body

Please read main.py

Install

Create a user if you need to.

sudo adduser --disabled-password yourusername

Create a directory.

mkdir /some/dir/consumer
chown yourusername.yourusername /some/dir/consumer
su - yourusername

Download the code.

git clone https://codeberg.org/LiberaForms/webhook-consumer.git /some/dir/consumer
cd /some/dir/consumer
python3 -m venv ./venv
source venv/bin/activate
pip install --upgrade pip
pip install "fastapi[standard]"
pip install gunicorn

Create .env and edit

cp dotenv.example .env

And run

fastapi dev main.py

Use the localhost url as a consumer endpoint for testing the LiberaForms app.

Internet installation

If you would like to set this up on a server on the internet.

su - yourusername
cd /some/dir/consumer
source venv/bin/activate
pip install gunicorn

Create gunicorn.sh and edit

cp gunicorn.sh.example gunicorn.sh
chmod +x gunicorn.sh

Nginx config

An example. Please adjust

upstream consumer_app_server {
    server unix:/some/dir/consumer/gunicorn.sock fail_timeout=0;
}

server {
    listen         80;
    server_name    test-consumer.localdomain;
    return         301 https://$server_name$request_uri;
}
server {
    listen 443 ssl;
    server_name test-consumer.localdomain;
    add_header Referrer-Policy "origin-when-cross-origin";
    add_header X-Content-Type-Options nosniff;

    ssl_certificate           /etc/ssl/certs/nginx-selfsigned.crt;
    ssl_certificate_key       /etc/ssl/private/nginx-selfsigned.key;
    #ssl_dhparam               /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass          http://consumer_app_server;
        proxy_set_header    Host    $host;
        proxy_set_header    X-Forwarded-For $remote_addr;
        proxy_set_header    X-Real-IP   $remote_addr;
        proxy_pass_header   server;
    }
    access_log /var/log/nginx/test-consumer.localdomain.access.log;
    error_log /var/log/nginx/test-consumer.localdomain.error.log notice;
}

Setup supervisord

Create /etc/supervisor/conf.d/test_consumer.conf with this content

[program:test_consumer]
command=/some/dir/consumer/gunicorn.sh
user=yourusername
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stdout_logfile=/some/dir/consumer/logs/gunicorn-errors.log
supervisorctl update
supervisorctl restart test_consumer