Able to edit settings in interface
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
25b896c41d
commit
8df05d57c7
14 changed files with 538 additions and 2 deletions
|
|
@ -4,12 +4,18 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.nemunai.re/nemunaire/hathoris/config"
|
||||
"git.nemunai.re/nemunaire/hathoris/settings"
|
||||
)
|
||||
|
||||
func DeclareRoutes(router *gin.Engine, cfg *config.Config) {
|
||||
type SettingsGetter func() *settings.Settings
|
||||
|
||||
type SettingsReloader func() error
|
||||
|
||||
func DeclareRoutes(router *gin.Engine, cfg *config.Config, getsettings SettingsGetter, reloadsettings SettingsReloader) {
|
||||
apiRoutes := router.Group("/api")
|
||||
|
||||
declareInputsRoutes(cfg, apiRoutes)
|
||||
declareSettingsRoutes(cfg, getsettings, reloadsettings, apiRoutes)
|
||||
declareSourcesRoutes(cfg, apiRoutes)
|
||||
declareVolumeRoutes(cfg, apiRoutes)
|
||||
}
|
||||
|
|
|
|||
64
api/settings.go
Normal file
64
api/settings.go
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.nemunai.re/nemunaire/hathoris/config"
|
||||
"git.nemunai.re/nemunaire/hathoris/settings"
|
||||
"git.nemunai.re/nemunaire/hathoris/sources"
|
||||
)
|
||||
|
||||
type loadableSourceExposed struct {
|
||||
Description string `json:"description"`
|
||||
Fields []*sources.SourceField `json:"fields"`
|
||||
}
|
||||
|
||||
func declareSettingsRoutes(cfg *config.Config, getsettings SettingsGetter, reloadsettings SettingsReloader, router *gin.RouterGroup) {
|
||||
router.GET("/settings", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, getsettings())
|
||||
})
|
||||
|
||||
router.POST("/settings", func(c *gin.Context) {
|
||||
var params settings.Settings
|
||||
|
||||
// Parse settings
|
||||
err := c.ShouldBindJSON(¶ms)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": fmt.Sprintf("Something is wrong in received settings: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
// Reload current settings
|
||||
*getsettings() = params
|
||||
|
||||
// Save settings
|
||||
getsettings().Save(cfg.SettingsPath)
|
||||
|
||||
err = reloadsettings()
|
||||
if err != nil {
|
||||
log.Println("Unable to reload settings:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to reload settings: %s", err.Error())})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, getsettings())
|
||||
})
|
||||
|
||||
router.GET("/settings/loadable_sources", func(c *gin.Context) {
|
||||
ret := map[string]loadableSourceExposed{}
|
||||
|
||||
for k, ls := range sources.LoadableSources {
|
||||
ret[k] = loadableSourceExposed{
|
||||
Description: ls.Description,
|
||||
Fields: sources.GenFields(ls.SourceDefinition),
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, ret)
|
||||
})
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue