Alibrown commited on
Commit
ec267e9
·
verified ·
1 Parent(s): cb497a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -29
app.py CHANGED
@@ -1,48 +1,52 @@
1
  import os
2
  import json
3
  import configparser
4
- from flask import Flask, jsonify
5
  from flask_cors import CORS
6
  from waitress import serve
 
7
 
8
- # 1. CONFIG PARSER
9
  config = configparser.ConfigParser()
10
  config.read('.codey-lab')
11
 
12
- # Pfade aus Config ziehen
13
  TRESOR_DIR = config['TRESOR']['STORAGE_DIR']
14
  FILE_EXT = config['TRESOR']['FILE_SYNTAX']
15
- ORGA_URL = config['REPOSITORY']['URL']
16
 
17
  app = Flask(__name__)
18
- CORS(app) # Sicherheit für GitHub Pages
 
19
 
20
- def get_latest_entry(hex_id):
21
- # Dynamischer Pfadbau: z.B. authority/2026_database.jsonl
22
- year = "2026" # Kann später per datetime.now().year dynamisch gemacht werden
 
 
 
 
 
 
 
23
  db_path = f"{TRESOR_DIR}/{year}{FILE_EXT}.jsonl"
 
24
 
25
- if not os.path.exists(db_path):
26
- return None
 
 
27
 
28
- with open(db_path, 'r', encoding='utf-8') as f:
29
- # Wir lesen von hinten (effizient für große Files)
30
- lines = f.readlines()
31
- for line in reversed(lines):
32
- data = json.loads(line)
33
- if data.get('h_uid') == hex_id.upper():
34
- return data
35
- return None
36
-
37
- @app.route('/api/auth/<hex_id>')
38
- def auth_gate(hex_id):
39
- result = get_latest_entry(hex_id)
40
- if result:
41
- return jsonify(result)
42
- return jsonify({"error": "UID_NOT_FOUND"}), 404
43
 
44
  if __name__ == "__main__":
45
- print(f"[*] AUTHORITY_SERVER STARTING FOR {ORGA_URL}")
46
- print(f"[*] READING FROM TRESOR: {TRESOR_DIR}")
47
- # Waitress als stabiler Unterbau
48
- serve(app, host='0.0.0.0', port=7860, threads=6)
 
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)