Update docs

This commit is contained in:
Richard Chien
2020-01-31 13:08:17 +08:00
parent da7c62a259
commit 389e83b4f7
5 changed files with 67 additions and 72 deletions

View File

@ -2,22 +2,38 @@
如果需要对 web 框架进行更详细的控制,可以通过 `bot.server_app` 访问到内部的 Quart 对象,之后可以像使用 Quart 的 app 对象一样添加路由、设置生命周期处理函数等。
::: tip 提示
Quart 是一个与 Flask 具有相同 API 的异步 web 框架,其用法可以参考 [官方文档](https://pgjones.gitlab.io/quart/)。
:::
## 自定义路由
### 简单的主页
这里以一个简单的管理页面为例:
```python
from nonebot import get_bot
bot = get_bot()
import nonebot
@bot.server_app.route('/')
async def hello_world():
await bot.send_private_msg(1002647525, '你的主页被访问了')
return '欢迎来到我的主页'
bot = nonebot.get_bot() # 在此之前必须已经 init
@bot.server_app.route('/admin')
async def admin():
await bot.send_private_msg(12345678, '你的主页被访问了')
return '欢迎来到管理页面'
```
启动 nonebot 后访问 <http://127.0.0.1:8080/>,你会看见主页的欢迎词,并收到机器人的提醒。
启动 NoneBot 后访问 <http://127.0.0.1:8080/admin>,你会看见管理页面的欢迎词,并收到机器人的提醒。
### 更多应用
## 处理生命周期事件
Quart 是一个与 Flask 具有相同 API 的异步 web 框架,其用法可以参考[Flask官方文档](https://flask.palletsprojects.com/)或它的[简中翻译版本](http://docs.jinkan.org/docs/flask/),关于 Quart 可以参考[Quart官方文档](https://pgjones.gitlab.io/quart/)
有时可能需要在 NoneBot 启动时初始化数据库连接池,例如:
```python
import nonebot
bot = nonebot.get_bot() # 在此之前必须已经 init
@bot.server_app.before_serving
async def init_db():
# 这会在 NoneBot 启动后立即运行
pass
```