-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask_app.py
More file actions
149 lines (124 loc) · 4.33 KB
/
Copy pathflask_app.py
File metadata and controls
149 lines (124 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from flask import Flask, request, jsonify
import grpc
import user_pb2
import user_pb2_grpc
import time
import traceback
app = Flask(__name__)
time.sleep(5)
# Configurazione del canale gRPC
grpc_channel = grpc.insecure_channel('server:50051')
stub = user_pb2_grpc.UserServiceStub(grpc_channel)
# Endpoint per registrare un nuovo utente
@app.route('/register', methods=['POST'])
def register_user():
try:
# Ottieni i dati JSON dalla richiesta HTTP
data = request.json
email = data['email']
ticker = data['ticker']
high_value = data['high_value']
low_value = data['low_value']
# Effettua la chiamata gRPC
response = stub.RegisterUser(user_pb2.RegisterUserRequest(
email=email,
ticker=ticker,
high_value=high_value,
low_value=low_value
))
# Restituisci la risposta come JSON
return jsonify({
'success': response.success,
'message': response.message
}), 200 if response.success else 400
except Exception as e:
print("[ERROR] Si è verificato un errore nel server Flask:")
print(traceback.format_exc())
return jsonify({'error': str(e)}), 500
# Endpoint per aggiornare il ticker di un utente
@app.route('/update-ticker', methods=['PUT'])
def update_user_ticker():
try:
data = request.json
email = data['email']
new_ticker = data['ticker']
response = stub.UpdateUserTicker(user_pb2.UserRequest(
email=email,
ticker=new_ticker
))
return jsonify({
'success': response.success,
'message': response.message
}), 200 if response.success else 400
except Exception as e:
return jsonify({'error': str(e)}), 500
# Endpoint per cancellare un utente
@app.route('/delete', methods=['DELETE'])
def delete_user():
try:
email = request.json['email']
response = stub.DeleteUser(user_pb2.DeleteUserRequest(
email=email
))
return jsonify({
'success': response.success,
'message': response.message
}), 200 if response.success else 400
except Exception as e:
return jsonify({'error': str(e)}), 500
# Endpoint per recuperare l'ultimo valore del ticker
@app.route('/get-latest-value', methods=['GET'])
def get_latest_value():
try:
email = request.args.get('email')
# Effettua la chiamata gRPC
response = stub.GetLatestValue(user_pb2.UserRequest(
email=email
))
# Restituisci la risposta come JSON
if response.success:
return jsonify({
'success': True,
'message': response.message,
'value': response.value,
'timestamp': response.timestamp
}), 200
else:
return jsonify({
'success': False,
'message': response.message
}), 400
except Exception as e:
print("[ERROR] Si è verificato un errore nel server Flask:")
print(traceback.format_exc())
return jsonify({'error': str(e)}), 500
# Endpoint per calcolare la media degli ultimi X valori
@app.route('/calculate-average', methods=['GET'])
def calculate_average():
try:
email = request.args.get('email')
count = int(request.args.get('count'))
# Effettua la chiamata gRPC
response = stub.CalculateAverage(user_pb2.AverageRequest(
email=email,
count=count
))
# Restituisci la risposta come JSON
if response.success:
return jsonify({
'success': True,
'message': response.message,
'average': response.average
}), 200
else:
return jsonify({
'success': False,
'message': response.message
}), 400
except Exception as e:
print("[ERROR] Si è verificato un errore nel server Flask:")
print(traceback.format_exc())
return jsonify({'error': str(e)}), 500
# Avvia il server Flask
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)