reveil/api/settings.go

39 lines
922 B
Go
Raw Permalink Normal View History

2022-10-02 21:24:53 +00:00
package api
import (
2022-10-05 18:16:53 +00:00
"net/http"
2022-10-02 21:24:53 +00:00
"github.com/gin-gonic/gin"
"git.nemunai.re/nemunaire/reveil/config"
2022-10-05 18:16:53 +00:00
"git.nemunai.re/nemunaire/reveil/model"
2022-10-02 21:24:53 +00:00
)
func declareSettingsRoutes(cfg *config.Config, router *gin.RouterGroup) {
router.GET("/settings", func(c *gin.Context) {
2022-10-05 18:16:53 +00:00
settings, err := reveil.ReadSettings(cfg.SettingsFile)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-05 18:16:53 +00:00
c.JSON(http.StatusOK, settings)
2022-10-02 21:24:53 +00:00
})
router.PUT("/settings", func(c *gin.Context) {
2022-10-05 18:16:53 +00:00
var config reveil.Settings
err := c.ShouldBindJSON(&config)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
err = reveil.SaveSettings(cfg.SettingsFile, config)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return
}
2022-10-02 21:24:53 +00:00
2022-10-05 18:16:53 +00:00
c.JSON(http.StatusOK, config)
2022-10-02 21:24:53 +00:00
})
}