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
9 class UnitedRequestHandler(PlomHandler):
10 routes = {'GET':{}, 'POST':{}}
14 def register_app(cls, app):
15 name, data = app.app_init(app, cls)
19 def add_route(cls, method, path, service):
20 if not method in cls.routes.keys():
21 cls.routes[method] = {}
22 cls.routes[method][path] = service
26 parsed_url = urlparse(self.path)
27 path_toks = parsed_url.path.split('/')
28 while len(path_toks) > 0:
29 target_path = '/'.join(path_toks)
30 if target_path in self.routes['POST'].keys():
31 self.routes['POST'][target_path](self)
34 page = 'nothing to post?'
36 except PlomException as e:
41 parsed_url = urlparse(self.path)
42 path_toks = parsed_url.path.split('/')
43 while len(path_toks) > 0:
44 target_path = '/'.join(path_toks)
46 if target_path in self.routes['GET'].keys():
47 self.routes['GET'][target_path](self)
50 page = 'hi there!<br />'
51 for route in self.routes['GET']:
52 page += f'<a href="{route}"/>{route}</a><br />'
54 except PlomException as e:
58 if __name__ == "__main__":
59 UnitedRequestHandler.register_app(IncomeProgressHandler)
60 UnitedRequestHandler.register_app(ConsumptionsHandler)
61 UnitedRequestHandler.register_app(LedgerHandler)
62 run_server(server_port, UnitedRequestHandler)