diff --git a/handlers.go b/handlers.go index 21cd481..bde38ab 100644 --- a/handlers.go +++ b/handlers.go @@ -3,7 +3,6 @@ package main import ( "fmt" "io" - "io/ioutil" "log" "net/http" "os" @@ -236,7 +235,7 @@ func clearPasswordHandler(c echo.Context) error { func setKeysHandler(c echo.Context) error { name := c.Param("name") - body, err := ioutil.ReadAll(c.Request().Body) + body, err := io.ReadAll(c.Request().Body) if err != nil { return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) } @@ -495,3 +494,119 @@ func metricsHandler(c echo.Context) error { return c.String(http.StatusOK, metrics) } + +func CreateSnapshotHandler(c echo.Context) error { + name := c.Param("name") + + body := createSnapshotBody{} + err := c.Bind(body) + if err != nil { + return c.JSONPretty(http.StatusBadRequest, Message{Message: err.Error()}, JSONIndent) + } + + processor := glue.Processor{ + AppName: name, + DB: common.GetDBConnection(), + SnapshotProcessor: &snapshotProcessor, + DockerSock: config.DockerSocket, + BindIPHTTP: config.AppsBindIPHTTP, + BindIPSSH: config.AppsBindIPSSH, + AppsPath: config.AppsPath, + } + + err = processor.CreateSnapshot(body.Labels) + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSONPretty(http.StatusOK, Message{Message: "ok"}, JSONIndent) +} + +func RestoreFromSnapshotHandler(c echo.Context) error { + name := c.Param("name") + snapshot := c.Param("snapshot") + + processor := glue.Processor{ + AppName: name, + DB: common.GetDBConnection(), + SnapshotProcessor: &snapshotProcessor, + DockerSock: config.DockerSocket, + BindIPHTTP: config.AppsBindIPHTTP, + BindIPSSH: config.AppsBindIPSSH, + AppsPath: config.AppsPath, + } + + err := processor.RestoreFromSnapshot(snapshot) + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSONPretty(http.StatusOK, Message{Message: "ok"}, JSONIndent) +} + +func ListSnapshotsHandler(c echo.Context) error { + name := c.Param("name") + + processor := glue.Processor{ + AppName: name, + DB: common.GetDBConnection(), + SnapshotProcessor: &snapshotProcessor, + DockerSock: config.DockerSocket, + BindIPHTTP: config.AppsBindIPHTTP, + BindIPSSH: config.AppsBindIPSSH, + AppsPath: config.AppsPath, + } + + snapshots, err := processor.ListSnapshots() + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSON(http.StatusOK, snapshots) +} + +func ListAppsSnapshotsHandler(c echo.Context) error { + name := c.Param("name") + apps := []string{} + + err := c.Bind(&apps) + if err != nil { + return c.JSONPretty(http.StatusBadRequest, Message{Message: err.Error()}, JSONIndent) + } + + processor := glue.Processor{ + AppName: name, + DB: common.GetDBConnection(), + SnapshotProcessor: &snapshotProcessor, + DockerSock: config.DockerSocket, + BindIPHTTP: config.AppsBindIPHTTP, + BindIPSSH: config.AppsBindIPSSH, + AppsPath: config.AppsPath, + } + snapshots, err := processor.ListAppsSnapshots(apps) + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSON(http.StatusOK, snapshots) +} + +func ListSnapshotsByLabelHandler(c echo.Context) error { + label := c.Param("label") + + processor := glue.Processor{ + AppName: "", + DB: common.GetDBConnection(), + SnapshotProcessor: &snapshotProcessor, + DockerSock: config.DockerSocket, + BindIPHTTP: config.AppsBindIPHTTP, + BindIPSSH: config.AppsBindIPSSH, + AppsPath: config.AppsPath, + } + snapshots, err := processor.ListSnapshotsByLabel(label) + if err != nil { + return c.JSONPretty(http.StatusInternalServerError, Message{Message: err.Error()}, JSONIndent) + } + + return c.JSON(http.StatusOK, snapshots) +} diff --git a/main.go b/main.go index 0898d75..5b568fc 100644 --- a/main.go +++ b/main.go @@ -198,6 +198,13 @@ func main() { // Delete one app e.DELETE("/v1/apps/:name", deleteAppHandler) + // Snapshots + e.POST("/v1/apps/:name/snapshots", CreateSnapshotHandler) + e.POST("/v1/apps/:name/snapshots/restore/:snapshot", RestoreFromSnapshotHandler) + e.GET("/v1/apps/:name/snapshots", ListSnapshotsHandler) + e.GET("/v1/snapshots", ListAppsSnapshotsHandler) + e.GET("/v1/snapshots/by-label", ListSnapshotsByLabelHandler) + // Orphans returns directories in /srv that doesn't match any hosted application e.GET("/v1/orphans", getOrphansHander) diff --git a/types.go b/types.go index b38aa20..aabfd26 100644 --- a/types.go +++ b/types.go @@ -65,3 +65,7 @@ type Technology struct { Name string Version string } + +type createSnapshotBody struct { + Labels []string `json:"labels"` +}