12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import json
- import logging
- import os
- from aiohttp import web
- from aiohttp.web_urldispatcher import Response
- STATIC_INDEX = '''
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- </head>
- <body>
- <div id="app">Nothing to see here...</div>
- </html>
- '''
- 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",
- )
|