-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
138 lines (107 loc) · 4.32 KB
/
Copy pathserver.py
File metadata and controls
138 lines (107 loc) · 4.32 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
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
import json
import os
import logic # tes fonctions métier
class Handler(BaseHTTPRequestHandler):
# --------------------------
# GET
# --------------------------
def do_GET(self):
parsed = urlparse(self.path)
path = parsed.path
if path == "/":
path = "/index.html"
filepath = os.path.join("public", path.lstrip("/"))
if os.path.exists(filepath):
if filepath.endswith(".html"):
ctype = "text/html"
elif filepath.endswith(".css"):
ctype = "text/css"
elif filepath.endswith(".js"):
ctype = "application/javascript"
else:
ctype = "application/octet-stream"
with open(filepath, "rb") as f:
content = f.read()
self.send_response(200)
self.send_header("Content-Type", ctype)
self.end_headers()
self.wfile.write(content)
return # on sort ici, fichier statique servi
if parsed.path == "/algo/dijkstra":
qs = parse_qs(parsed.query)
src = qs.get("src", [""])[0]
dst = qs.get("dst", [""])[0]
path, dist = logic.api_dijkstra(src, dst)
if dist == float("inf") or dist == -1:
dist = "inf"
return self.send_json({"distance": dist, "path": path})
if parsed.path == "/graph":
nodes, edges, cons = logic.load_graph()
return self.send_json({"nodes": nodes, "edges": edges, "cons": cons})
if parsed.path == "/algo/coloring":
result = logic.color()
return self.send_json(result)
# Aucune route trouvée
self.send_error(404)
# --------------------------
# POST
# --------------------------
def do_POST(self):
parsed = urlparse(self.path)
length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(length)
try:
data = json.loads(body)
except:
return self.send_json({"success": False, "message": "JSON invalide"})
if parsed.path == "/graph/node":
status = logic.add_node(data["name"], data["x"], data["y"])
return self.send_json({"success": status == "success"})
if parsed.path == "/graph/cons":
status = logic.add_cons(data["from"], data["to"], data["weight"])
return self.send_json({"success": status == "success"})
if parsed.path == "/graph/edge":
status = logic.add_edge(data["from"], data["to"], data["weight"])
return self.send_json({"success": status == "success"})
self.send_error(404)
# --------------------------
# POST
# --------------------------
def do_DELETE(self):
parsed = urlparse(self.path)
length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(length)
try:
data = json.loads(body)
except:
return self.send_json({"success": False, "message": "JSON invalide"})
if parsed.path == "/graph/node":
status = logic.del_node(data["name"])
return self.send_json({"success": status == "success"})
if parsed.path == "/graph/cons":
status = logic.del_cons(data["from"], data["to"])
return self.send_json({"success": status == "success"})
if parsed.path == "/graph/edge":
status = logic.del_edge(data["from"], data["to"])
return self.send_json({"success": status == "success"})
self.send_error(404)
# --------------------------
# JSON Helper - ✅ CORRIGÉ
# --------------------------
def send_json(self, obj):
j = json.dumps(obj)
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
# ✅ BYTES au lieu de STRING
self.wfile.write(j.encode("utf-8"))
# --------------------------
# SERVER START
# --------------------------
if __name__ == "__main__":
server = HTTPServer(("0.0.0.0", 5000), Handler)
print("✅ Serveur lancé : http://localhost:5000")
server.serve_forever()