import json
import logging
import os
from aiohttp import web
from aiohttp.web_urldispatcher import Response
STATIC_INDEX = '''
Nothing to see here...
'''
class WebServer(object):
def __init__(self, _loop, transparency_watcher):
self.stats_url = os.getenv("STATS_URL", 'stats')
self.logger = logging.getLogger('certstream.webserver')
self.loop = _loop
self.watcher = transparency_watcher
self.app = web.Application(loop=self.loop)
self._add_routes()
def run_server(self):
web.run_app(
self.app,
port=int(os.environ.get('PORT', 8080)),
ssl_context=None
)
def _add_routes(self):
self.app.router.add_get("/{}".format(self.stats_url), self.stats_handler)
self.app.router.add_get('/', self.root_handler)
async def root_handler(self, request):
return Response(body=STATIC_INDEX, content_type="text/html")
async def stats_handler(self, _):
return web.Response(
body=json.dumps({
"last_seen": self.watcher.lastseen,
}, indent=4
),
content_type="application/json",
)