update box api

This commit is contained in:
shoopea
2026-01-10 11:33:33 +01:00
parent 3aa0a852a2
commit ba418edc51
4 changed files with 153 additions and 4 deletions

136
api.go
View File

@@ -634,3 +634,139 @@ func ApiAppSourceDel(c *gin.Context) {
"message": "done",
})
}
func ApiBoxList(c *gin.Context) {
log.WithFields(log.Fields{}).Debugf("starting")
log.WithFields(log.Fields{}).Debugf("done")
list := make([]string, 0)
CfgLock()
defer CfgUnlock()
for name, _ := range cfg.Box {
list = append(list, name)
}
slices.Sort(list)
b, err := json.Marshal(list)
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 ApiBoxAdd(c *gin.Context) {
log.WithFields(log.Fields{"box": c.Param("box")}).Debugf("starting")
log.WithFields(log.Fields{"box": c.Param("box")}).Debugf("done")
name := c.Param("box")
CfgLock()
defer CfgUnlock()
if _, ok := cfg.Box[name]; ok {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
"error": "box already exist",
})
return
}
box := &BoxConfig{}
cfg.Box[name] = box
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 ApiBoxDel(c *gin.Context) {
log.WithFields(log.Fields{"box": c.Param("box")}).Debugf("starting")
log.WithFields(log.Fields{"box": c.Param("box")}).Debugf("done")
name := c.Param("box")
CfgLock()
defer CfgUnlock()
if _, ok := cfg.Box[name]; !ok {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
"error": "no box found",
})
return
}
for _, app := range cfg.Apps {
for _, src := range app.Sources {
a := Addr(src)
if a.Box() == name {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
"error": fmt.Sprintf("box used in %s", app.Name),
})
return
}
}
for _, dst := range app.Destinations {
a := Addr(dst)
if a.Box() == name {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
"error": fmt.Sprintf("box used in %s", app.Name),
})
return
}
}
for _, bfr := range app.Before {
a := Addr(bfr)
if a.Box() == name {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
"error": fmt.Sprintf("box used in %s", app.Name),
})
return
}
}
for _, atr := range app.After {
a := Addr(atr)
if a.Box() == name {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
"error": fmt.Sprintf("box used in %s", app.Name),
})
return
}
}
}
delete(cfg.Box, name)
delete(cfg.box, name)
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",
})
}