Skip to content

Commit

Permalink
server: add jsonrpc support for tcp server
Browse files Browse the repository at this point in the history
  • Loading branch information
cosven committed Feb 23, 2025
1 parent e64662f commit 0a743f5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
2 changes: 2 additions & 0 deletions feeluown/argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def add_common_cmds(subparsers: argparse._SubParsersAction):
remove_parser = add_parser('remove', help='从播放列表移除歌曲')
add_parser_ = add_parser('add', help='添加歌曲到播放列表')
exec_parser = add_parser('exec')
jsonrpc_parser = add_parser('jsonrpc')
add_parser('pause', help='暂停播放')
add_parser('resume', help='回复播放')
add_parser('toggle', help='')
Expand All @@ -88,6 +89,7 @@ def add_common_cmds(subparsers: argparse._SubParsersAction):
choices=['song', 'album', 'artist', 'video', 'playlist']
)
exec_parser.add_argument('code', nargs='?', help='Python 代码')
jsonrpc_parser.add_argument('body')


def add_cli_cmds(subparsers: argparse._SubParsersAction):
Expand Down
16 changes: 10 additions & 6 deletions feeluown/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,17 @@ class ExecHandler(BaseHandler):

def before_request(self):
code = self.args.code
if code is None:
self._req.cmd_args = (code, )


class JsonRPCHandler(BaseHandler):
cmds = ('jsonrpc', )

def before_request(self):
body = self.args.body
if body is None:
body = sys.stdin.read()
self._req.has_heredoc = True
self._req.heredoc_word = 'EOF'
self._req.set_heredoc_body(body)
else:
self._req.cmd_args = (code, )
self._req.cmd_args = (body, )


class OnceClient:
Expand Down
1 change: 1 addition & 0 deletions feeluown/server/handlers/handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,4 @@ async def handle_request(
from .exec_ import ExecHandler # noqa
from .sub import SubHandler # noqa
from .set_ import SetHandler # noqa
from .jsonrpc_ import JsonRPCHandler # noqa
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import json
from functools import wraps

from jsonrpc import JSONRPCResponseManager, Dispatcher

from feeluown.fuoexec.fuoexec import fuoexec_get_globals
from feeluown.serializers import serialize, deserialize
from .base import AbstractHandler


class DynamicDispatcher(Dispatcher):
def __getitem__(self, key):
try:
return self.method_map[key]
except KeyError:
method = eval(key, fuoexec_get_globals())
method = eval(key, fuoexec_get_globals()) # pylint: disable=eval-used
return method_wrapper(method)


Expand All @@ -31,10 +33,7 @@ def wrapper(*args, **kwargs):


dispatcher = DynamicDispatcher()


def initialize():
dispatcher.add_method(lambda: "pong", name="ping")
dispatcher.add_method(lambda: "pong", name="ping")


def handle(data):
Expand All @@ -43,3 +42,11 @@ def handle(data):
result = serialize('python', response.result)
response.result = result
return response.data


class JsonRPCHandler(AbstractHandler):
cmds = 'jsonrpc'

def handle(self, cmd):
payload = cmd.args[0]
return json.dumps(handle(payload))
3 changes: 1 addition & 2 deletions feeluown/webserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from feeluown.server.handlers.cmd import Cmd
from feeluown.server.handlers.status import StatusHandler
from feeluown.server.handlers.player import PlayerHandler
from .jsonrpc_ import initialize, handle
from feeluown.server.handlers.jsonrpc_ import handle


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -86,7 +86,6 @@ def write_topic_msg(self, topic, msg):


async def run_web_server(host, port):
initialize()
sanic_app.config.MOTD = False
server = await sanic_app.create_server(host, port, return_asyncio_server=True)
await server.startup()
Expand Down

0 comments on commit 0a743f5

Please sign in to comment.