123 lines
2.6 KiB
Go
123 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"text/template"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/gorilla/websocket"
|
|
"github.com/julienschmidt/httprouter"
|
|
)
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
ReadBufferSize: 1024,
|
|
WriteBufferSize: 1024,
|
|
}
|
|
|
|
var router *httprouter.Router
|
|
|
|
func init() {
|
|
router = httprouter.New()
|
|
|
|
router.GET("/", Index)
|
|
router.GET("/play/:playlist", Play)
|
|
router.GET("/reset", Reset)
|
|
router.GET("/public/*js", ServeFiles)
|
|
router.GET("/css/*css", ServeFiles)
|
|
|
|
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)
|
|
}
|
|
}))
|
|
|
|
go func() {
|
|
log.Fatal(http.ListenAndServe(":8824", router))
|
|
}()
|
|
}
|
|
|
|
type IndexData struct {
|
|
Playlists []Playlist
|
|
Ambiance []Ambiance
|
|
}
|
|
|
|
func 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)
|
|
}
|
|
|
|
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
playlists, err := GetPlaylists()
|
|
if err != nil {
|
|
http.Error(w, "Unable to get playlists. "+err.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
amblist, err := GetAmbiances()
|
|
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 Play(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
plname := p.ByName("playlist")
|
|
|
|
if plname == "reset" {
|
|
ev.Emit("stop", nil)
|
|
return
|
|
}
|
|
|
|
plid, err := uuid.ParseBytes([]byte(plname))
|
|
if err != nil {
|
|
http.Error(w, "Unable to parse uuid. "+err.Error(), http.StatusInternalServerError)
|
|
}
|
|
|
|
ev.Emit("new_playlist", plid)
|
|
}
|
|
|
|
func Add(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
r.ParseForm()
|
|
|
|
}
|
|
|
|
func Reset(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
ev.Emit("stop", nil)
|
|
}
|