add destination api

This commit is contained in:
shoopea
2026-06-17 22:18:09 +02:00
parent 8fdaba6fc7
commit 9850a3763d
2 changed files with 204 additions and 4 deletions
+200
View File
@@ -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")