This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.py
92 lines (68 loc) · 2.77 KB
/
start.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
91
92
"""
Author: John Beckingham
Student ID: <redacted>
Author: Hai Yang Xu
Student ID: <redacted>
Maze Generator startup script
Based on code for assignment 2:
<redacted>
"""
import http.server
import os.path
import server.controller as controller
class WebServer(http.server.SimpleHTTPRequestHandler):
"""A simple web server, just enough for this project"""
def _write_headers(self, response_code, content_type):
"""Write response code and basic headers"""
self.send_response(response_code)
# Make sure the user agent does not cache the file
self.send_header("Cache-Control",
"no-cache, no-store, must-revalidate")
self.send_header("Content-Type", content_type)
self.end_headers()
def _respond(self, path, header_only=False):
"""
A simple request handler, serves static files and pass API calls to
the appropriate function
"""
if path == "/" or path == "/index.html":
# The main HTML file
with open("./client/index.html", "rb") as f:
self._write_headers(200, "text/html; charset=utf-8")
if not header_only:
self.wfile.write(f.read())
elif (path == "/index.js" or path == "/engine.js" or
path == "/util.js" or path == "/input.js"):
# Script files
with open("./client{}".format(path), "rb") as f:
self._write_headers(200, "text/html; charset=utf-8")
if not header_only:
self.wfile.write(f.read())
elif path == "/generate_new_map":
# Generate new maze map and mark the new map as active
data = controller.new_maze()
self._write_headers(200, "text/plain; charset=utf-8")
self.wfile.write(data.encode("utf-8"))
elif path == "/solve_current_map":
# Solve the current maze
data = controller.solve_current_maze()
self._write_headers(200, "text/plain; charset=utf-8")
self.wfile.write(data.encode("utf-8"))
elif path.startswith("/get_enemy_path?"):
# Get a path that an enemy should take
data = controller.get_enemy_path(path)
self._write_headers(200, "text/plain; charset=utf-8")
self.wfile.write(data.encode("utf-8"))
else:
self.send_error(404, "Not Found")
def do_GET(self):
"""GET request handler"""
self._respond(self.path, header_only=False)
def do_HEAD(self):
"""HEAD request handler"""
self._respond(self.path, header_only=True)
# Start server
port = 8000
httpd = http.server.HTTPServer(("", port), WebServer)
print("Server started on http://localhost:{}/".format(port))
httpd.serve_forever()