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

9
.gitignore vendored
View File

@@ -1,3 +1,12 @@
config.json
backup
backup.json
tmp/server_test.go
tmp/server.go
tmp/static/script.js
tmp/static/style.css
tmp/templates/app_form.html
tmp/templates/apps.html
tmp/templates/box.html
tmp/templates/email.html
tmp/templates/login.html

View File

@@ -73,6 +73,10 @@ func (a *AdminConfig) Run() {
r.GET("/app/:app/source/add/*src", ApiAppSourceAdd)
r.GET("/app/:app/source/del/*src", ApiAppSourceDel)
r.GET("/box/list", ApiBoxList)
r.GET("/box/add/:box", ApiBoxAdd)
r.GET("/box/del/:box", ApiBoxDel)
srv := &http.Server{
Addr: a.Addr,
Handler: r,

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",
})
}

View File

@@ -1,7 +1,7 @@
// Code generated by version.sh (@generated) DO NOT EDIT.
package main
var githash = "54d8576"
var githash = "3aa0a85"
var branch = "master"
var buildstamp = "2025-12-28_21:17:45"
var commits = "129"
var version = "54d8576-b129 - 2025-12-28_21:17:45"
var buildstamp = "2026-01-10_10:33:26"
var commits = "130"
var version = "3aa0a85-b130 - 2026-01-10_10:33:26"