#!/usr/bin/env python3 # This is the entry point to a gunicorn app (not intended for standalone use) import json import time import base64 import re CACHE_TIMEOUT = 14400 import rapp.overview import rapp.bat import rapp.drafts webmap = { '/api/overview': rapp.overview.run, '/api/bat/comments': rapp.bat.comments, '/api/bat/agenda': rapp.bat.agenda, '/api/bat/agenda/refresh': rapp.bat.agenda_forced, '/api/bat/publish': rapp.bat.publish, '/api/drafts/index': rapp.drafts.index, '/api/drafts/save': rapp.drafts.save, '/api/drafts/fetch': rapp.drafts.fetch, '/api/drafts/delete': rapp.drafts.delete, '/api/drafts/forgotten': rapp.drafts.forgotten, } def app(environ, start_fn): now = time.time() uri = environ.get('PATH_INFO', '/') if uri in webmap: user = None bauth = re.match(r"Basic (.+)", environ.get('HTTP_AUTHORIZATION', 'foo')) if bauth: bdec = base64.b64decode(bauth.group(1)).decode('utf-8') m = re.match("^(.+?):.+$", bdec) if m: user = m.group(1) if user: try: output = webmap[uri](environ, user) except Exception as e: output = {'okay': False, 'message': str(e)} else: output = {'okay': False, 'message': 'Not authorized'} else: output = {'okay': False, 'message': 'Unknown URI %s' % uri} output['took'] = int((time.time() - now) * 1000) out = json.dumps(output, indent = 2, sort_keys = True).encode('ascii') start_fn('200 OK', [('Content-Type', 'application/json'), ('Content-Length', str(len(out)))]) return [out]