60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package backupmonitor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"os"
|
|
|
|
"hfb_sys/backend/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Status struct {
|
|
GeneratedAt string `json:"generated_at"`
|
|
Health string `json:"health"`
|
|
Schedule struct {
|
|
Enabled string `json:"enabled"`
|
|
FullHour string `json:"full_hour"`
|
|
FullMinute string `json:"full_minute"`
|
|
BinlogIntervalMinutes string `json:"binlog_interval_minutes"`
|
|
} `json:"schedule"`
|
|
LastFull struct {
|
|
LocalComplete string `json:"local_complete"`
|
|
RemoteComplete string `json:"remote_complete"`
|
|
} `json:"last_full"`
|
|
LastBinlog struct {
|
|
File string `json:"file"`
|
|
RemoteDir string `json:"remote_dir"`
|
|
} `json:"last_binlog"`
|
|
LastJob struct {
|
|
Type string `json:"type"`
|
|
Status string `json:"status"`
|
|
At string `json:"at"`
|
|
Message string `json:"message"`
|
|
} `json:"last_job"`
|
|
}
|
|
|
|
type Handler struct{ path string }
|
|
|
|
func NewHandler(path string) *Handler { return &Handler{path: path} }
|
|
|
|
func (h *Handler) Status(c *gin.Context) {
|
|
content, err := os.ReadFile(h.path)
|
|
if err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
response.Error(c, http.StatusServiceUnavailable, "backup_status_unavailable", "备份状态尚未生成")
|
|
return
|
|
}
|
|
response.Error(c, http.StatusInternalServerError, "backup_status_unavailable", "无法读取备份状态")
|
|
return
|
|
}
|
|
var status Status
|
|
if err := json.Unmarshal(content, &status); err != nil {
|
|
response.Error(c, http.StatusServiceUnavailable, "backup_status_invalid", "备份状态文件格式无效")
|
|
return
|
|
}
|
|
response.OK(c, status)
|
|
}
|