aboutsummaryrefslogtreecommitdiffstats
path: root/app.py
diff options
context:
space:
mode:
authorGravatar martmists <[email protected]>2018-02-05 20:26:54 +0100
committerGravatar martmists <[email protected]>2018-02-05 20:26:54 +0100
commit80a1ab522e7a49f3ea1168d8658fbff293c866f1 (patch)
tree206a45b1a68c273db43ad04b105bcd3e7c48f565 /app.py
parentDynamic route loader; proper application structure (diff)
Major update
- Switch to Japronto - More linters - Rewrite route handling - Rewrite error handling - Rewrite static handling - Error when no `Index` propery is found - Probably some more stuff? idk Code needs testing; Maybe we could use pytest? Signed-off-by: martmists <[email protected]>
Diffstat (limited to 'app.py')
-rw-r--r--app.py110
1 files changed, 104 insertions, 6 deletions
diff --git a/app.py b/app.py
index f3bb9a60..5efc1ee7 100644
--- a/app.py
+++ b/app.py
@@ -1,10 +1,108 @@
-# coding=utf-8
+#!/usr/bin/env python3.6
-from pysite.route_manager import RouteManager
+# Stdlib
+from importlib.util import module_from_spec, spec_from_file_location
+import mimetypes
+import os
+# External Libraries
+from japronto import Application
-manager = RouteManager()
-app = manager.app
+app = Application()
-if __name__ == '__main__':
- manager.run()
+
+def static_file(path: str): # type: (str) -> (req: {Response}) -> Coroutine
+ async def inner(req): # type: ({Response}) -> Coroutine
+ with open(path) as file:
+ return req.Response(
+ text=file.read(), mime_type=mimetypes.guess_type(path)[0])
+
+ return inner
+
+
+def find_static_files(dir_: str) -> list: # type: (str) -> List[str]
+ data = []
+ for path, _, files in os.walk(dir_):
+ if not files:
+ continue
+
+ for file in files:
+ if not file.split(".")[-1] not in ("html", "css", "js"):
+ continue
+
+ pathname = f"{path[len(dir_):]}/{file.strip()}"
+ data.append(pathname.split(".")[0], static_file(pathname))
+
+ return data
+
+
+def find_routes(
+ dir_: str) -> list: # type: (str) -> List[Tuple[str, str, callable]]
+ data = []
+ for path, _, files in os.walk(dir_):
+ if not files:
+ continue
+
+ for file in files:
+ if not file.endswith(".py"):
+ continue
+ pathname = f"{path}/{file.strip()}"
+ spec = spec_from_file_location(file[:-3], pathname)
+ module = module_from_spec(spec)
+ spec.loader.exec_module(module)
+
+ if not hasattr(module, "Index"):
+ raise Exception("No `Index` class!")
+
+ res = module.Index()
+ del module, spec
+
+ route_paths = res.path
+ for path_ in route_paths:
+ # TODO: Add all request types here
+ for method in ("GET", "POST", "DELETE", "PATCH"):
+ if hasattr(res, method.lower()):
+ data.append((path_, method, getattr(
+ res, method.lower())))
+
+ return data
+
+
+def find_errors(dir_: str) -> list: # type: (str) -> List[str]
+ data = []
+ for path, _, files in os.walk(dir_):
+ if not files:
+ continue
+
+ for file in files:
+ if not file.endswith(".py"):
+ continue
+ pathname = f"{path}/{file.strip()}"
+ spec = spec_from_file_location(file[:-3], pathname)
+ module = module_from_spec(spec)
+ spec.loader.exec_module(module)
+
+ if not hasattr(module, "Index"):
+ raise Exception("No `Index` class!")
+
+ res = module.Index()
+ del module, spec
+ data.append((res.error_code, res.err))
+
+ return data
+
+
+routes = find_routes("routes")
+static = find_static_files("static")
+errors = find_errors("error_handlers")
+
+for route in routes:
+ app.router.add_route(route.path, route.run, method=route.method)
+
+for path, method, handle in static:
+ app.router.add_route(path, handle, method=method)
+
+for errcode, handler in errors:
+ app.add_error_handler(errcode, handler)
+
+app.run(debug=True, port=80)