-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
56 lines (45 loc) · 1.72 KB
/
main.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
import os
from http.server import HTTPServer, BaseHTTPRequestHandler
from http import HTTPStatus
from app import settings
from app.router import Router
router = Router()
class _RequestHandler(BaseHTTPRequestHandler):
"""
The RequestHandler is the main point of the application.
The Request handler ties the different parts of the web server together.
"""
def _set_headers(self, headers):
"Set response headers"
self.send_response(HTTPStatus.OK.value)
key, value = headers
self.send_header(key, value)
# Allow requests from any origin, so CORS policies don't
# prevent local development.
self.send_header('Access-Control-Allow-Origin', settings.ALLOWED_HOSTS)
self.end_headers()
def do_GET(self):
"Handle all get requests."
resolved_route = router.get_view(self.path, settings.GET_REQUEST)
view_instance = resolved_route["view"](self)
self._set_headers(view_instance.headers)
if resolved_route["route"]:
self.send_response(200, message="Success")
else:
self.send_response(404, message="Not found")
self.wfile.write(view_instance.render())
def run_server():
"Main entrypoint of the application"
from app.logging import logger
port = settings.PORT
if not port:
logger.error("Set the PORT number in app/settings.py")
raise Exception("Set the PORT number in app/settings.py")
os.chdir(settings.ROOT)
server_address = ('', port)
httpd = HTTPServer(server_address, _RequestHandler)
print('serving at %s:%d' % server_address)
logger.info("Serving at {}".format(port))
httpd.serve_forever()
if __name__ == "__main__":
run_server()