From da83e1e9569aff122ffbc963ea384e4e108718a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=B6rkem?= Date: Thu, 29 Jan 2026 11:02:44 -0800 Subject: [PATCH] initialize repo --- .DS_Store | Bin 0 -> 6148 bytes Dockerfile | 9 ++++ README.md | 38 ++++++++++++++++- app.py | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 .DS_Store create mode 100644 Dockerfile create mode 100644 app.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 # if a frontend url is not provided the app will default the allow all utl's which is not recommended! + - NTFY_TOPIC= # for psuh notification support (optional) +``` + +## Dependencies +## Configuration \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..61723f4 --- /dev/null +++ b/app.py @@ -0,0 +1,118 @@ +import os +import json +import time +import requests +from datetime import datetime +from flask import Flask, request, jsonify +from flask_cors import CORS + +app = Flask(__name__) + +# configure daily limit and data directory. +DAILY_LIMIT = 50 +DATA_DIR = 'guestbook' + +currentDate = datetime.now().strftime('%Y-%m-%d') +submissionCountDay = 0 + + +frontend_url = os.environ.get("FRONTEND_URL","*") + +topic = os.environ.get("NTFY_TOPIC") + +CORS(app, resources={r"/*": {"origins": frontend_url}}) + +if not os.path.exists(DATA_DIR): + os.makedirs(DATA_DIR) + +@app.route('/comments', methods=['GET']) +def getComments(): + comments = [] + try: + files = sorted([f for f in os.listdir(DATA_DIR) if f.endswith('.json')], reverse=True) + + for filename in files: + filepath = os.path.join(DATA_DIR, filename) + with open(filepath, 'r', encoding='utf-8') as f: + try: + data = json.load(f) + comments.append(data) + except json.JSONDecodeError: + continue + return jsonify(comments) + except Exception as e: + return jsonify({"error": str(e)}), 500 + +@app.route('/comments', methods=['POST']) +def addComment(): + global currentDate, submissionCountDay + + # Check date + today_str = datetime.now().strftime('%Y-%m-%d') + if today_str != currentDate: + currentDate = today_str + submissionCountDay = 0 + + # Check limit + if submissionCountDay >= DAILY_LIMIT: + return jsonify({"error": "Guestbook full for the day, try tomorrow!"}), 403 + + data = request.json + name = data.get('name', '').strip() + message = data.get('message', '').strip() + website = data.get('website', '').strip() + + if not name or not message: + return jsonify({"error": "Missing fields"}), 400 + + #URL cleanup + if website: + # If forgot http://, add it for them + if not website.startswith(('http://', 'https://')): + website = 'https://' + website + + entry = { + 'name': name, + 'message': message, + 'website': website, + 'date': time.strftime("%d-%m-%Y %H:%M") + } + + now = datetime.now() + readable_time = now.strftime('%Y-%m-%d_%H-%M-%S') + + + + filename = f"{readable_time}.json" + filepath = os.path.join(DATA_DIR, filename) + + try: + with open(filepath, 'x', encoding='utf-8') as f: + json.dump(entry, f) + except FileExistsError: + filename = f"{readable_time}_2.json" + filepath = os.path.join(DATA_DIR, filename) + with open(filepath, 'x', encoding='utf-8') as f: + json.dump(entry, f) + + + send_ntfy_notification(name, message) + + + submissionCountDay += 1 + + return jsonify({"status": "success"}) + +def send_ntfy_notification(name, message): + if not topic: + return + try: + requests.post(f"https://ntfy.sh/{topic}", + data=f"{name} wrote: {message}", + headers={ + "Title": "Someone Signed Your Guestbook!" + }) + except Exception as e: + print(f"Notification failed: {e}") +if __name__ == '__main__': + app.run(host='0.0.0.0', port=5000) \ No newline at end of file