76 lines
1.9 KiB
Go
76 lines
1.9 KiB
Go
package sonar
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
// Register the routes.
|
|
func (s *Sonar) Register() *mux.Router {
|
|
r := mux.NewRouter()
|
|
|
|
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
templ, err := template.ParseFiles("./www/index.gohtml")
|
|
if err != nil {
|
|
log.Error(err.Error())
|
|
}
|
|
|
|
flashes := s.GetFlashes(r, w)
|
|
|
|
v := map[string]interface{}{
|
|
"Flashes": flashes,
|
|
"Volume": s.Volume(),
|
|
"PlaylistStatus": s.Player.Status(),
|
|
}
|
|
templ.ExecuteTemplate(w, "index.gohtml", v)
|
|
})
|
|
|
|
// API functions
|
|
POST := r.Methods("GET", "POST").Subrouter()
|
|
POST.HandleFunc("/playlist/start", func(w http.ResponseWriter, r *http.Request) {
|
|
err := s.StartPlaylist()
|
|
if err != nil {
|
|
log.Info("Start: %s", err.Error())
|
|
s.FlashAndRedirect(r, w, "/", "Error: "+err.Error())
|
|
} else {
|
|
s.FlashAndRedirect(r, w, "/", "Playlist started!")
|
|
}
|
|
})
|
|
POST.HandleFunc("/playlist/stop", func(w http.ResponseWriter, r *http.Request) {
|
|
err := s.StopPlaylist()
|
|
if err != nil {
|
|
s.FlashAndRedirect(r, w, "/", "Error: "+err.Error())
|
|
} else {
|
|
s.FlashAndRedirect(r, w, "/", "Playlist stopped!")
|
|
}
|
|
})
|
|
POST.HandleFunc("/volume/higher", func(w http.ResponseWriter, r *http.Request) {
|
|
err := s.VolumeUp()
|
|
if err != nil {
|
|
s.FlashAndRedirect(r, w, "/", "Error:"+err.Error())
|
|
} else {
|
|
s.FlashAndRedirect(r, w, "/", "Volume increased!")
|
|
}
|
|
})
|
|
POST.HandleFunc("/volume/lower", func(w http.ResponseWriter, r *http.Request) {
|
|
err := s.VolumeDown()
|
|
if err != nil {
|
|
s.FlashAndRedirect(r, w, "/", "Error:"+err.Error())
|
|
} else {
|
|
s.FlashAndRedirect(r, w, "/", "Volume lowered!")
|
|
}
|
|
})
|
|
POST.HandleFunc("/volume/mute", func(w http.ResponseWriter, r *http.Request) {
|
|
err := s.VolumeMute()
|
|
if err != nil {
|
|
s.FlashAndRedirect(r, w, "/", "Error:"+err.Error())
|
|
} else {
|
|
s.FlashAndRedirect(r, w, "/", "Volume mute toggled!")
|
|
}
|
|
})
|
|
|
|
return r
|
|
}
|