from flask import Flask, jsonify, request from spaceapi import SpaceAPI, Location, Contact, State, LinkedSpace import json from dotenv import load_dotenv import os # Load environment variables from .env file load_dotenv() app = Flask(__name__) # Global variable to store the current state current_state = {} def require_token(f): def decorated(*args, **kwargs): token = request.headers.get('X-SpaceAPI-Token') if not token or token != os.getenv('SPACEAPI_TOKEN'): return jsonify({"error": "Invalid or missing token"}), 401 return f(*args, **kwargs) decorated.__name__ = f.__name__ return decorated @app.route('/') def hello_world(): space = SpaceAPI( api_compatibility=["14", "15"], space="Leitstelle511 - Chaos Computer Club Hannover", logo="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Logo_CCC.svg/330px-Logo_CCC.svg.png", url="http://hannover.ccc.de", location=Location( address="Klaus-Müller-Kilian-Weg 2, 30167 Hannover, Germany", lat=52.3881, lon=9.7181 ), contact=Contact( email="kontakt@hannover.ccc.de", irc="irc://hackint.eu/leitstelle511", ml="511@hannover.ccc.de", matrix="#leitstelle511-public:leitstelle511.net" ), state=current_state, projects=[ "https://hannover.ccc.de/projekte/schule/", "https://hackover.de" ], linked_spaces=[ LinkedSpace( endpoint="https://leinelab.org/api/spaceapi.json", website="https://leinelab.org" ) ] ) return jsonify(space.__dict__) @app.route('/state', methods=['POST']) @require_token def update_state(): global current_state try: data = request.get_json() if not data or 'open' not in data: return jsonify({"error": "Invalid state data. 'open' field is required"}), 400 # Create new state with provided open status # lastchange will be automatically set to current timestamp current_state = State(open=data['open']) return jsonify({ "message": "State updated successfully", "state": current_state.__dict__ }) except Exception as e: return jsonify({"error": str(e)}), 400 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True)