api app source
This commit is contained in:
229
api.go
229
api.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
@@ -24,7 +25,7 @@ func ApiSnapshotAdd(c *gin.Context) {
|
||||
if err := app.RunStandaloneSchedule(schedule); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -57,7 +58,7 @@ func ApiSnapshotList(c *gin.Context) {
|
||||
if snapshots, err := app.Snapshots(); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -102,7 +103,7 @@ func ApiRunApp(c *gin.Context) {
|
||||
if err := app.RunStandaloneTime(time.Now()); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -121,7 +122,7 @@ func ApiSave(c *gin.Context) {
|
||||
if err := cfg.Save(true); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -134,7 +135,7 @@ func ApiConfig(c *gin.Context) {
|
||||
if b, err := cfg.Pretty(true); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
} else {
|
||||
c.Data(http.StatusOK, "application/json", b)
|
||||
@@ -154,7 +155,7 @@ func ApiConfigApp(c *gin.Context) {
|
||||
if b, err := app.Pretty(false); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
} else {
|
||||
c.Data(http.StatusOK, "application/json", b)
|
||||
@@ -185,7 +186,7 @@ func ApiAppList(c *gin.Context) {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -213,7 +214,7 @@ func ApiListSchedule(c *gin.Context) {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -251,7 +252,7 @@ func ApiAppAdd(c *gin.Context) {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -293,7 +294,7 @@ func ApiAppDel(c *gin.Context) {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -323,7 +324,7 @@ func ApiAppActivate(c *gin.Context) {
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": err,
|
||||
"error": fmt.Sprint(err),
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -331,6 +332,16 @@ func ApiAppActivate(c *gin.Context) {
|
||||
cfg.apps[app.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",
|
||||
})
|
||||
@@ -358,31 +369,203 @@ func ApiAppDeactivate(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
})
|
||||
}
|
||||
|
||||
// FIXME
|
||||
func ApiAppSourceList(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "not implemented",
|
||||
})
|
||||
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.Sources)
|
||||
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: " "}))
|
||||
}
|
||||
|
||||
// FIXME
|
||||
func ApiAppSourceAdd(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "not implemented",
|
||||
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
|
||||
}
|
||||
|
||||
src := Addr(c.Param("src"))
|
||||
if src.Box() == "" {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "source box incorrect",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if _, ok := cfg.box[src.Box()]; !ok {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "source box doesn't exist",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if src.Path() == "" {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "source path incorrect",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if slices.Contains(app.Sources, src.String()) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "source already exists",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
app.Sources = append(app.Sources, src.String())
|
||||
|
||||
if app.Active {
|
||||
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": 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",
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// FIXME
|
||||
func ApiAppSourceDel(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "not implemented",
|
||||
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
|
||||
}
|
||||
|
||||
src := c.Param("src")
|
||||
if !slices.Contains(app.Sources, src) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": "error",
|
||||
"error": "source does not exist",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
for id, s := range app.Sources {
|
||||
if s == src {
|
||||
app.Sources[id] = app.Sources[len(app.Sources)-1]
|
||||
app.Sources = app.Sources[:len(app.Sources)-1]
|
||||
}
|
||||
}
|
||||
|
||||
if app.Active {
|
||||
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": 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",
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user