| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import json
- import os
- import random
- from flask import Flask, jsonify, render_template, request
- BASE_DIR = os.path.dirname(os.path.abspath(__file__))
- SCHEMA_PATH = os.path.normpath(os.path.join(BASE_DIR, 'schema.json'))
- print(SCHEMA_PATH)
- app = Flask(__name__)
- @app.route("/")
- def index():
- return render_template("home.html")
- @app.route("/health")
- def health():
- return "Health check passed!"
- def load_questions():
- with open(SCHEMA_PATH, 'r', encoding='utf-8') as file:
- return json.load(file)
- @app.route('/api/questions', methods=['GET'])
- def get_questions():
- count_param = request.args.get('count', default=3)
- try:
- count = int(count_param)
- except ValueError:
- return jsonify({"error": "Parametr 'count' musí být platné číslo."}), 400
- if count < 0:
- return jsonify({"error": "Počet otázek nemůže být záporný."}), 400
- try:
- all_questions = load_questions()
- except Exception as e:
- print(e)
- return jsonify({"error": "Soubor schema.json nebyl nalezen nebo je prázdný."}), 500
- if count > len(all_questions):
- count = len(all_questions)
- random_questions = random.sample(all_questions, count)
- return random_questions
- if __name__ == "__main__":
- app.run(port=5000)
|