From 2f894adf5280e2d1e0f478794d77b2d2038c09ca Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Tue, 21 Jan 2020 11:43:15 -0800 Subject: [PATCH] add static routing, improve making the test urls.. --- NOTES.md | 6 +++++ solardash/__init__.py | 56 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/NOTES.md b/NOTES.md index 3a588a8..3844b67 100644 --- a/NOTES.md +++ b/NOTES.md @@ -4,6 +4,12 @@ https://web.archive.org/web/20200108190917/https://humanwhocodes.com/blog/2009/0 aiohttp websockets: https://web.archive.org/web/20200108235147/https://aiohttp.readthedocs.io/en/stable/web_quickstart.html#websockets +aiohttp static files: +https://aiohttp.readthedocs.io/en/stable/web_reference.html#aiohttp.web.UrlDispatcher.add_static + +aiohttp client response: +https://aiohttp.readthedocs.io/en/stable/client_reference.html#aiohttp.ClientResponse + WebSocket JS specification: https://web.archive.org/web/20200109005314/https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket diff --git a/solardash/__init__.py b/solardash/__init__.py index 7435c7f..3e577f6 100644 --- a/solardash/__init__.py +++ b/solardash/__init__.py @@ -26,10 +26,14 @@ import aiohttp import asyncio import concurrent +import logging import os import shutil import tempfile import unittest +import urllib + +logging.basicConfig(level=logging.DEBUG) from aiohttp import web from RainEagle.parse import LogDir as RELogDir, _cmaiter @@ -73,8 +77,21 @@ class SolarDataWS(object): # XXX - how to configure this properly sdws = SolarDataWS('./raineagle.') -app = web.Application() -app.add_routes(sdws.get_routes()) +def getapp(sdws): + app = web.Application() + app.add_routes(sdws.get_routes()) + p = os.path.join(os.path.dirname(__file__), '..', 'root') + # XXX - aiohttp doesn't have a way to serve static files, or do + # a simple redirect + async def index(request): + return web.FileResponse(os.path.join(p, 'index.html')) + + app.add_routes([ web.RouteDef('GET', '/', index, kwargs={}) ]) + app.add_routes([ web.static(prefix='/', path=p) ]) + + return app + +app = getapp(sdws) # https://stackoverflow.com/questions/23033939/how-to-test-python-3-4-asyncio-code # Slightly modified to timeout and to print trace back when canceled. @@ -105,27 +122,47 @@ class Test(unittest.TestCase): self.tempdir = os.path.join(d, 'subdir') os.mkdir(self.tempdir) + self.rootdir = os.path.join(os.path.dirname(__file__), '..', 'root') os.chdir(self.tempdir) sdws = SolarDataWS(os.path.join(self.tempdir, 'raineagle')) - app = web.Application() - app.add_routes(sdws.get_routes()) + app = getapp(sdws) self._app = app + # launch the server + self._webport = 58323 + + def makeurl(self, path): + return urllib.parse.urljoin('http://localhost:%d/' % self._webport, + path) + def tearDown(self): #print('td:', time.time()) shutil.rmtree(self.basetempdir) self.tempdir = None + @async_test + async def test_staticfiles(self): + runner = web.AppRunner(self._app) + await runner.setup() + site = web.TCPSite(runner, 'localhost', self._webport) + await site.start() + + async with aiohttp.ClientSession() as session, \ + session.get(self.makeurl('')) as req: + #print(repr(req)) + self.assertEqual(req.status, 200) + body = await req.content.read() + with open(os.path.join(self.rootdir, 'index.html'), 'rb') as fp: + self.assertEqual(body, fp.read()) + @async_test async def test_daemon(self): loop = asyncio.get_event_loop() - # launch the server - webport = 58323 runner = web.AppRunner(self._app) await runner.setup() - site = web.TCPSite(runner, 'localhost', webport) + site = web.TCPSite(runner, 'localhost', self._webport) await site.start() with open(os.path.join(self.tempdir, 'raineagle.0.log'), 'w', buffering=1) as fp: @@ -136,14 +173,13 @@ class Test(unittest.TestCase): r = [] try: async with aiohttp.ClientSession() as session, \ - session.ws_connect('http://localhost:%d/ws' % - webport) as ws: + session.ws_connect(self.makeurl('ws')) as ws: async for msg in ws: r.append(msg) if len(r) == 2: break - await ws.send_str('q') + #await ws.send_str('q') self.assertEqual(r[0].type, aiohttp.WSMsgType.TEXT) self.assertEqual(r[0].data, 'ng 1.2740')