feat: add FFmpeg status check and shutdown button

- FFmpeg check no longer blocks server startup
- Add /api/checkFFmpeg endpoint to query FFmpeg availability
- Add /api/shutdown endpoint to gracefully stop the server
- Add FFmpeg status indicator in web header
- Add shutdown button in web interface
- Move static files output to cmd/bilidown/static for embedding
- Update .gitignore to exclude cmd/bilidown/static

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-07 15:27:55 +08:00
parent dd8eaef97d
commit eb662a8b66
7 changed files with 69 additions and 37 deletions
+24 -10
View File
@@ -15,7 +15,7 @@ import (
_ "modernc.org/sqlite"
)
//go:embed all:build/static
//go:embed all:static
var staticFiles embed.FS
const (
@@ -25,34 +25,48 @@ const (
)
var urlLocal = fmt.Sprintf("http://127.0.0.1:%d", HTTP_PORT)
var ffmpegAvailable bool
func main() {
checkFFmpeg()
ffmpegAvailable = checkFFmpeg()
mustInitTables()
mustRunServer()
fmt.Printf("Bilidown %s server running at %s\n", VERSION, urlLocal)
if !ffmpegAvailable {
fmt.Println("Warning: FFmpeg is not installed. Video download will not work.")
}
select {} // 保持运行
}
// checkFFmpeg 检测 ffmpeg 的安装情况
func checkFFmpeg() {
if _, err := util.GetFFmpegPath(); err != nil {
fmt.Println("FFmpeg is missing. Install it from https://www.ffmpeg.org/download.html, then restart.")
os.Exit(1)
}
// checkFFmpeg 检测 ffmpeg 的安装情况,返回是否可用
func checkFFmpeg() bool {
_, err := util.GetFFmpegPath()
return err == nil
}
// mustRunServer 配置和启动 HTTP 服务器
func mustRunServer() {
// 从嵌入的文件系统中获取静态文件
staticFS, err := fs.Sub(staticFiles, "build/static")
staticFS, err := fs.Sub(staticFiles, "static")
if err != nil {
log.Fatal("Failed to load static files:", err)
}
// 前端静态文件
http.Handle("/", http.FileServer(http.FS(staticFS)))
// 后端 API 接口
http.Handle("/api/", http.StripPrefix("/api", router.API()))
apiRouter := router.API()
// 添加 FFmpeg 状态检查接口
http.HandleFunc("/api/checkFFmpeg", func(w http.ResponseWriter, r *http.Request) {
util.Res{Success: true, Data: map[string]bool{"available": ffmpegAvailable}}.Write(w)
})
// 添加关闭服务接口
http.HandleFunc("/api/shutdown", func(w http.ResponseWriter, r *http.Request) {
util.Res{Success: true, Message: "Shutting down..."}.Write(w)
go func() {
os.Exit(0)
}()
})
http.Handle("/api/", http.StripPrefix("/api", apiRouter))
// 启动 HTTP 服务器
go func() {
err := http.ListenAndServe(fmt.Sprintf("%s:%d", HTTP_HOST, HTTP_PORT), nil)