reveil/api/alarm.go
Pierre-Olivier Mercier c8c6282216
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Refactor federation + can sync track between instances
2024-07-30 11:00:05 +02:00

77 lines
1.8 KiB
Go

package api
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"git.nemunai.re/nemunaire/reveil/config"
"git.nemunai.re/nemunaire/reveil/model"
"git.nemunai.re/nemunaire/reveil/player"
)
func declareAlarmRoutes(cfg *config.Config, router *gin.RouterGroup) {
router.GET("/alarm", func(c *gin.Context) {
if player.CommonPlayer == nil {
c.JSON(http.StatusOK, false)
} else {
c.JSON(http.StatusOK, true)
}
})
router.POST("/alarm/run", func(c *gin.Context) {
if player.CommonPlayer == nil {
err := player.WakeUp(cfg, nil, true)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
} else {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Player already running"})
return
}
c.JSON(http.StatusOK, true)
})
router.POST("/alarm/next", func(c *gin.Context) {
if player.CommonPlayer == nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "No player currently playing"})
return
} else {
player.CommonPlayer.NextTrack()
}
c.JSON(http.StatusOK, true)
})
router.DELETE("/alarm", func(c *gin.Context) {
if player.CommonPlayer != nil {
err := player.CommonPlayer.Stop()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
settings, err := reveil.ReadSettings(cfg.SettingsFile)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Errorf("Unable to read settings: %s", err.Error())})
return
}
for k, srv := range settings.Federation {
err = srv.WakeStop()
if err != nil {
log.Printf("Unable to do federated wakeup on %s: %s", k, err.Error())
} else {
log.Printf("Federated wakeup on %s: launched!", k)
}
}
}
c.JSON(http.StatusOK, true)
})
}