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

View File

@@ -65,6 +65,9 @@ func (a *AdminConfig) Run() {
r.GET("/app/add/:app", ApiAppAdd)
r.GET("/app/del/:app", ApiAppDel)
r.GET("/app/activate/:app", ApiAppActivate)
r.GET("/app/deactivate/:app", ApiAppDeactivate)
srv := &http.Server{
Addr: a.Addr,
Handler: r,

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

View File

@@ -170,6 +170,7 @@ func LoadConfigByte(conf []byte) (*Config, error) {
c.apps = make(map[string]*App)
for _, v := range c.Apps {
if v.Active {
if a, err := c.NewApp(v.Name, v.Sources, v.Destinations, v.Schedule, v.Before, v.After); err != nil {
log.WithFields(log.Fields{"call": "NewApp", "attr": v.Name, "error": err}).Errorf("")
return nil, err
@@ -196,6 +197,7 @@ func LoadConfigByte(conf []byte) (*Config, error) {
}
}
}
}
return c, nil
}

View File

@@ -1,7 +1,7 @@
// Code generated by version.sh (@generated) DO NOT EDIT.
package main
var githash = "3e867da"
var githash = "1890050"
var branch = "master"
var buildstamp = "2025-12-27_20:59:37"
var commits = "118"
var version = "3e867da-b118 - 2025-12-27_20:59:37"
var buildstamp = "2025-12-28_15:14:58"
var commits = "119"
var version = "1890050-b119 - 2025-12-28_15:14:58"