-
Notifications
You must be signed in to change notification settings - Fork 2
/
Handler.py
78 lines (71 loc) · 2.98 KB
/
Handler.py
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
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
from threading import Thread
import uuid
import Channel
import cloudCredentials
from redis_db import redisDB
class RequestHandler(BaseHTTPRequestHandler):
def __init__(self, *args, storage, **kwargs):
self.channel = Channel.Channel()
self.storage = storage
super().__init__(*args, **kwargs)
def do_POST(self):
print("Received a POST request")
content_length = int(self.headers['Content-Length'])
if 'X-Forwarded-For' in self.headers:
client_address = self.headers['X-Forwarded-For']
else:
client_address = self.client_address[0]
post_data = self.rfile.read(content_length)
request_data = json.loads(post_data)
task_id = str(uuid.uuid4())
service_num = request_data['service_num']
encoded_image_as_str = request_data['image']
try:
self.storage.upload_image(encoded_image_as_str, task_id)
except Exception as e:
print(f"Failed to upload image: {e}")
self.storage.upload_image(encoded_image_as_str, task_id)
message = {'client_address': client_address, 'task_id': task_id, 'service_num': service_num}
test_r = None
# ttl = None
try:
redisDB.update_image_status(task_id, {"status": 'Received But Not Processed Yet',
"link": 'None'})
test_r = redisDB.pull(task_id)
print("RECEIVED STATUS TEST : ", test_r)
# # Check TTL of a key
# ttl = redisDB.redis_client.ttl(task_id)
# print("TTL of 'TASK ID is':", ttl)
except Exception as e:
print(f"Failed to update status in redis: {e}")
try:
self.channel.publish('requests', json.dumps(message))
except Exception as e:
print(f"Failed to send request to workers: {e}")
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(task_id).encode())
def do_GET(self):
response = json.dumps({'hello': 'world', 'received': 'ok'})
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(response.encode('utf-8'))
class Node:
def __init__(self, server_address, storage):
self.server_address = server_address
self.storage = storage
def run(self):
httpd = HTTPServer(self.server_address, lambda *args, **kwargs: RequestHandler(*args, storage=self.storage, **kwargs))
print(f"Starting server on {self.server_address}")
httpd.serve_forever()
if __name__ == "__main__":
storage = cloudCredentials.Storage()
node = Node(('0.0.0.0', 8000), storage)
# Starting a new thread for handling requests
# request_thread = Thread(target=node.run)
# request_thread.start()
node.run()