aboutsummaryrefslogtreecommitdiff
path: root/bin/web_kbn.py
diff options
context:
space:
mode:
Diffstat (limited to 'bin/web_kbn.py')
-rw-r--r--bin/web_kbn.py77
1 files changed, 64 insertions, 13 deletions
diff --git a/bin/web_kbn.py b/bin/web_kbn.py
index b66e2a5..e160fde 100644
--- a/bin/web_kbn.py
+++ b/bin/web_kbn.py
@@ -5,11 +5,13 @@ import aiohttp_jinja2
import os
import __py_include
+from io import StringIO
from typing import Optional
from homekit.config import config, AppConfigUnit
from homekit.util import homekit_path
from aiohttp import web
from homekit import http
+from homekit.modem import ModemsConfig
class WebKbnConfig(AppConfigUnit):
@@ -18,10 +20,50 @@ class WebKbnConfig(AppConfigUnit):
@classmethod
def schema(cls) -> Optional[dict]:
return {
- 'listen_addr': cls._addr_schema(required=True)
+ 'listen_addr': cls._addr_schema(required=True),
+ 'assets_public_path': {'type': 'string'}
}
+STATIC_FILES = [
+ 'bootstrap.min.css',
+ 'bootstrap.min.js',
+ 'polyfills.js',
+ 'app.js',
+ 'app.css'
+]
+
+
+def get_js_link(file, version) -> str:
+ if version:
+ file += f'?version={version}'
+ return f'<script src="{config.app_config["assets_public_path"]}/{file}" type="text/javascript"></script>'
+
+
+def get_css_link(file, version) -> str:
+ if version:
+ file += f'?version={version}'
+ return f'<link rel="stylesheet" type="text/css" href="{config.app_config["assets_public_path"]}/{file}">'
+
+
+def get_head_static() -> str:
+ buf = StringIO()
+ for file in STATIC_FILES:
+ v = 1
+ try:
+ q_ind = file.index('?')
+ v = file[q_ind+1:]
+ file = file[:file.index('?')]
+ except ValueError:
+ pass
+
+ if file.endswith('.js'):
+ buf.write(get_js_link(file, v))
+ else:
+ buf.write(get_css_link(file, v))
+ return buf.getvalue()
+
+
class WebSite(http.HTTPServer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@@ -31,20 +73,29 @@ class WebSite(http.HTTPServer):
loader=jinja2.FileSystemLoader(homekit_path('web', 'kbn_templates'))
)
+ self.app.router.add_static('/assets/', path=homekit_path('web', 'kbn_assets'))
+
self.get('/', self.get_index)
+ self.get('/modems', self.get_modems)
+
+ async def render_page(self,
+ req: http.Request,
+ context: Optional[dict] = None):
+ if context is None:
+ context = {}
+ context = {
+ **context,
+ 'head_static': get_head_static(),
+ 'title': 'this is title'
+ }
+ response = aiohttp_jinja2.render_template('index.html', req, context=context)
+ return response
+
+ async def get_index(self, req: http.Request):
+ return await self.render_page(req)
- @staticmethod
- async def get_index(req: http.Request):
- # context = {
- # 'username': request.match_info.get("username", ""),
- # 'current_date': 'January 27, 2017'
- # }
- # response = aiohttp_jinja2.render_template("example.html", request,
- # context=context)
- # return response
-
- message = "nothing here, keep lurking"
- return http.Response(text=message, content_type='text/plain')
+ async def get_modems(self, req: http.Request):
+ pass
if __name__ == '__main__':