add fastapi routes, adapters

This commit is contained in:
yanyongyu 2020-07-05 20:39:34 +08:00
parent 4d242875d2
commit ea21d6ffcc
9 changed files with 64 additions and 18 deletions

View File

@ -6,9 +6,9 @@ import importlib
from typing import Optional
from ipaddress import IPv4Address
from .log import logger
from .config import Env, Config
from .drivers import BaseDriver
from nonebot.log import logger
from nonebot.config import Env, Config
from nonebot.drivers import BaseDriver
_driver: Optional[BaseDriver] = None
@ -48,4 +48,4 @@ def run(host: Optional[IPv4Address] = None,
get_driver().run(host, port, *args, **kwargs)
from .plugin import load_plugins, get_loaded_plugins
from nonebot.plugin import load_plugins, get_loaded_plugins

View File

@ -0,0 +1,8 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class BaseBot(object):
def __init__(self):
raise NotImplementedError

10
nonebot/adapters/coolq.py Normal file
View File

@ -0,0 +1,10 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from nonebot.adapters import BaseBot
class Bot(BaseBot):
def __init__(self):
pass

View File

@ -7,6 +7,9 @@ from ipaddress import IPv4Address
class BaseDriver(object):
def __init__(self):
raise NotImplementedError
@property
def server_app(self):
raise NotImplementedError
@ -25,3 +28,9 @@ class BaseDriver(object):
*args,
**kwargs):
raise NotImplementedError
async def _handle_http(self):
raise NotImplementedError
async def _handle_ws_reverse(self):
raise NotImplementedError

View File

@ -1,15 +1,16 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import json
import logging
from typing import Optional
from ipaddress import IPv4Address
import uvicorn
from fastapi import FastAPI
from fastapi import Body, FastAPI, WebSocket
from . import BaseDriver
from nonebot.log import logger
from nonebot.drivers import BaseDriver
class Driver(BaseDriver):
@ -24,6 +25,9 @@ class Driver(BaseDriver):
self.config = config
self._server_app.post("/coolq/")(self._handle_http)
self._server_app.websocket("/coolq/ws")(self._handle_ws_reverse)
@property
def server_app(self):
return self._server_app
@ -76,3 +80,18 @@ class Driver(BaseDriver):
debug=self.config.debug,
log_config=LOGGING_CONFIG,
**kwargs)
async def _handle_http(self, data: dict = Body(...)):
logger.debug(f"Received message: {data}")
return {"status": 200, "message": "success"}
async def _handle_ws_reverse(self, websocket: WebSocket):
await websocket.accept()
while True:
try:
data = await websocket.receive_json()
except json.decoder.JSONDecodeError as e:
logger.exception(e)
continue
logger.debug(f"Received message: {data}")

View File

@ -5,10 +5,10 @@ from functools import wraps
from collections import defaultdict
from typing import Type, List, Dict, Optional, Callable
from .event import Event
from .typing import Handler
from .rule import Rule, user
from .exception import PausedException, RejectedException, FinishedException
from nonebot.event import Event
from nonebot.typing import Handler
from nonebot.rule import Rule, user
from nonebot.exception import PausedException, RejectedException, FinishedException
matchers: Dict[int, List[Type["Matcher"]]] = defaultdict(list)

View File

@ -1,9 +1,9 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from .log import logger
from .event import Event
from .matcher import matchers
from nonebot.log import logger
from nonebot.event import Event
from nonebot.matcher import matchers
async def handle_message(bot, event: Event):

View File

@ -7,9 +7,9 @@ import importlib
from types import ModuleType
from typing import Set, Dict, Type, Optional
from .log import logger
from .rule import Rule
from .matcher import Matcher
from nonebot.rule import Rule
from nonebot.log import logger
from nonebot.matcher import Matcher
plugins: Dict[str, "Plugin"] = {}

View File

@ -4,7 +4,7 @@
import re
from typing import Union, Callable, Optional
from .event import Event
from nonebot.event import Event
class Rule: