feat: 删除网关模式
This commit is contained in:
parent
cca6e47da5
commit
e401a73595
@ -153,39 +153,15 @@ def main():
|
|||||||
log_config=LOGGING_CONFIG,
|
log_config=LOGGING_CONFIG,
|
||||||
)
|
)
|
||||||
|
|
||||||
process = None
|
|
||||||
|
|
||||||
def run_gate():
|
|
||||||
command = [
|
|
||||||
"uvicorn",
|
|
||||||
"xiaomusic.gate:app",
|
|
||||||
"--workers",
|
|
||||||
"4",
|
|
||||||
"--host",
|
|
||||||
"0.0.0.0",
|
|
||||||
"--port",
|
|
||||||
str(config.port),
|
|
||||||
]
|
|
||||||
global process
|
|
||||||
process = subprocess.Popen(command)
|
|
||||||
|
|
||||||
def signal_handler(sig, frame):
|
def signal_handler(sig, frame):
|
||||||
print("主进程收到退出信号,准备退出...")
|
print("主进程收到退出信号,准备退出...")
|
||||||
if process is not None:
|
|
||||||
process.terminate() # 终止子进程
|
|
||||||
process.wait() # 等待子进程退出
|
|
||||||
print("子进程已退出")
|
|
||||||
os._exit(0) # 退出主进程
|
os._exit(0) # 退出主进程
|
||||||
|
|
||||||
# 捕获主进程的退出信号
|
# 捕获主进程的退出信号
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
signal.signal(signal.SIGTERM, signal_handler)
|
signal.signal(signal.SIGTERM, signal_handler)
|
||||||
port = int(config.port)
|
port = int(config.port)
|
||||||
if config.enable_gate:
|
run_server(port)
|
||||||
run_gate()
|
|
||||||
run_server(port + 1)
|
|
||||||
else:
|
|
||||||
run_server(port)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -137,7 +137,6 @@ class Config:
|
|||||||
os.getenv("XIAOMUSIC_REMOVE_ID3TAG", "false").lower() == "true"
|
os.getenv("XIAOMUSIC_REMOVE_ID3TAG", "false").lower() == "true"
|
||||||
)
|
)
|
||||||
delay_sec: int = int(os.getenv("XIAOMUSIC_DELAY_SEC", 3)) # 下一首歌延迟播放秒数
|
delay_sec: int = int(os.getenv("XIAOMUSIC_DELAY_SEC", 3)) # 下一首歌延迟播放秒数
|
||||||
enable_gate: bool = os.getenv("XIAOMUSIC_ENABLE_GATE", "false").lower() == "true"
|
|
||||||
|
|
||||||
def append_keyword(self, keys, action):
|
def append_keyword(self, keys, action):
|
||||||
for key in keys.split(","):
|
for key in keys.split(","):
|
||||||
|
@ -1,124 +0,0 @@
|
|||||||
import json
|
|
||||||
import logging
|
|
||||||
import mimetypes
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
from contextlib import asynccontextmanager
|
|
||||||
|
|
||||||
import aiofiles
|
|
||||||
import httpx
|
|
||||||
from fastapi import FastAPI, HTTPException, Request
|
|
||||||
from fastapi.responses import Response, StreamingResponse
|
|
||||||
from fastapi.staticfiles import StaticFiles
|
|
||||||
|
|
||||||
from xiaomusic import __version__
|
|
||||||
from xiaomusic.config import Config
|
|
||||||
|
|
||||||
config = Config()
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
@asynccontextmanager
|
|
||||||
async def app_lifespan(app):
|
|
||||||
global config
|
|
||||||
try:
|
|
||||||
filename = config.getsettingfile()
|
|
||||||
with open(filename) as f:
|
|
||||||
data = json.loads(f.read())
|
|
||||||
config.update_config(data)
|
|
||||||
except Exception as e:
|
|
||||||
log.exception(f"Execption {e}")
|
|
||||||
yield
|
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(
|
|
||||||
lifespan=app_lifespan,
|
|
||||||
version=__version__,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
folder = os.path.dirname(__file__)
|
|
||||||
app.mount("/static", StaticFiles(directory=f"{folder}/static"), name="static")
|
|
||||||
|
|
||||||
|
|
||||||
async def file_iterator(file_path, start, end):
|
|
||||||
async with aiofiles.open(file_path, mode="rb") as file:
|
|
||||||
await file.seek(start)
|
|
||||||
chunk_size = 1024
|
|
||||||
while start <= end:
|
|
||||||
read_size = min(chunk_size, end - start + 1)
|
|
||||||
data = await file.read(read_size)
|
|
||||||
if not data:
|
|
||||||
break
|
|
||||||
start += len(data)
|
|
||||||
yield data
|
|
||||||
|
|
||||||
|
|
||||||
range_pattern = re.compile(r"bytes=(\d+)-(\d*)")
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/music/{file_path:path}")
|
|
||||||
async def music_file(request: Request, file_path: str):
|
|
||||||
absolute_path = os.path.abspath(config.music_path)
|
|
||||||
absolute_file_path = os.path.normpath(os.path.join(absolute_path, file_path))
|
|
||||||
if not absolute_file_path.startswith(absolute_path):
|
|
||||||
raise HTTPException(status_code=404, detail="File not found")
|
|
||||||
if not os.path.exists(absolute_file_path):
|
|
||||||
raise HTTPException(status_code=404, detail="File not found")
|
|
||||||
|
|
||||||
file_size = os.path.getsize(absolute_file_path)
|
|
||||||
range_start, range_end = 0, file_size - 1
|
|
||||||
|
|
||||||
range_header = request.headers.get("Range")
|
|
||||||
log.info(f"music_file range_header {range_header}")
|
|
||||||
if range_header:
|
|
||||||
range_match = range_pattern.match(range_header)
|
|
||||||
if range_match:
|
|
||||||
range_start = int(range_match.group(1))
|
|
||||||
if range_match.group(2):
|
|
||||||
range_end = int(range_match.group(2))
|
|
||||||
|
|
||||||
log.info(f"music_file in range {absolute_file_path}")
|
|
||||||
|
|
||||||
log.info(f"music_file {range_start} {range_end} {absolute_file_path}")
|
|
||||||
headers = {
|
|
||||||
"Content-Range": f"bytes {range_start}-{range_end}/{file_size}",
|
|
||||||
"Accept-Ranges": "bytes",
|
|
||||||
}
|
|
||||||
mime_type, _ = mimetypes.guess_type(file_path)
|
|
||||||
if mime_type is None:
|
|
||||||
mime_type = "application/octet-stream"
|
|
||||||
return StreamingResponse(
|
|
||||||
file_iterator(absolute_file_path, range_start, range_end),
|
|
||||||
headers=headers,
|
|
||||||
status_code=206 if range_header else 200,
|
|
||||||
media_type=mime_type,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@app.options("/music/{file_path:path}")
|
|
||||||
async def music_options():
|
|
||||||
headers = {
|
|
||||||
"Accept-Ranges": "bytes",
|
|
||||||
}
|
|
||||||
return Response(headers=headers)
|
|
||||||
|
|
||||||
|
|
||||||
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
|
|
||||||
async def proxy(path: str, request: Request):
|
|
||||||
async with httpx.AsyncClient() as client:
|
|
||||||
port = config.port + 1
|
|
||||||
url = f"http://127.0.0.1:{port}/{path}"
|
|
||||||
response = await client.request(
|
|
||||||
method=request.method,
|
|
||||||
url=url,
|
|
||||||
headers=request.headers,
|
|
||||||
params=request.query_params,
|
|
||||||
content=await request.body() if request.method in ["POST", "PUT"] else None,
|
|
||||||
)
|
|
||||||
return Response(
|
|
||||||
content=response.content,
|
|
||||||
status_code=response.status_code,
|
|
||||||
headers=dict(response.headers),
|
|
||||||
)
|
|
@ -127,12 +127,6 @@ var vConsole = new window.VConsole();
|
|||||||
<option value="false">false</option>
|
<option value="false">false</option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<label for="enable_gate">开启网关(重启生效):</label>
|
|
||||||
<select id="enable_gate">
|
|
||||||
<option value="true">true</option>
|
|
||||||
<option value="false" selected>false</option>
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<label for="use_music_api">触屏版兼容模式:</label>
|
<label for="use_music_api">触屏版兼容模式:</label>
|
||||||
<select id="use_music_api">
|
<select id="use_music_api">
|
||||||
<option value="true">true</option>
|
<option value="true">true</option>
|
||||||
|
Loading…
Reference in New Issue
Block a user