Implement Tuner v1 — Go backend, React frontend, Docker setup

- Go backend: REST API, M3U parser, FFmpeg/MediaMTX process manager
- React/Vite frontend: HLS player, admin panel, channel browser (dark theme)
- MediaMTX config for RTMP ingest + HLS output
- Multi-stage Dockerfile (Go + Bun + Alpine runtime)
- docker-compose.yml for single-container deployment
- Sample M3U playlist with test streams

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 09:47:32 -08:00
parent 2e66b8c73d
commit b8bfcefee8
33 changed files with 2257 additions and 0 deletions

124
backend/main.go Normal file
View File

@@ -0,0 +1,124 @@
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"tuner/api"
"tuner/m3u"
"tuner/process"
)
func getEnv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}
func main() {
playlistPath := getEnv("PLAYLIST_PATH", "/data/playlist.m3u")
mediamtxBin := getEnv("MEDIAMTX_PATH", "mediamtx")
mediamtxCfg := getEnv("MEDIAMTX_CONFIG", "mediamtx.yml")
port := getEnv("RESTREAMER_PORT", "8080")
// Initialize process managers
mtxManager := process.NewMediaMTXManager(mediamtxBin, mediamtxCfg)
ffmpegManager := process.NewFFmpegManager()
// Build shared app state
app := &api.App{
PlaylistPath: playlistPath,
MediaMTX: mtxManager,
FFmpeg: ffmpegManager,
}
// Load playlist (warn but don't crash if missing)
channels, err := m3u.ParseFile(playlistPath)
if err != nil {
log.Printf("[startup] warning: could not load playlist %s: %v", playlistPath, err)
} else {
app.Channels = channels
log.Printf("[startup] loaded %d channels from %s", len(channels), playlistPath)
}
// Start MediaMTX (warn but don't crash if binary not found)
if err := mtxManager.Start(); err != nil {
log.Printf("[startup] warning: could not start mediamtx: %v", err)
}
// Register routes
mux := http.NewServeMux()
// API routes
mux.HandleFunc("/api/status", app.HandleStatus)
mux.HandleFunc("/api/admin/channels", app.HandleChannels)
mux.HandleFunc("/api/admin/groups", app.HandleGroups)
mux.HandleFunc("/api/admin/source", app.HandleSetSource)
mux.HandleFunc("/api/admin/channel", app.HandleSetChannel)
mux.HandleFunc("/api/admin/playlist/reload", app.HandleReloadPlaylist)
mux.HandleFunc("/api/admin/process/status", app.HandleProcessStatus)
// Serve frontend static files (falls back to index.html for SPA routing)
frontendDir := getEnv("FRONTEND_DIR", "frontend/dist")
if info, err := os.Stat(frontendDir); err == nil && info.IsDir() {
fs := http.FileServer(http.Dir(frontendDir))
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Try to serve the file; if it doesn't exist, serve index.html (SPA fallback)
path := frontendDir + r.URL.Path
if _, err := os.Stat(path); os.IsNotExist(err) && r.URL.Path != "/" {
http.ServeFile(w, r, frontendDir+"/index.html")
return
}
fs.ServeHTTP(w, r)
}))
log.Printf("[startup] serving frontend from %s", frontendDir)
} else {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Tuner backend running. Frontend not found."))
})
log.Printf("[startup] frontend dir %s not found, serving placeholder", frontendDir)
}
// Wrap with middleware
handler := api.Logging(api.CORS(mux))
server := &http.Server{
Addr: ":" + port,
Handler: handler,
}
// Graceful shutdown
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigCh
log.Printf("[shutdown] received %v, shutting down...", sig)
// Stop child processes
if err := ffmpegManager.Stop(); err != nil {
log.Printf("[shutdown] error stopping ffmpeg: %v", err)
}
if err := mtxManager.Stop(); err != nil {
log.Printf("[shutdown] error stopping mediamtx: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Printf("[shutdown] http server shutdown error: %v", err)
}
}()
log.Printf("[startup] listening on :%s", port)
if err := server.ListenAndServe(); err != http.ErrServerClosed {
log.Fatalf("[fatal] server error: %v", err)
}
log.Println("[shutdown] complete")
}