173 lines
3.9 KiB
Go
173 lines
3.9 KiB
Go
package api
|
|
|
|
import (
|
|
"ddbb/internal/docker"
|
|
"ddbb/internal/dump"
|
|
"ddbb/internal/integrity"
|
|
"ddbb/internal/restore"
|
|
"ddbb/internal/types"
|
|
"io/fs"
|
|
"log"
|
|
"net/http"
|
|
"slices"
|
|
|
|
"embed"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
//go:embed public
|
|
var embeddedFiles embed.FS
|
|
|
|
type RequestBody struct {
|
|
Id string `json:"id"`
|
|
}
|
|
|
|
// API entrypoint
|
|
func SetupRouter() *gin.Engine {
|
|
// GIN General setup
|
|
gin.SetMode(gin.ReleaseMode)
|
|
r := gin.Default()
|
|
err := r.SetTrustedProxies([]string{"127.0.0.1"})
|
|
if err != nil {
|
|
log.Printf("%v", err)
|
|
}
|
|
|
|
// API group: isolate endpoints under /api
|
|
apiGroup := r.Group("/api")
|
|
{
|
|
apiGroup.GET("/list", listJobs)
|
|
apiGroup.POST("/dump", triggerDump)
|
|
apiGroup.POST("/restore", triggerRestore)
|
|
apiGroup.POST("/integrity", triggerIntegrityCheck)
|
|
}
|
|
|
|
// Serve static assets under /static to avoid conflict
|
|
staticFS, _ := fs.Sub(embeddedFiles, "public/static")
|
|
r.StaticFS("/static", http.FS(staticFS))
|
|
|
|
// Serve SPA index.html at root or fallback route for client-side routing
|
|
r.GET("/", func(c *gin.Context) {
|
|
indexFile, err := embeddedFiles.Open("public/index.html")
|
|
if err != nil {
|
|
c.String(http.StatusInternalServerError, "index.html not found")
|
|
return
|
|
}
|
|
defer indexFile.Close()
|
|
|
|
stat, err := indexFile.Stat()
|
|
if err != nil {
|
|
c.String(http.StatusInternalServerError, "could not stat index.html")
|
|
return
|
|
}
|
|
|
|
c.DataFromReader(http.StatusOK, stat.Size(), "text/html; charset=utf-8", indexFile, nil)
|
|
})
|
|
|
|
return r
|
|
}
|
|
|
|
func selectJob(id string) types.Job {
|
|
var chosenJob *types.Job
|
|
jobs, err := docker.GetConfigsFromContainers()
|
|
if err != nil {
|
|
log.Fatalf("Error while parsing docker labels: %v", err)
|
|
}
|
|
for i, job := range jobs {
|
|
if job.Id == id {
|
|
chosenJob = &jobs[i]
|
|
break
|
|
}
|
|
}
|
|
return *chosenJob
|
|
}
|
|
|
|
func isValidJobId(id string) bool {
|
|
var idLists []string
|
|
jobs, err := docker.GetConfigsFromContainers()
|
|
if err != nil {
|
|
log.Fatalf("Error while parsing docker labels: %v", err)
|
|
}
|
|
for _, job := range jobs {
|
|
idLists = append(idLists, job.Id)
|
|
}
|
|
|
|
return slices.Contains(idLists, id)
|
|
}
|
|
|
|
func listJobs(c *gin.Context) {
|
|
jobs, err := docker.GetConfigsFromContainers()
|
|
if err != nil {
|
|
log.Fatalf("Error while parsing docker labels: %v", err)
|
|
}
|
|
c.JSON(200, jobs)
|
|
}
|
|
|
|
// Dump any database based on Id
|
|
func triggerDump(c *gin.Context) {
|
|
// Read Id from JSON submitted data
|
|
var req RequestBody
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
if req.Id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing backup job id"})
|
|
}
|
|
if isValidJobId(req.Id) {
|
|
result, err := dump.NewDumpJob(selectJob(req.Id))
|
|
if err != nil {
|
|
log.Printf("%v", err)
|
|
}
|
|
c.JSON(200, result)
|
|
} else {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Invalid job id"})
|
|
}
|
|
}
|
|
|
|
// Restore any database based on Id
|
|
func triggerRestore(c *gin.Context) {
|
|
// Read Id from JSON submitted data
|
|
var req RequestBody
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
if req.Id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing backup job id"})
|
|
}
|
|
if isValidJobId(req.Id) {
|
|
result, err := restore.NewRestoreJob(selectJob(req.Id))
|
|
if err != nil {
|
|
log.Printf("%v", err)
|
|
}
|
|
c.JSON(200, result)
|
|
} else {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Invalid job id"})
|
|
}
|
|
}
|
|
|
|
// Check any database based on Id
|
|
func triggerIntegrityCheck(c *gin.Context) {
|
|
// Read Id from JSON submitted data
|
|
var req RequestBody
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
if req.Id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing backup job id"})
|
|
}
|
|
if isValidJobId(req.Id) {
|
|
result, err := integrity.NewIntegrityCheckJob(selectJob(req.Id))
|
|
if err != nil {
|
|
log.Printf("%v", err)
|
|
}
|
|
c.JSON(200, result)
|
|
} else {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Invalid job id"})
|
|
}
|
|
}
|