From c27c7caaaa5f49d041fb06d3491d2c4e20204ea1 Mon Sep 17 00:00:00 2001 From: shoopea Date: Wed, 17 Jun 2026 22:56:17 +0200 Subject: [PATCH] add app schedule api --- admin.go | 10 ++- api.go | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++- version.go | 8 +-- 3 files changed, 187 insertions(+), 10 deletions(-) diff --git a/admin.go b/admin.go index 7fa65ff..1aaa2ac 100644 --- a/admin.go +++ b/admin.go @@ -75,9 +75,13 @@ func (a *AdminConfig) Run() { r.GET("/app/:app/source/add/*src", ApiAppSourceAdd) r.GET("/app/:app/source/del/*src", ApiAppSourceDel) - r.GET("/app/:app/dest/list", ApiAppDestList) - r.GET("/app/:app/dest/add/*dst", ApiAppDestAdd) - r.GET("/app/:app/dest/del/*dst", ApiAppDestDel) + r.GET("/app/:app/dest/list", ApiAppDestinationList) + r.GET("/app/:app/dest/add/*dst", ApiAppDestinationAdd) + r.GET("/app/:app/dest/del/*dst", ApiAppDestinationDel) + + r.GET("/app/:app/schedule/list", ApiAppScheduleList) + r.GET("/app/:app/schedule/add/:schedule", ApiAppScheduleAdd) + r.GET("/app/:app/schedule/del/:schedule", ApiAppScheduleDel) r.GET("/box/list", ApiBoxList) r.GET("/box/add/:box", ApiBoxAdd) diff --git a/api.go b/api.go index 2675871..69e9bfb 100644 --- a/api.go +++ b/api.go @@ -693,7 +693,7 @@ func ApiAppSourceDel(c *gin.Context) { }) } -func ApiAppDestList(c *gin.Context) { +func ApiAppDestinationList(c *gin.Context) { log.WithFields(log.Fields{"app": c.Param("app")}).Debugf("starting") log.WithFields(log.Fields{"app": c.Param("app")}).Debugf("done") @@ -730,7 +730,7 @@ func ApiAppDestList(c *gin.Context) { c.Data(http.StatusOK, "application/json", pretty.PrettyOptions(b, &pretty.Options{Indent: " "})) } -func ApiAppDestAdd(c *gin.Context) { +func ApiAppDestinationAdd(c *gin.Context) { app := c.Param("app") dst := c.Param("dst") dst = dst[1:] // cut first char from wildcard match @@ -822,7 +822,7 @@ func ApiAppDestAdd(c *gin.Context) { } -func ApiAppDestDel(c *gin.Context) { +func ApiAppDestinationDel(c *gin.Context) { app := c.Param("app") dst := c.Param("dst") dst = dst[1:] // cut first char from wildcard match @@ -893,6 +893,179 @@ func ApiAppDestDel(c *gin.Context) { }) } +func ApiAppScheduleList(c *gin.Context) { + log.WithFields(log.Fields{"app": c.Param("app")}).Debugf("starting") + log.WithFields(log.Fields{"app": c.Param("app")}).Debugf("done") + + name := c.Param("app") + found := false + app := &AppConfig{} + + CfgLock() + defer CfgUnlock() + + for _, a := range cfg.Apps { + if a.Name == name { + found = true + app = a + } + } + + if !found { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "app does not exist", + }) + return + } + + b, err := json.Marshal(app.Schedule) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": fmt.Sprint(err), + }) + return + } + c.Data(http.StatusOK, "application/json", pretty.PrettyOptions(b, &pretty.Options{Indent: " "})) +} + +func ApiAppScheduleAdd(c *gin.Context) { + app := c.Param("app") + schedule := c.Param("schedule") + + log.WithFields(log.Fields{"app": app, "schedule": schedule}).Debugf("starting") + log.WithFields(log.Fields{"app": app, "schedule": schedule}).Debugf("done") + + found := false + ac := &AppConfig{} + + CfgLock() + defer CfgUnlock() + + for _, a := range cfg.Apps { + if a.Name == app { + found = true + ac = a + } + } + + if !found { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "app does not exist", + }) + return + } + + if slices.Contains(ac.Schedule, schedule) { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "schedule already exists", + }) + return + } + + ac.Schedule = append(ac.Schedule, schedule) + + if ac.Active { + a, err := cfg.NewApp(ac.Name, ac.Sources, ac.Destinations, ac.Schedule, ac.Before, ac.After) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": fmt.Sprint(err), + }) + return + } + + cfg.apps[a.name] = a + } + + err := cfg.Save(false) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": fmt.Sprint(err), + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "message": "done", + }) + +} + +func ApiAppScheduleDel(c *gin.Context) { + app := c.Param("app") + schedule := c.Param("schedule") + + log.WithFields(log.Fields{"app": app, "schedule": schedule}).Debugf("starting") + log.WithFields(log.Fields{"app": app, "schedule": schedule}).Debugf("done") + + found := false + ac := &AppConfig{} + + CfgLock() + defer CfgUnlock() + + for _, a := range cfg.Apps { + if a.Name == app { + found = true + ac = a + } + } + + if !found { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "app does not exist", + }) + return + } + + if !slices.Contains(ac.Schedule, schedule) { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "schedule does not exist", + }) + return + } + + for id, s := range ac.Schedule { + if s == schedule { + ac.Schedule[id] = ac.Schedule[len(ac.Schedule)-1] + ac.Schedule = ac.Schedule[:len(ac.Schedule)-1] + } + } + + if ac.Active { + a, err := cfg.NewApp(ac.Name, ac.Sources, ac.Destinations, ac.Schedule, ac.Before, ac.After) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": fmt.Sprint(err), + }) + return + } + + cfg.apps[a.name] = a + } + + err := cfg.Save(false) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": fmt.Sprint(err), + }) + return + } + + c.JSON(http.StatusOK, gin.H{ + "message": "done", + }) +} + func ApiBoxList(c *gin.Context) { log.WithFields(log.Fields{}).Debugf("starting") log.WithFields(log.Fields{}).Debugf("done") diff --git a/version.go b/version.go index 6d81a3e..b9e29b9 100644 --- a/version.go +++ b/version.go @@ -1,7 +1,7 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "8fdaba6" +var githash = "fef11b6" var branch = "master" -var buildstamp = "2026-06-17_20:16:26" -var commits = "135" -var version = "8fdaba6-b135 - 2026-06-17_20:16:26" +var buildstamp = "2026-06-17_20:54:42" +var commits = "137" +var version = "fef11b6-b137 - 2026-06-17_20:54:42"