-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
52 lines (43 loc) · 1.69 KB
/
server.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
import os
from http.server import BaseHTTPRequestHandler, HTTPServer
from backend.index import handle_post
PORT = int(os.getenv('PORT', '80'))
class RequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
if self.path == '/backend/':
handle_post(self)
else:
self.send_error(404, 'Not Found')
def do_GET(self):
base_path = self.path.split('?')[0].split('#')[0]
if base_path in ['/index.html', '/main.css', '/main.js', '/']:
if base_path == '/':
file_path = '/index.html'
else:
file_path = self.path
try:
with open(file_path[1:], 'rb') as file:
content = file.read()
self.send_response(200)
if file_path.endswith('.html'):
self.send_header('Content-Type', 'text/html')
elif file_path.endswith('.css'):
self.send_header('Content-Type', 'text/css')
elif file_path.endswith('.js'):
self.send_header('Content-Type', 'text/javascript')
self.end_headers()
self.wfile.write(content)
except FileNotFoundError:
self.send_error(404, 'File Not Found')
else:
self.send_error(404, 'Not Found')
def run(server_class=HTTPServer, handler_class=RequestHandler):
server_address = ('', PORT)
httpd = server_class(server_address, handler_class)
try:
httpd.serve_forever()
except KeyboardInterrupt:
httpd.shutdown()
if __name__ == "__main__":
print(f"Web service starting on port {PORT}")
run()