Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import json
|
| 3 |
-
import configparser
|
| 4 |
-
from flask import Flask, jsonify, request
|
| 5 |
-
from flask_cors import CORS
|
| 6 |
-
from waitress import serve
|
| 7 |
-
import asyncio
|
| 8 |
-
|
| 9 |
-
# 1. Config laden
|
| 10 |
-
config = configparser.ConfigParser()
|
| 11 |
-
config.read('.codey-lab')
|
| 12 |
-
|
| 13 |
-
TRESOR_DIR = config['TRESOR']['STORAGE_DIR']
|
| 14 |
-
FILE_EXT = config['TRESOR']['FILE_SYNTAX']
|
| 15 |
-
|
| 16 |
-
app = Flask(__name__)
|
| 17 |
-
# CORS Sicherheit: Nur Anfragen von deiner GitHub-URL erlauben
|
| 18 |
-
CORS(app, resources={r"/api/*": {"origins": "https://codey-lab.github.io"}})
|
| 19 |
-
|
| 20 |
-
async def read_jsonl_async(filepath):
|
| 21 |
-
"""Liest die Datei asynchron, um den Server nicht zu blockieren."""
|
| 22 |
-
if not os.path.exists(filepath):
|
| 23 |
-
return []
|
| 24 |
-
# In einem echten asynchronen Kontext würde man hier aiofiles nutzen
|
| 25 |
-
with open(filepath, 'r', encoding='utf-8') as f:
|
| 26 |
-
return [json.loads(line) for line in f if line.strip()]
|
| 27 |
-
|
| 28 |
-
@app.route('/api/v1/ledger/<int:year>/<hex_id>', methods=['GET'])
|
| 29 |
-
async def get_user_data(year, hex_id):
|
| 30 |
-
db_path = f"{TRESOR_DIR}/{year}{FILE_EXT}.jsonl"
|
| 31 |
-
hex_id = hex_id.upper()
|
| 32 |
-
|
| 33 |
-
try:
|
| 34 |
-
data = await read_jsonl_async(db_path)
|
| 35 |
-
# Wir filtern die Daten: Nur den letzten Stand für die ID
|
| 36 |
-
user_history = [d for d in data if d.get('h_uid') == hex_id]
|
| 37 |
-
|
| 38 |
-
if not user_logs:
|
| 39 |
-
return jsonify({"status": "error", "msg": "UID_NOT_FOUND"}), 404
|
| 40 |
-
|
| 41 |
-
return jsonify({
|
| 42 |
-
"status": "success",
|
| 43 |
-
"data": user_history[-1], # Aktuellster Stand
|
| 44 |
-
"count": len(user_history) # Wie oft hat der User gesynct?
|
| 45 |
-
})
|
| 46 |
-
except Exception as e:
|
| 47 |
-
return jsonify({"status": "error", "msg": str(e)}), 500
|
| 48 |
-
|
| 49 |
-
if __name__ == "__main__":
|
| 50 |
-
# Waitress als stabiler Worker
|
| 51 |
-
print(f"[*] API ONLINE // Listening for GitHub Pages requests...")
|
| 52 |
-
serve(app, host='0.0.0.0', port=7860, threads=8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|