package main import ( "encoding/json" "net/http" "time" "github.com/gin-gonic/gin" "github.com/tidwall/pretty" ) func ApiRun(c *gin.Context) { cfg.Run() c.JSON(http.StatusOK, gin.H{ "message": "done", }) } func ApiSnapshotAdd(c *gin.Context) { if app, ok := cfg.apps[c.Param("app")]; ok { schedule := c.Param("schedule") if _, ok := app.schedule[schedule]; ok { if err := app.RunStandaloneSchedule(schedule); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": err, }) } else { c.JSON(http.StatusOK, gin.H{ "message": "done", }) } } else { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "no schedule found", }) } } else { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "no app found", }) } } func ApiSnapshotDel(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "not implemented", }) } func ApiSnapshotList(c *gin.Context) { if app, ok := cfg.apps[c.Param("app")]; ok { if snapshots, err := app.Snapshots(); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": err, }) } else { c.JSON(http.StatusOK, gin.H{ "message": "done", "snapshots": snapshots, }) } } else { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "no app found", }) } } func ApiScheduleAdd(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "not implemented", }) } func ApiScheduleDel(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "not implemented", }) } func ApiScheduleList(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "not implemented", }) } func ApiRunApp(c *gin.Context) { if app, ok := cfg.apps[c.Param("app")]; ok { if err := app.RunStandaloneTime(time.Now()); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": err, }) } else { c.JSON(http.StatusOK, gin.H{ "message": "done", }) } } else { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "no app found", }) } } func ApiSave(c *gin.Context) { if err := cfg.Save(); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": err, }) } else { c.JSON(http.StatusOK, gin.H{ "message": "done", }) } } func ApiConfig(c *gin.Context) { if b, err := cfg.Pretty(); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": err, }) } else { c.Data(http.StatusOK, "application/json", b) } } func ApiConfigApp(c *gin.Context) { name := c.Param("app") found := false for _, app := range cfg.Apps { if app.Name == name { found = true if b, err := app.Pretty(); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": err, }) } else { c.Data(http.StatusOK, "application/json", b) } } } if !found { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "no app found", }) } } func ApiAppList(c *gin.Context) { list := make([]string, 0) for _, app := range cfg.apps { list = append(list, app.name) } b, err := json.Marshal(list) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": err, }) return } c.Data(http.StatusOK, "application/json", pretty.PrettyOptions(b, &pretty.Options{Indent: " "})) } func ApiAppAdd(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "not implemented", }) } func ApiAppDel(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{ "message": "error", "error": "not implemented", }) }