The application class. It's passed a directory with configuration values.
| 19 | |
| 20 | |
| 21 | class CoolMagicApplication: |
| 22 | """ |
| 23 | The application class. It's passed a directory with configuration values. |
| 24 | """ |
| 25 | |
| 26 | def __init__(self, config): |
| 27 | self.config = config |
| 28 | |
| 29 | for fn in listdir(path.join(path.dirname(__file__), "views")): |
| 30 | if fn.endswith(".py") and fn != "__init__.py": |
| 31 | __import__(f"coolmagic.views.{fn[:-3]}") |
| 32 | |
| 33 | from coolmagic.utils import exported_views |
| 34 | |
| 35 | rules = [ |
| 36 | # url for shared data. this will always be unmatched |
| 37 | # because either the middleware or the webserver |
| 38 | # handles that request first. |
| 39 | Rule("/public/<path:file>", endpoint="shared_data") |
| 40 | ] |
| 41 | self.views = {} |
| 42 | for endpoint, (func, rule, extra) in exported_views.items(): |
| 43 | if rule is not None: |
| 44 | rules.append(Rule(rule, endpoint=endpoint, **extra)) |
| 45 | self.views[endpoint] = func |
| 46 | self.url_map = Map(rules) |
| 47 | |
| 48 | def __call__(self, environ, start_response): |
| 49 | urls = self.url_map.bind_to_environ(environ) |
| 50 | req = Request(environ, urls) |
| 51 | try: |
| 52 | endpoint, args = urls.match(req.path) |
| 53 | resp = self.views[endpoint](**args) |
| 54 | except NotFound: |
| 55 | resp = self.views["static.not_found"]() |
| 56 | except (HTTPException, RequestRedirect) as e: |
| 57 | resp = e |
| 58 | return resp(environ, start_response) |
| 59 | |
| 60 | |
| 61 | def make_app(config=None): |