1 from plomlib import PlomException, run_server, PlomHandler
2 from urllib.parse import urlparse
3 from income_progress_bars import IncomeProgressHandler
4 from calories import ConsumptionsHandler
5 from ledger import LedgerHandler
6 from todo import TodoHandler
10 class UnitedRequestHandler(PlomHandler):
11 routes = {'GET':{}, 'POST':{}}
15 def register_app(cls, app):
16 name, data = app.app_init(app, cls)
20 def add_route(cls, method, path, service):
21 if not method in cls.routes.keys():
22 cls.routes[method] = {}
23 cls.routes[method][path] = service
26 self.try_do(self._do_posts)
29 parsed_url = urlparse(self.path)
30 path_toks = parsed_url.path.split('/')
31 while len(path_toks) > 0:
32 target_path = '/'.join(path_toks)
33 if target_path in self.routes['POST'].keys():
34 self.routes['POST'][target_path](self)
37 page = 'nothing to post?'
41 self.try_do(self._do_gets)
44 parsed_url = urlparse(self.path)
45 path_toks = parsed_url.path.split('/')
46 while len(path_toks) > 0:
47 target_path = '/'.join(path_toks)
49 if target_path in self.routes['GET'].keys():
50 self.routes['GET'][target_path](self)
53 page = 'hi there!<br />'
54 for route in self.routes['GET']:
55 page += f'<a href="{route}"/>{route}</a><br />'
59 if __name__ == "__main__":
60 UnitedRequestHandler.register_app(IncomeProgressHandler)
61 UnitedRequestHandler.register_app(ConsumptionsHandler)
62 UnitedRequestHandler.register_app(LedgerHandler)
63 UnitedRequestHandler.register_app(TodoHandler)
64 run_server(server_port, UnitedRequestHandler)