84 lines
1.8 KiB
Python
84 lines
1.8 KiB
Python
import asyncio
|
|
|
|
from izzylib import logging
|
|
from izzylib.http_server_async import Application
|
|
|
|
from . import __version__, views
|
|
from .config import config, path, first_start
|
|
from .database import db
|
|
from .functions import push_message
|
|
from .middleware import AuthCheck
|
|
|
|
|
|
def template_context(context):
|
|
with db.session as s:
|
|
config = s.get.config_all()
|
|
|
|
context['config'] = config
|
|
|
|
return context
|
|
|
|
|
|
with db.session as s:
|
|
app = Application(
|
|
title = s.get.config('name'),
|
|
name = 'UnciaRelay',
|
|
version = __version__,
|
|
listen = config.listen,
|
|
port = config.port,
|
|
host = config.host,
|
|
git_repo = 'https://git.barkshark.xyz/izaliamae/uncia',
|
|
proto = 'https',
|
|
tpl_search = [path.frontend],
|
|
tpl_context = template_context,
|
|
views = [getattr(views, view) for view in dir(views) if view.startswith('Uncia')],
|
|
middleware = [AuthCheck]
|
|
)
|
|
|
|
app.add_static('/style', path.frontend.join('style'))
|
|
|
|
|
|
async def run_retries():
|
|
await asyncio.sleep(60 * 60)
|
|
|
|
with db.session as s:
|
|
for instance in s.search('inbox'):
|
|
retry_instance(instance)
|
|
|
|
|
|
def retry_instance(instance):
|
|
retries = instance.retries()
|
|
fails = 0
|
|
|
|
if not retries:
|
|
return
|
|
|
|
logging.verbose(f'Retrying {len(retries)} message(s) for {instance.domain}')
|
|
|
|
for retry in instance.retries():
|
|
try:
|
|
if push_message(instance.inbox, retry.data, headers=retry.headers):
|
|
fails = 0
|
|
|
|
with db.session as s:
|
|
s.remove('retry', row=retry)
|
|
|
|
else:
|
|
fails += 1
|
|
|
|
if fails >= 5:
|
|
logging.verbose(f'Failed 5 times in a row when retrying messages for {instance.domain}')
|
|
return
|
|
|
|
except Exception as e:
|
|
logging.debug(f'{e.__class__.__name__}: {e}')
|
|
return
|
|
|
|
|
|
def main():
|
|
if first_start:
|
|
logging.error(f'Uncia has not been configured yet. Please edit {config.envfile} first.')
|
|
return
|
|
|
|
app.start(run_retries())
|