add app schedule api
This commit is contained in:
@@ -693,7 +693,7 @@ func ApiAppSourceDel(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func ApiAppDestList(c *gin.Context) {
|
||||
func ApiAppDestinationList(c *gin.Context) {
|
||||
log.WithFields(log.Fields{"app": c.Param("app")}).Debugf("starting")
|
||||
log.WithFields(log.Fields{"app": c.Param("app")}).Debugf("done")
|
||||
|
||||
@@ -730,7 +730,7 @@ func ApiAppDestList(c *gin.Context) {
|
||||
c.Data(http.StatusOK, "application/json", pretty.PrettyOptions(b, &pretty.Options{Indent: " "}))
|
||||
}
|
||||
|
||||
func ApiAppDestAdd(c *gin.Context) {
|
||||
func ApiAppDestinationAdd(c *gin.Context) {
|
||||
app := c.Param("app")
|
||||
dst := c.Param("dst")
|
||||
dst = dst[1:] // cut first char from wildcard match
|
||||
@@ -822,7 +822,7 @@ func ApiAppDestAdd(c *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
func ApiAppDestDel(c *gin.Context) {
|
||||
func ApiAppDestinationDel(c *gin.Context) {
|
||||
app := c.Param("app")
|
||||
dst := c.Param("dst")
|
||||
dst = dst[1:] // cut first char from wildcard match
|
||||
@@ -893,6 +893,179 @@ func ApiAppDestDel(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func ApiAppScheduleList(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.Schedule)
|
||||
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 ApiAppScheduleAdd(c *gin.Context) {
|
||||
app := c.Param("app")
|
||||
schedule := c.Param("schedule")
|
||||
|
||||
log.WithFields(log.Fields{"app": app, "schedule": schedule}).Debugf("starting")
|
||||
log.WithFields(log.Fields{"app": app, "schedule": schedule}).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.Schedule, schedule) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "schedule already exists",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
ac.Schedule = append(ac.Schedule, schedule)
|
||||
|
||||
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 ApiAppScheduleDel(c *gin.Context) {
|
||||
app := c.Param("app")
|
||||
schedule := c.Param("schedule")
|
||||
|
||||
log.WithFields(log.Fields{"app": app, "schedule": schedule}).Debugf("starting")
|
||||
log.WithFields(log.Fields{"app": app, "schedule": schedule}).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.Schedule, schedule) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "schedule does not exist",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
for id, s := range ac.Schedule {
|
||||
if s == schedule {
|
||||
ac.Schedule[id] = ac.Schedule[len(ac.Schedule)-1]
|
||||
ac.Schedule = ac.Schedule[:len(ac.Schedule)-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")
|
||||
|
||||
Reference in New Issue
Block a user