-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: introduce webserver (optional, experimental) (#736)
- Loading branch information
Showing
7 changed files
with
107 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
{ | ||
"files.trimTrailingWhitespace": true | ||
} | ||
"files.trimTrailingWhitespace": true, | ||
"editor.formatOnSave": false, | ||
"[python]": { | ||
"editor.formatOnSaveMode": "file", | ||
"editor.formatOnSave": false, | ||
"editor.defaultFormatter": "eeyore.yapf" | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .server import run_web_server # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import asyncio | ||
import json | ||
import logging | ||
|
||
from sanic import Sanic, json as jsonify, Websocket | ||
|
||
from feeluown.app import get_app | ||
from feeluown.serializers import serialize | ||
from feeluown.server.pubsub import Gateway as PubsubGateway | ||
from feeluown.server.handlers.cmd import Cmd | ||
from feeluown.server.handlers.status import StatusHandler | ||
from feeluown.server.handlers.player import PlayerHandler | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
sanic_app = Sanic('FeelUOwn') | ||
|
||
|
||
def resp(js): | ||
return jsonify({'code': 200, 'msg': 'ok', 'data': js}) | ||
|
||
|
||
@sanic_app.route('/api/v1/status') | ||
async def status(request): | ||
cmd = Cmd('status') | ||
app = get_app() | ||
handler = StatusHandler(app) | ||
rv = handler.handle(cmd) | ||
return resp(serialize('python', rv, brief=False)) | ||
|
||
|
||
@sanic_app.post('/api/v1/player/pause') | ||
async def pause(request): | ||
cmd = Cmd('pause') | ||
app = get_app() | ||
rv = PlayerHandler(app).handle(cmd) | ||
return resp(serialize('python', rv, brief=False)) | ||
|
||
|
||
@sanic_app.post('/api/v1/player/resume') | ||
async def resume(request): | ||
cmd = Cmd('resume') | ||
app = get_app() | ||
rv = PlayerHandler(app).handle(cmd) | ||
return resp(serialize('python', rv, brief=False)) | ||
|
||
|
||
@sanic_app.post('/api/v1/player/play') | ||
async def play(request): | ||
js = request.json | ||
cmd = Cmd('play', js['uri']) | ||
app = get_app() | ||
rv = PlayerHandler(app).handle(cmd) | ||
return resp(serialize('python', rv, brief=False)) | ||
|
||
|
||
@sanic_app.websocket('/signal/v1') | ||
async def signal(request, ws: Websocket): | ||
# TODO: 优化这个代码,比如处理连接的关闭。 | ||
queue = asyncio.Queue() | ||
|
||
class Subscriber: | ||
|
||
def write_topic_msg(self, topic, msg): | ||
# TODO: 这个结构体可能会变化,需要注意一下 | ||
queue.put_nowait(json.dumps({'topic': topic, 'data': msg, 'format': 'json'})) | ||
|
||
subscriber = Subscriber() | ||
|
||
pubsub_gateway: PubsubGateway = get_app().pubsub_gateway | ||
for topic in pubsub_gateway.topics: | ||
pubsub_gateway.link(topic, subscriber) | ||
|
||
while True: | ||
data = await queue.get() | ||
await ws.send(data) | ||
|
||
|
||
async def run_web_server(host, port): | ||
server = await sanic_app.create_server(host, port, return_asyncio_server=True) | ||
await server.startup() | ||
await server.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters