feat: 后端改为deno

This commit is contained in:
ethan.chen
2025-11-05 11:43:47 +08:00
parent ba3435e1a0
commit f67a3835fa
27 changed files with 1449 additions and 1404 deletions

View File

@@ -0,0 +1,161 @@
/**
* 路由模块 (Deno 版本)
* 定义所有 API 路由
*/
import { Hono } from "hono";
import { cors } from "cors";
import { logger } from "logger";
import { ConnectionManager } from "./websocket_manager.ts";
import { getMusicStatus, controlMusic } from "./apple_music.ts";
import { getLyricsData } from "./lyrics.ts";
export interface ControlRequest {
action: "playpause" | "previous" | "next" | "seek";
position?: number;
}
export interface SearchLyricsRequest {
title: string;
artist: string;
}
export interface GetLyricsFromIdRequest {
title: string;
artist: string;
}
/**
* 注册所有路由
*/
export function registerRoutes(app: Hono, manager: ConnectionManager): void {
// 添加中间件
app.use("*", async (c, next) => {
if (c.req.path !== "/ws") {
console.log("[CORS middleware triggered]", c.req.method, c.req.path)
return await cors({
origin: "*",
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allowHeaders: ["Content-Type", "Authorization"],
})(c, next);
} else {
return await next();
}
});
// 健康检查
app.get("/health", (c) => {
return c.json({
status: "ok",
timestamp: Date.now(),
connections: manager.getConnectionCount(),
});
});
// 获取音乐状态
app.get("/music/status", async (c) => {
try {
const status = await getMusicStatus();
return c.json(status);
} catch (error) {
return c.json({ status: "error", error: String(error) }, 500);
}
});
// 控制音乐播放
app.post("/music/control", async (c) => {
try {
const body = (await c.req.json()) as ControlRequest;
if (!body.action) {
return c.json({ status: "error", message: "缺少 action 参数" }, 400);
}
const result = await controlMusic(body.action, body.position);
return c.json(result);
} catch (error) {
return c.json({ status: "error", message: String(error) }, 500);
}
});
// 获取歌词
app.get("/lyrics", async (c) => {
try {
const status = await getMusicStatus();
const lyricsData = await getLyricsData(status);
return c.json(lyricsData);
} catch (error) {
return c.json({ status: "error", error: String(error) }, 500);
}
});
// 搜索歌词
app.post("/lyrics/search", async (c) => {
try {
const body = (await c.req.json()) as SearchLyricsRequest;
if (!body.title) {
return c.json({ status: "error", message: "缺少 title 参数" }, 400);
}
// 这里可以实现歌词搜索逻辑
// 暂时返回空结果
return c.json({
status: "success",
lyrics: null,
message: "歌词搜索功能待实现",
});
} catch (error) {
return c.json({ status: "error", error: String(error) }, 500);
}
});
// 根据歌曲信息获取歌词
app.post("/lyrics/get", async (c) => {
try {
const body = (await c.req.json()) as GetLyricsFromIdRequest;
if (!body.title) {
return c.json({ status: "error", message: "缺少 title 参数" }, 400);
}
// 构造音乐状态对象
const mockStatus = {
status: "playing" as const,
track_name: body.title,
artist: body.artist || "",
position: 0,
duration: 0,
};
const lyricsData = await getLyricsData(mockStatus);
return c.json(lyricsData);
} catch (error) {
return c.json({ status: "error", error: String(error) }, 500);
}
});
// WebSocket 连接端点
app.get("/ws", (c) => {
const upgrade = c.req.header("upgrade");
if (upgrade !== "websocket") {
return c.text("Expected websocket", 400);
}
const { socket, response } = Deno.upgradeWebSocket(c.req.raw);
const connectionId = `conn_${Date.now()}_${Math.random()
.toString(36)
.substr(2, 9)}`;
// 添加连接到管理器
manager.addConnection(connectionId, socket);
return response;
});
// 404 处理
app.notFound((c) => {
return c.json({ status: "error", message: "Not Found" }, 404);
});
}