This commit is contained in:
Evgeny Zinoviev 2023-09-13 09:34:49 +03:00
parent 44aad914a3
commit 405a17a9fd
5 changed files with 93 additions and 4 deletions

58
bin/web_kbn.py Normal file
View File

@ -0,0 +1,58 @@
#!/usr/bin/env python3
import asyncio
import jinja2
import aiohttp_jinja2
import os
import __py_include
from typing import Optional
from homekit.config import config, AppConfigUnit
from homekit.util import homekit_path
from aiohttp import web
from homekit import http
class WebKbnConfig(AppConfigUnit):
NAME = 'web_kbn'
@classmethod
def schema(cls) -> Optional[dict]:
return {
'listen_addr': cls._addr_schema(required=True)
}
class WebSite(http.HTTPServer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
aiohttp_jinja2.setup(
self.app,
loader=jinja2.FileSystemLoader(homekit_path('web', 'kbn_templates'))
)
self.get('/', self.get_index)
@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')
if __name__ == '__main__':
config.load_app(WebKbnConfig)
loop = asyncio.get_event_loop()
# print(config.app_config)
print(config.app_config['listen_addr'].host)
server = WebSite(config.app_config['listen_addr'])
server.run()

View File

@ -277,9 +277,7 @@ class Config:
and not isinstance(name, bool) \
and issubclass(name, AppConfigUnit) or name == AppConfigUnit:
self.app_name = name.NAME
print(self.app_config)
self.app_config = name()
print(self.app_config)
app_config = self.app_config
else:
self.app_name = name if isinstance(name, str) else None

View File

@ -9,6 +9,7 @@ import logging
import string
import random
import re
import os
from enum import Enum
from datetime import datetime
@ -252,4 +253,10 @@ def next_tick_gen(freq):
t = time.time()
while True:
t += freq
yield max(t - time.time(), 0)
yield max(t - time.time(), 0)
def homekit_path(*args) -> str:
return os.path.realpath(
os.path.join(os.path.dirname(__file__), '..', '..', '..', *args)
)

View File

@ -17,4 +17,7 @@ cerberus~=1.3.4
# following can be installed from debian repositories
# matplotlib~=3.5.0
Pillow==9.5.0
Pillow==9.5.0
jinja2~=3.1.2
aiohttp-jinja2~=1.5.1

View File

@ -0,0 +1,23 @@
<!doctype html>
<html>
<head>
<title>{{ title }}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script>
window.onerror = function(error) {
window.console && console.error(error);
}
</script>
{{ head_static }}
</head>
<body>
<div class="container py-3">
{% if js %}
<script>{{ js|raw }}</script>
{% endif %}
</div>
</body>
</html>