1
0
mirror of https://github.com/kmein/niveum synced 2026-03-21 12:21:08 +01:00

tarot: generalize

This commit is contained in:
2025-12-20 11:31:12 +01:00
parent dd75268d60
commit a0f7867a25
3 changed files with 36 additions and 33 deletions

View File

@@ -0,0 +1,67 @@
{
config,
pkgs,
lib,
...
}:
let
tarotPort = 7407;
tarotFiles = pkgs.fetchzip {
url = "https://c.krebsco.de/tarot.zip";
sha256 = "0jl5vdwlj17pqp94yj02xgsb1gyvs9i08m83kac0jdnhfjl2f75a";
stripRoot = false;
};
tarotKey = builtins.fetchurl {
url = "http://c.krebsco.de/tarot.pdf";
sha256 = "1n2m53kjg2vj9dbr70b9jrsbqwdfrcb48l4wswn21549fi24g6dx";
};
in
{
systemd.services.tarot = {
enable = true;
serviceConfig.Type = "simple";
wantedBy = [ "multi-user.target" ];
environment = {
TAROT_FILES = tarotFiles;
TAROT_PORT = toString tarotPort;
};
serviceConfig.ExecStart = pkgs.writers.writePython3 "tarot-server" {
libraries = py: [
py.pillow
py.flask
];
} ./tarot.py;
};
niveum.passport.services = [
rec {
link = "https://tarot.kmein.de";
title = "Tarot";
description = "draws Tarot cards for you. See <a href=\"${link}/files/key.pdf\">here</a> for information on how to interpret them.";
}
];
services.nginx.virtualHosts."tarot.kmein.de" = {
enableACME = true;
forceSSL = true;
locations = {
"/".proxyPass = "http://127.0.0.1:${toString tarotPort}/";
"/files/" = {
root = pkgs.linkFarm "tarot" [
{
name = "files/key.pdf";
path = tarotKey;
}
{
name = "files/cards";
path = tarotFiles;
}
];
extraConfig = ''
autoindex on;
charset UTF-8;
'';
};
};
};
}

View File

@@ -0,0 +1,26 @@
from flask import Flask, send_file
from pathlib import Path
from random import choice, randint
from io import BytesIO
from PIL import Image
import os
app = Flask(__name__)
TAROT_DIR = Path(os.environ["TAROT_FILES"])
@app.route("/")
def tarot():
card_path = choice(list(TAROT_DIR.glob("*")))
with Image.open(card_path) as img:
if randint(0, 1):
img = img.rotate(180)
buf = BytesIO()
img.save(buf, format="JPEG")
buf.seek(0)
return send_file(buf, mimetype="image/jpeg", as_attachment=False)
if __name__ == "__main__":
app.run(port=int(os.environ["TAROT_PORT"]))