Browse Source

add static routing, improve making the test urls..

main
John-Mark Gurney 5 years ago
parent
commit
2f894adf52
2 changed files with 52 additions and 10 deletions
  1. +6
    -0
      NOTES.md
  2. +46
    -10
      solardash/__init__.py

+ 6
- 0
NOTES.md View File

@@ -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



+ 46
- 10
solardash/__init__.py View File

@@ -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')


Loading…
Cancel
Save