86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
import subprocess
|
|
import json
|
|
import time
|
|
from flask import Flask, request, jsonify, session
|
|
from I2C_Function import analyze_data, read_data
|
|
from flask_cors import CORS
|
|
from flask_session import Session
|
|
|
|
app = Flask(__name__)
|
|
app.config.from_pyfile('config.py')
|
|
Session(app)
|
|
CORS(app)
|
|
|
|
initialized = False
|
|
|
|
@app.route('/trng/randomNum/init', methods=['GET'])
|
|
def initialize_generator():
|
|
#quantity = 1
|
|
#bits = 1
|
|
#startup = True # Boolean-Wert für die Initialisierung
|
|
global initialized # Zugriff auf die globale Variable
|
|
|
|
#result = analyze_data(int(quantity), int(bits), startup=true)
|
|
result = analyze_data(int(1), int(1), startup=True)
|
|
#session['initialized'] = True
|
|
|
|
if result is True:
|
|
initialized = True
|
|
return jsonify({'error': 'successful operation; random number generator is ready and random numbers can be requested'}), 200
|
|
else:
|
|
return jsonify({'error': 'Unable to initialize the random number generator within a timeout of 60 seconds.'}), 555
|
|
|
|
|
|
@app.route('/trng/randomNum/getRandom', methods=['GET'])
|
|
def get_random_numbers():
|
|
global initialized # Zugriff auf die globale Variable
|
|
quantity = request.args.get('quantity')
|
|
bits = request.args.get('numBits')
|
|
|
|
if not quantity.isdigit() or not bits.isdigit():
|
|
return jsonify({'error': 'Invalid input. Quantity and bits must be numeric.'}), 400
|
|
|
|
if int(quantity) <= 0 or int(bits) <= 0:
|
|
return jsonify({'error': 'Invalid input. Quantity and bits must be positive integers.'}), 400
|
|
|
|
# Überprüfe den Initialisierungsstatus
|
|
if not initialized:
|
|
return jsonify({'error': 'system not ready; try init'}), 432
|
|
|
|
#if 'initialized' not in session or not session['initialized']:
|
|
#return jsonify({'error': 'system not ready; try init'}), 432
|
|
|
|
#if 'initialized' not in session or not session['initialized', 'false']:
|
|
#return jsonify({'error': 'system not ready; try init'}), 432
|
|
|
|
filename = analyze_data(int(quantity), int(bits), startup=False)
|
|
|
|
if filename is False:
|
|
return jsonify({'error': 'Unable to generate random numbers.'}), 500
|
|
if filename:
|
|
return filename, 200
|
|
else:
|
|
return jsonify({'error': 'Unable to generate random numbers.'}), 500
|
|
|
|
@app.route('/trng/randomNum/shutdown', methods=['GET'])
|
|
def shutdown_generator():
|
|
global initialized # Zugriff auf die globale Variable
|
|
|
|
initialized = False # Setze den Initialisierungsstatus zurück
|
|
#session['initialized'] = False
|
|
|
|
# Beispielantwort
|
|
return jsonify({'message': 'Generator shutdown successfully.'}), 200
|
|
|
|
if __name__ == '__main__':
|
|
# Pfade zu den SSL-Zertifikat- und Schlüsseldateien
|
|
#cert_path = '/etc/nginx/ssl/server.crt'
|
|
#key_path = '/etc/nginx/ssl/server.key'
|
|
cert_path = '/etc/nginx/ssl/cert-gmtrom.pem'
|
|
key_path = '/etc/nginx/ssl/cert-gmtrom-key.pem'
|
|
|
|
# Starte die Flask-Anwendung mit SSL-Konfiguration
|
|
#app.run(host='172.16.78.57', port=5000, ssl_context=(cert_path, key_path))
|
|
app.run(host='0.0.0.0', ssl_context=(cert_path, key_path))
|
|
#app.run(host='0.0.0.0', ssl_context=adhoc)
|
|
#app.run(ssl_context=(cert_path, key_path)) |