2022-11-18 21:18:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-11-21 23:33:43 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2022-11-18 21:18:12 +00:00
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-11-18 22:44:10 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2022-11-18 21:18:12 +00:00
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
)
|
|
|
|
|
2022-11-18 22:44:10 +00:00
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
func init() {
|
|
|
|
app.router = httprouter.New()
|
2022-11-18 22:44:10 +00:00
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
app.router.GET("/", app.Index)
|
|
|
|
app.router.GET("/play/:playlist", app.Play)
|
|
|
|
app.router.GET("/reset", app.Reset)
|
2022-11-21 23:33:43 +00:00
|
|
|
app.router.GET("/public/*js", app.ServeFiles)
|
|
|
|
app.router.GET("/css/*css", app.ServeFiles)
|
2022-11-18 21:18:12 +00:00
|
|
|
|
2022-11-18 22:44:10 +00:00
|
|
|
app.router.HandlerFunc("GET", "/ws", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("WS connection from %v\n", r.RemoteAddr)
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handleWS(conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("WS connection closed, %v\n", r.RemoteAddr)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
go func() {
|
|
|
|
log.Fatal(http.ListenAndServe(":8824", app.router))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
type IndexData struct {
|
|
|
|
Playlists []Playlist
|
2022-11-25 13:06:26 +00:00
|
|
|
Ambiance []Ambiance
|
2022-11-18 21:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 23:33:43 +00:00
|
|
|
func (app App) ServeFiles(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
filePath := filepath.Join(".", r.URL.Path)
|
|
|
|
|
|
|
|
file, err := os.Open(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, "no such file", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
fileStat, err := os.Stat(filePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, "unable to get file stat", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, filename := path.Split(filePath)
|
|
|
|
t := fileStat.ModTime()
|
|
|
|
http.ServeContent(w, r, filename, t, file)
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:18:12 +00:00
|
|
|
func (app App) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
playlists, err := app.GetPlaylists()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Unable to get playlists. "+err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:06:26 +00:00
|
|
|
amblist, err := GetAmbiances()
|
2022-11-18 21:18:12 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data := IndexData{playlists, amblist}
|
|
|
|
|
|
|
|
t := template.Must(template.New("index.tmpl").ParseFiles("tmpl/index.tmpl"))
|
|
|
|
err = t.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Unable to load template. "+err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Play(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
plname := p.ByName("playlist")
|
|
|
|
|
|
|
|
if plname == "reset" {
|
|
|
|
app.events.Emit("stop", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
plid, err := uuid.ParseBytes([]byte(plname))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Unable to parse uuid. "+err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
app.events.Emit("new_playlist", plid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Add(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *App) Reset(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
app.events.Emit("stop", nil)
|
|
|
|
}
|