import { getRequestURL } from "h3"; import db from "../db"; export default defineEventHandler((event) => { const requestUrl = getRequestURL(event); const siteUrl = requestUrl.origin; // 查询所有 playable = 1 的歌曲 const songs = db.prepare( "SELECT id, filename FROM songs WHERE playable = 1" ).all() as Array<{ id: number; filename: string }>; // 生成 XML 内容 const urls: string[] = []; // 添加首页 urls.push(` ${siteUrl}/ daily 1.0 `); // 循环添加歌曲页面 for (const song of songs) { const encodedName = encodeURIComponent(song.filename); const loc = `${siteUrl}/song/${song.id}/${encodedName}`; urls.push(` ${loc} weekly 0.6 `); } const sitemapXml = ` ${urls.join("")} `; setHeader(event, "Content-Type", "application/xml; charset=utf-8"); return sitemapXml; });