diff --git a/CHANGELOG.md b/CHANGELOG.md index a873d14..af22280 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ # xbotlib x.x.x (UNRELEASED) - 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)) # xbotlib 0.15.1 (2021-01-24) diff --git a/README.md b/README.md index e73db41..6575200 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ you want to chat or just invite your bots for testing. - [Bot.direct(message)](#bot-direct-message) - [Bot.group(message)](#bot-group-message) - [Bot.serve(request)](#bot-serve-request) + - [Bot.routes()](#bot-routes) - [Bot.setup()](#bot-setup) - [SimpleMessage](#simplemessage) - [Bot](#bot) @@ -115,6 +116,21 @@ Arguments: - **request**: the web request +### Bot.routes() + +Register additional routes for web serving. + +See [this section](#serving-http) for more. + +This is an advanced feature. Use `self.web.add_routes`. + +```python +from aiohttp.web import get + +def routes(self): + self.web.add_routes([get("/foo", self.foo)]) +``` + ### Bot.setup() Run some setup logic before starting your bot. diff --git a/xbotlib.py b/xbotlib.py index a51aaf4..5726f6f 100644 --- a/xbotlib.py +++ b/xbotlib.py @@ -702,6 +702,12 @@ class Bot(ClientXMPP): except Exception: self.web.add_routes([get("/", self.default_serve)]) + try: + self.routes() + self.log.info("Registered additional web routes") + except Exception: + pass + self.log.info(f"Serving on http://0.0.0.0:{self.port}") run_app(self.web, port=self.port, print=None)