app activate/deactivate

This commit is contained in:
shoopea
2025-12-28 16:15:25 +01:00
parent 1890050a30
commit 6a0c8006d6
4 changed files with 107 additions and 24 deletions

82
api.go
View File

@@ -144,6 +144,10 @@ func ApiConfig(c *gin.Context) {
func ApiConfigApp(c *gin.Context) {
name := c.Param("app")
found := false
CfgLock()
defer CfgUnlock()
for _, app := range cfg.Apps {
if app.Name == name {
found = true
@@ -167,8 +171,12 @@ func ApiConfigApp(c *gin.Context) {
func ApiAppList(c *gin.Context) {
list := make([]string, 0)
for _, app := range cfg.apps {
list = append(list, app.name)
CfgLock()
defer CfgUnlock()
for _, app := range cfg.Apps {
list = append(list, app.Name)
}
slices.Sort(list)
@@ -187,11 +195,16 @@ func ApiAppList(c *gin.Context) {
func ApiAppAdd(c *gin.Context) {
name := c.Param("app")
found := false
CfgLock()
defer CfgUnlock()
for _, app := range cfg.Apps {
if app.Name == name {
found = true
}
}
if found {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
@@ -199,6 +212,7 @@ func ApiAppAdd(c *gin.Context) {
})
return
}
app := &AppConfig{
Name: name,
Active: false,
@@ -220,6 +234,10 @@ func ApiAppAdd(c *gin.Context) {
func ApiAppDel(c *gin.Context) {
name := c.Param("app")
found := false
CfgLock()
defer CfgUnlock()
for id, app := range cfg.Apps {
if app.Name == name {
if app.Active {
@@ -245,3 +263,63 @@ func ApiAppDel(c *gin.Context) {
"message": "done",
})
}
func ApiAppActivate(c *gin.Context) {
name := c.Param("app")
CfgLock()
defer CfgUnlock()
for _, app := range cfg.Apps {
if app.Name == name {
if app.Active {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
"error": "app already active",
})
return
}
a, err := cfg.NewApp(app.Name, app.Sources, app.Destinations, app.Schedule, app.Before, app.After)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
"error": err,
})
return
}
cfg.apps[app.Name] = a
}
}
c.JSON(http.StatusOK, gin.H{
"message": "done",
})
}
func ApiAppDeactivate(c *gin.Context) {
name := c.Param("app")
CfgLock()
defer CfgUnlock()
if _, ok := cfg.apps[name]; ok {
delete(cfg.apps, name)
} else {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "error",
"error": "app is not active",
})
return
}
for _, app := range cfg.Apps {
if app.Name == name {
app.Active = false
}
}
c.JSON(http.StatusOK, gin.H{
"message": "done",
})
}