From 9850a3763d7f67ff4dc7fed9b4190f2b3a5235f7 Mon Sep 17 00:00:00 2001 From: shoopea Date: Wed, 17 Jun 2026 22:18:09 +0200 Subject: [PATCH] add destination api --- api.go | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++++ version.go | 8 +-- 2 files changed, 204 insertions(+), 4 deletions(-) diff --git a/api.go b/api.go index 5ab1888..2675871 100644 --- a/api.go +++ b/api.go @@ -693,6 +693,206 @@ func ApiAppSourceDel(c *gin.Context) { }) } +func ApiAppDestList(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.Destinations) + 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 ApiAppDestAdd(c *gin.Context) { + app := c.Param("app") + dst := c.Param("dst") + dst = dst[1:] // cut first char from wildcard match + + log.WithFields(log.Fields{"app": app, "dst": dst}).Debugf("starting") + log.WithFields(log.Fields{"app": app, "dst": dst}).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 + } + + d := Addr(dst) + if d.Box() == "" { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "destination box incorrect", + }) + return + } + + if _, ok := cfg.box[d.Box()]; !ok { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "destination box doesn't exist", + }) + return + } + + if d.Path() == "" { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "destination path incorrect", + }) + return + } + + if slices.Contains(ac.Destinations, d.String()) { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "destination already exists", + }) + return + } + + ac.Destinations = append(ac.Destinations, d.String()) + + 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 ApiAppDestDel(c *gin.Context) { + app := c.Param("app") + dst := c.Param("dst") + dst = dst[1:] // cut first char from wildcard match + + log.WithFields(log.Fields{"app": app, "dst": dst}).Debugf("starting") + log.WithFields(log.Fields{"app": app, "dst": dst}).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.Destinations, dst) { + c.JSON(http.StatusInternalServerError, gin.H{ + "message": "error", + "error": "destination does not exist", + }) + return + } + + for id, d := range ac.Destinations { + if d == dst { + ac.Destinations[id] = ac.Destinations[len(ac.Destinations)-1] + ac.Destinations = ac.Destinations[:len(ac.Destinations)-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 4cbfd82..6d81a3e 100644 --- a/version.go +++ b/version.go @@ -1,7 +1,7 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "1c2bac4" +var githash = "8fdaba6" var branch = "master" -var buildstamp = "2026-01-10_11:08:28" -var commits = "134" -var version = "1c2bac4-b134 - 2026-01-10_11:08:28" +var buildstamp = "2026-06-17_20:16:26" +var commits = "135" +var version = "8fdaba6-b135 - 2026-06-17_20:16:26"