forked from alexwafula/Customizable_Load_Balancerr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
93 lines (75 loc) · 2.3 KB
/
app.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
79
80
81
82
83
84
85
86
87
88
89
90
from flask import Flask, request, jsonify
app = Flask(__name__)
# Placeholder for the replicas
replicas = ["Server 1", "Server 2", "Server 3"]
@app.route('/rep', methods=['GET'])
def get_replicas():
response = {
"message": {
"N": len(replicas),
"replicas": replicas
},
"status": "successful"
}
return jsonify(response), 200
@app.route('/add', methods=['POST'])
def add_replicas():
data = request.get_json()
n = data.get('n')
hostnames = data.get('hostnames', [])
if len(hostnames) > n:
response = {
"message": "<Error> Length of hostname list is more than newly added instances",
"status": "failure"
}
return jsonify(response), 400
new_replicas = hostnames[:n]
replicas.extend(new_replicas)
response = {
"message": {
"N": len(replicas),
"replicas": replicas
},
"status": "successful"
}
return jsonify(response), 200
@app.route('/rm', methods=['DELETE'])
def remove_replicas():
data = request.get_json()
n = data.get('n')
hostnames = data.get('hostnames', [])
if len(hostnames) > n:
response = {
"message": "<Error> Length of hostname list is more than removable instances",
"status": "failure"
}
return jsonify(response), 400
for hostname in hostnames:
if hostname in replicas:
replicas.remove(hostname)
while len(hostnames) < n and replicas:
replicas.pop()
response = {
"message": {
"N": len(replicas),
"replicas": replicas
},
"status": "successful"
}
return jsonify(response), 200
@app.route('/<path>', methods=['GET'])
def get_path(path):
if path != "home":
response = {
"message": f"<Error> '/{path}' endpoint does not exist in server replicas",
"status": "failure"
}
return jsonify(response), 400
# Here you should have the logic to forward the request to one of the replicas
response = {
"message": "Request forwarded to one of the replicas",
"status": "successful"
}
return jsonify(response), 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)