From 5efee0ffd208700f9f8aa2fe6c94d6048b977a1f Mon Sep 17 00:00:00 2001 From: Izalia Mae Date: Thu, 2 Sep 2021 17:27:05 -0400 Subject: [PATCH] template: merge context and globals before context function, http_server: start adding fediverse views --- README.md | 6 ++- .../izzylib/http_server/application.py | 17 +++++-- http_server/izzylib/http_server/view.py | 48 +++++++++++++++++++ template/izzylib/template/__init__.py | 15 ++++-- 4 files changed, 77 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 26a4023..4fb66f8 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,10 @@ You only need to install the base and whatever sub-modules you want to use $(venv)/bin/python -m pip install -e "git+https://git.barkshark.xyz/izaliamae/izzylib.git@rework#egg=izzylib-base&subdirectory=base" + $(venv)/bin/python -m pip install -e "git+https://git.barkshark.xyz/izaliamae/izzylib.git@rework#egg=izzylib-dbus&subdirectory=dbus" + + $(venv)/bin/python -m pip install -e "git+https://git.barkshark.xyz/izaliamae/izzylib.git@rework#egg=izzylib-hasher&subdirectory=hasher" + $(venv)/bin/python -m pip install -e "git+https://git.barkshark.xyz/izaliamae/izzylib.git@rework#egg=izzylib-http-server&subdirectory=http_server" $(venv)/bin/python -m pip install -e "git+https://git.barkshark.xyz/izaliamae/izzylib.git@rework#egg=izzylib-http-requests-client&subdirectory=requests_client" @@ -22,7 +26,7 @@ You only need to install the base and whatever sub-modules you want to use ### From Source - $(venv)/bin/python setup.py install ['all' or a combination of these: http_server requests_client sql template tinydb] + $(venv)/bin/python setup.py install ['all' or a combination of these: dbus hasher http_server requests_client sql template tinydb] ## Documentation diff --git a/http_server/izzylib/http_server/application.py b/http_server/izzylib/http_server/application.py index 9502747..3e08bcd 100644 --- a/http_server/izzylib/http_server/application.py +++ b/http_server/izzylib/http_server/application.py @@ -30,7 +30,7 @@ log_ext_ignore = [ frontend = Path(__file__).resolve.parent.join('frontend') class Application(sanic.Sanic): - def __init__(self, **kwargs): + def __init__(self, class_views=[], **kwargs): self.cfg = Config(**kwargs) super().__init__(self.cfg.name, request_class=self.cfg.request_class) @@ -45,6 +45,7 @@ class Application(sanic.Sanic): self.cfg.tpl_autoescape ) + self.template.add_env('app', self) self.template.add_env('cfg', self.cfg) self.template.add_env('len', len) @@ -56,6 +57,9 @@ class Application(sanic.Sanic): self.static('/favicon.ico', frontend.join('static/icon64.png')) self.static('/framework/static', frontend.join('static')) + for view in class_views: + self.add_class_route(view) + self.add_error_handler(MissingTemplateError) self.add_error_handler(GenericError) @@ -64,6 +68,9 @@ class Application(sanic.Sanic): signal.signal(signal.SIGQUIT, self.finish) signal.signal(signal.SIGTERM, self.finish) + ## compat + self.start = self.run + def add_class_route(self, cls): for route in cls.paths: @@ -100,10 +107,12 @@ class Application(sanic.Sanic): return handler - def start(self): + def run(self): # register built-in middleware now so they're last in the chain self.add_middleware(Headers) - self.add_middleware(AccessLog) + + if self.cfg.access_log: + self.add_middleware(AccessLog) msg = f'Starting {self.cfg.name} at {self.cfg.host}:{self.cfg.port}' @@ -111,7 +120,7 @@ class Application(sanic.Sanic): msg += f' with {self.cfg.workers} workers' logging.info(msg) - self.run( + super().run( host = self.cfg.listen, port = self.cfg.port, workers = self.cfg.workers, diff --git a/http_server/izzylib/http_server/view.py b/http_server/izzylib/http_server/view.py index 5db3de3..887d376 100644 --- a/http_server/izzylib/http_server/view.py +++ b/http_server/izzylib/http_server/view.py @@ -17,6 +17,8 @@ class View(HTTPMethodView): return handler(request, Response(self.app, request), *args, **kwargs) +### Frontend Template Views ### + class Manifest(View): paths = ['/framework/manifest.json'] @@ -62,3 +64,49 @@ class Style(View): async def get(self, request, response): return response.template('base.css', content_type='text/css') + +### ActivityPub Views ### + +class Actor(View): + paths = ['/actor', '/actor/', '/inbox'] + + def get(self, request, response, actor=None): + if not actor: + actor = 'system' + + pass + + + def post(self, request, response, actor=None): + if not actor: + actor = 'system' + + pass + + +class Nodeinfo(View): + paths = ['/nodeinfo/2.0.json'] + + def get(self, request, response): + pass + + +class Webfinger(View): + paths = ['/.well-known/webfinger'] + + def get(self, request, response): + pass + + +class WkHostMeta(View): + paths = ['/.well-known/host-meta'] + + def get(self, request, response): + pass + + +class WkNodeinfo(View): + paths = ['/.well-known/nodeinfo'] + + def get(self, request, response): + pass diff --git a/template/izzylib/template/__init__.py b/template/izzylib/template/__init__.py index 108c4b7..25bf1c7 100644 --- a/template/izzylib/template/__init__.py +++ b/template/izzylib/template/__init__.py @@ -69,7 +69,7 @@ class Template(Environment): izzylog.error('Context is not callable') return - if not isinstance(context({}, {}), dict): + if not isinstance(context({}), dict): izzylog.error('Context does not return a dict or dict-like object') return @@ -113,14 +113,21 @@ class Template(Environment): self.filters.update(data) - def render(self, tplfile, context={}, headers={}, cookies={}, request=None, pprint=False): - if not isinstance(context, dict): + def render(self, tplfile, context_data={}, headers={}, cookies={}, request=None, pprint=False): + if not isinstance(context_data, dict): raise TypeError(f'context for {tplfile} not a dict: {type(context)} {context}') + context = DotDict(self.globals) + context.update(context_data) context['request'] = request if request else {'headers': headers, 'cookies': cookies} if self.func_context: - context.update(self.func_context(DotDict(context), DotDict(self.globals))) + # Backwards compat + try: + context = self.func_context(context) + + except TypeError: + context = self.func_context(context, {}) result = self.get_template(tplfile).render(context)