sanic/server.go

148 lines
3.7 KiB
Go
Raw Normal View History

2023-10-19 18:01:48 +02:00
package main
import (
2023-12-02 17:53:44 +01:00
"fmt"
2023-10-24 18:17:36 +02:00
"github.com/labstack/echo-contrib/echoprometheus"
2023-12-02 17:53:44 +01:00
"github.com/labstack/echo/v4"
2023-10-24 18:17:36 +02:00
"github.com/labstack/echo/v4/middleware"
2023-12-26 17:23:53 +01:00
"gopkg.in/ini.v1"
2023-10-19 18:01:48 +02:00
"net/http"
2023-12-10 21:33:54 +01:00
"os"
"os/exec"
2023-10-19 18:01:48 +02:00
)
2024-07-15 10:51:53 +02:00
// Config holds the configuration for the mpd connection and for the web server.
2023-12-26 17:23:53 +01:00
type Config struct {
MPD struct {
Hostname string `ini:"hostname"`
Port int `ini:"port"`
Username string `ini:"username"`
Password string `ini:"password"`
} `ini:"mpd"`
UI struct {
Hostname string `ini:"hostname"`
Port int `ini:"port"`
Tls bool `ini:"tls"`
Certificate string `ini:"cert"`
Key string `ini:"key"`
} `ini:"ui"`
}
2023-10-19 18:01:48 +02:00
func main() {
2023-12-26 17:23:53 +01:00
iniData, err := ini.Load("config.ini")
if err != nil {
fmt.Printf("Fail to read configuration file: %v", err)
os.Exit(1)
}
var config Config
err = iniData.MapTo(&config)
if err != nil {
fmt.Printf("Fail to parse configuration file: %v", err)
os.Exit(1)
}
2023-10-19 18:01:48 +02:00
e := echo.New()
2023-10-24 18:17:36 +02:00
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.Gzip())
e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "static",
HTML5: true, // SPA mode; not-found will be redirected to root
}))
e.Use(echoprometheus.NewMiddleware("sanic")) // adds middleware to gather metrics
e.GET("/metrics", echoprometheus.NewHandler())
e.GET("/", func(c echo.Context) (err error) {
2023-12-02 17:53:44 +01:00
// HTTP/2 Server Push
2023-10-24 18:17:36 +02:00
pusher, ok := c.Response().Writer.(http.Pusher)
if ok {
if err = pusher.Push("/style.css", nil); err != nil {
return
}
if err = pusher.Push("/index.js", nil); err != nil {
return
}
if err = pusher.Push("/favicon.ico", nil); err != nil {
return
}
}
return c.File("index.html")
})
2024-08-07 23:35:16 +02:00
// echo back request to check if HTTP/2 works etc
e.GET("/echo", func(c echo.Context) error {
req := c.Request()
format := `
<code>
Protocol: %s<br>
Host: %s<br>
Remote Address: %s<br>
Method: %s<br>
Path: %s<br>
</code>
`
return c.HTML(http.StatusOK, fmt.Sprintf(format, req.Proto, req.Host, req.RemoteAddr, req.Method, req.URL.Path))
})
2023-12-02 17:53:44 +01:00
g := e.Group("/api")
2023-12-29 16:55:06 +01:00
g.GET("/update_db", updateDb)
2023-12-02 17:53:44 +01:00
g.GET("/previous_track", previousTrack)
g.GET("/next_track", nextTrack)
g.GET("/stop", stopPlayback)
g.GET("/play", resumePlayback)
g.GET("/pause", pausePlayback)
g.GET("/seek/:seconds", seek)
g.GET("/repeat", toggleRepeat)
g.GET("/random", toggleRandom)
g.GET("/volume/:level", setVolume)
2024-04-06 18:57:43 +02:00
g.GET("/queue/:song_id/delete", deleteTrackFromQueue)
g.GET("/queue/:song_id/move/:position", moveTrackInQueue)
2024-04-18 11:46:09 +02:00
g.GET("/queue/replace/:playlist_name", replaceQueue)
2024-04-18 12:12:52 +02:00
g.GET("/queue/attach/:playlist_name", attachPlaylist)
2024-04-06 15:28:00 +02:00
2024-04-16 23:37:48 +02:00
g.GET("/playlists", listPlaylists)
2024-04-18 15:06:12 +02:00
g.POST("/playlists/:name", savePlaylist)
2024-04-17 00:24:18 +02:00
g.GET("/playlists/:name", listPlaylist)
2024-04-18 14:28:02 +02:00
g.DELETE("/playlists/:name", deletePlaylist)
2024-04-16 23:37:48 +02:00
2024-04-29 00:14:39 +02:00
g.GET("/database/:pattern", searchDatabase)
2023-12-10 21:33:54 +01:00
g.GET("/download", downloadTrack)
2024-08-07 23:35:16 +02:00
e.GET("/sse", serveSSE)
2023-11-07 21:16:42 +01:00
2023-12-26 17:23:53 +01:00
if config.UI.Tls {
e.Logger.Fatal(e.StartTLS(fmt.Sprintf("%s:%d", config.UI.Hostname, config.UI.Port), config.UI.Certificate, config.UI.Key))
} else {
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", config.UI.Hostname, config.UI.Port)))
}
2023-10-19 18:01:48 +02:00
}
2023-10-24 18:17:36 +02:00
2024-07-15 10:51:53 +02:00
// downloadTrack tries to download a given URL and saves the song to the database.
2023-12-10 21:33:54 +01:00
func downloadTrack(c echo.Context) error {
cmd := exec.Command(
"yt-dlp",
"--no-wait-for-video",
"--no-playlist",
"--windows-filenames",
"--newline",
"--extract-audio",
"--audio-format", "mp3",
"--audio-quality", "0",
"--format", "bestaudio/best",
c.Param("url"),
)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
2024-08-07 23:35:16 +02:00
c.Logger().Fatal(err)
2023-12-10 21:33:54 +01:00
}
return c.String(http.StatusAccepted, "")
2023-12-02 17:53:44 +01:00
}