template: merge context and globals before context function, http_server: start adding fediverse views

This commit is contained in:
Izalia Mae 2021-09-02 17:27:05 -04:00
parent cd20eaea97
commit 5efee0ffd2
4 changed files with 77 additions and 9 deletions

View file

@ -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

View file

@ -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,

View file

@ -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/<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

View file

@ -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)