Support stylesheets and fix serving
Closes https://git.autonomic.zone/decentral1se/xbotlib/issues/38.
This commit is contained in:
parent
09e4c97597
commit
bf8ccedca1
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
- Add `setup` function hook ([#36](https://git.autonomic.zone/decentral1se/xbotlib/issues/36))
|
- Add `setup` function hook ([#36](https://git.autonomic.zone/decentral1se/xbotlib/issues/36))
|
||||||
- Add support for defining additional routes ([#31](https://git.autonomic.zone/decentral1se/xbotlib/issues/31))
|
- Add support for defining additional routes ([#31](https://git.autonomic.zone/decentral1se/xbotlib/issues/31))
|
||||||
|
- Fix loading of template and serving (attribute clash) ([#38](https://git.autonomic.zone/decentral1se/xbotlib/issues/38))
|
||||||
|
|
||||||
# xbotlib 0.15.1 (2021-01-24)
|
# xbotlib 0.15.1 (2021-01-24)
|
||||||
|
|
||||||
|
11
README.md
11
README.md
@ -343,12 +343,19 @@ easily template and generate HTML. The web server is provided by
|
|||||||
The default template search path is `index.html.j2` in the current working
|
The default template search path is `index.html.j2` in the current working
|
||||||
directory. This can be configured through the usual configuration entrypoints.
|
directory. This can be configured through the usual configuration entrypoints.
|
||||||
|
|
||||||
Here's a small example that renders a random ASCII letter.
|
Here's a small example that renders a random ASCII letter and uses a stylesheet.
|
||||||
|
|
||||||
> index.html.j2
|
> index.html.j2
|
||||||
|
|
||||||
```jinja
|
```jinja
|
||||||
<h1>{{ letter }}</h1>
|
<html>
|
||||||
|
<head>
|
||||||
|
<style> h1 { color: red; } </style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>{{ letter }}</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
```
|
```
|
||||||
|
|
||||||
> bot.py
|
> bot.py
|
||||||
|
26
xbotlib.py
26
xbotlib.py
@ -446,7 +446,7 @@ class Bot(ClientXMPP):
|
|||||||
or environ.get("XBOT_TEMPLATE", None)
|
or environ.get("XBOT_TEMPLATE", None)
|
||||||
or "index.html.j2"
|
or "index.html.j2"
|
||||||
)
|
)
|
||||||
serve = (
|
serve_web = (
|
||||||
self.args.serve
|
self.args.serve
|
||||||
or self.config.serve
|
or self.config.serve
|
||||||
or environ.get("XBOT_SERVE", None)
|
or environ.get("XBOT_SERVE", None)
|
||||||
@ -484,8 +484,12 @@ class Bot(ClientXMPP):
|
|||||||
self.rooms = rooms
|
self.rooms = rooms
|
||||||
self.no_auto_join = no_auto_join
|
self.no_auto_join = no_auto_join
|
||||||
self.port = port
|
self.port = port
|
||||||
self.serve = serve
|
self.serve_web = serve_web
|
||||||
self.template = self.load_template(template) if self.serve else None
|
|
||||||
|
self.template = None
|
||||||
|
if self.serve_web:
|
||||||
|
self.template = self.load_template(template)
|
||||||
|
|
||||||
self.storage = storage
|
self.storage = storage
|
||||||
self.storage_file = Path(storage_file).absolute()
|
self.storage_file = Path(storage_file).absolute()
|
||||||
|
|
||||||
@ -670,14 +674,14 @@ class Bot(ClientXMPP):
|
|||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if self.serve:
|
if self.serve_web:
|
||||||
self.log.info("Turning on the web server")
|
self.log.info("Running the web server")
|
||||||
self.serve_web()
|
self.run_web_server()
|
||||||
|
|
||||||
if hasattr(self, "setup"):
|
if hasattr(self, "setup"):
|
||||||
try:
|
try:
|
||||||
self.setup()
|
self.setup()
|
||||||
self.log.info("Finished running Bot.setup")
|
self.log.info("Finished running setup")
|
||||||
except Exception as exception:
|
except Exception as exception:
|
||||||
message = f"Bot.setup failed: {exception}"
|
message = f"Bot.setup failed: {exception}"
|
||||||
self.log.info(message)
|
self.log.info(message)
|
||||||
@ -686,8 +690,8 @@ class Bot(ClientXMPP):
|
|||||||
except (KeyboardInterrupt, RuntimeError):
|
except (KeyboardInterrupt, RuntimeError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def serve_web(self):
|
def run_web_server(self):
|
||||||
"""Serve the web."""
|
"""Run the web server."""
|
||||||
try:
|
try:
|
||||||
from aiohttp.web import Application, get, run_app
|
from aiohttp.web import Application, get, run_app
|
||||||
except ModuleNotFoundError:
|
except ModuleNotFoundError:
|
||||||
@ -697,9 +701,9 @@ class Bot(ClientXMPP):
|
|||||||
|
|
||||||
self.web = Application()
|
self.web = Application()
|
||||||
|
|
||||||
try:
|
if hasattr(self, "serve"):
|
||||||
self.web.add_routes([get("/", self.serve)])
|
self.web.add_routes([get("/", self.serve)])
|
||||||
except Exception:
|
else:
|
||||||
self.web.add_routes([get("/", self.default_serve)])
|
self.web.add_routes([get("/", self.default_serve)])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
Reference in New Issue
Block a user