Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FEATURE (disk usage & healthcheck): Add healthckeck API endpoint with disk usage stats

Rostislav Dugin9d528820c7b3
diff --git a/backend/README.md b/backend/README.md
index f17f0b86..d2a9d940 100644
--- a/backend/README.md
+++ b/backend/README.md
@@ -49,7 +49,7 @@ To generate swagger docs:
Swagger URL is:
-> http://localhost:8080/api/v1/docs/swagger/index.html#/
+> http://localhost:4005/api/v1/docs/swagger/index.html#/
# Project structure
diff --git a/backend/cmd/main.go b/backend/cmd/main.go
index 0b9ce777..eea65df3 100644
--- a/backend/cmd/main.go
+++ b/backend/cmd/main.go
@@ -16,6 +16,8 @@ import (
"postgresus-backend/internal/downdetect"
"postgresus-backend/internal/features/backups"
"postgresus-backend/internal/features/databases"
+ "postgresus-backend/internal/features/disk"
+ "postgresus-backend/internal/features/healthcheck"
"postgresus-backend/internal/features/notifiers"
"postgresus-backend/internal/features/restores"
"postgresus-backend/internal/features/storages"
@@ -127,6 +129,8 @@ func setUpRoutes(r *gin.Engine) {
databaseController := databases.GetDatabaseController()
backupController := backups.GetBackupController()
restoreController := restores.GetRestoreController()
+ healthcheckController := healthcheck.GetHealthcheckController()
+ diskController := disk.GetDiskController()
downdetectContoller.RegisterRoutes(v1)
userController.RegisterRoutes(v1)
@@ -135,6 +139,8 @@ func setUpRoutes(r *gin.Engine) {
databaseController.RegisterRoutes(v1)
backupController.RegisterRoutes(v1)
restoreController.RegisterRoutes(v1)
+ healthcheckController.RegisterRoutes(v1)
+ diskController.RegisterRoutes(v1)
}
func setUpDependencies() {
diff --git a/backend/go.mod b/backend/go.mod
index d855a163..2177e83c 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -20,6 +20,7 @@ require (
github.com/swaggo/swag v1.16.4
github.com/testcontainers/testcontainers-go v0.37.0
golang.org/x/crypto v0.39.0
+ golang.org/x/sys v0.33.0
gorm.io/driver/postgres v1.5.11
gorm.io/gorm v1.26.1
)
@@ -100,6 +101,7 @@ require (
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/rs/xid v1.6.0 // indirect
+ github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/shirou/gopsutil/v4 v4.25.5 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tinylib/msgp v1.3.0 // indirect
@@ -118,7 +120,6 @@ require (
golang.org/x/arch v0.17.0 // indirect
golang.org/x/net v0.40.0 // indirect
golang.org/x/sync v0.15.0 // indirect
- golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.26.0 // indirect
golang.org/x/tools v0.33.0 // indirect
google.golang.org/protobuf v1.36.6 // indirect
diff --git a/backend/go.sum b/backend/go.sum
index 679d4e54..592749eb 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -213,6 +213,8 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
+github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
+github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shirou/gopsutil/v4 v4.25.5 h1:rtd9piuSMGeU8g1RMXjZs9y9luK5BwtnG7dZaQUJAsc=
github.com/shirou/gopsutil/v4 v4.25.5/go.mod h1:PfybzyydfZcN+JMMjkF6Zb8Mq1A/VcogFFg7hj50W9c=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
diff --git a/backend/internal/features/disk/controller.go b/backend/internal/features/disk/controller.go
new file mode 100644
index 00000000..2050c584
--- /dev/null
+++ b/backend/internal/features/disk/controller.go
@@ -0,0 +1,33 @@
+package disk
+
+import (
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+)
+
+type DiskController struct {
+ diskService *DiskService
+}
+
+func (c *DiskController) RegisterRoutes(router *gin.RouterGroup) {
+ router.GET("/disk/usage", c.GetDiskUsage)
+}
+
+// GetDiskUsage
+// @Summary Get disk usage information
+// @Description Returns information about disk space usage
+// @Tags disk
+// @Produce json
+// @Success 200 {object} DiskUsage
+// @Failure 500
+// @Router /disk/usage [get]
+func (c *DiskController) GetDiskUsage(ctx *gin.Context) {
+ diskUsage, err := c.diskService.GetDiskUsage()
+ if err != nil {
+ ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
+ return
+ }
+
+ ctx.JSON(http.StatusOK, diskUsage)
+}
diff --git a/backend/internal/features/disk/di.go b/backend/internal/features/disk/di.go
new file mode 100644
index 00000000..096969f1
--- /dev/null
+++ b/backend/internal/features/disk/di.go
@@ -0,0 +1,22 @@
+package disk
+
+var (
+ diskService *DiskService
+ diskController *DiskController
+)
+
+func init() {
+ diskService = &DiskService{}
+
+ diskController = &DiskController{
+ diskService,
+ }
+}
+
+func GetDiskService() *DiskService {
+ return diskService
+}
+
+func GetDiskController() *DiskController {
+ return diskController
+}
diff --git a/backend/internal/features/disk/dto.go b/backend/internal/features/disk/dto.go
new file mode 100644
index 00000000..8a9d978a
--- /dev/null
+++ b/backend/internal/features/disk/dto.go
@@ -0,0 +1,8 @@
+package disk
+
+type DiskUsage struct {
+ Platform Platform `json:"platform"`
+ TotalSpaceBytes int64 `json:"totalSpaceBytes"`
+ UsedSpaceBytes int64 `json:"usedSpaceBytes"`
+ FreeSpaceBytes int64 `json:"freeSpaceBytes"`
+}
diff --git a/backend/internal/features/disk/enums.go b/backend/internal/features/disk/enums.go
new file mode 100644
index 00000000..48f1622d
--- /dev/null
+++ b/backend/internal/features/disk/enums.go
@@ -0,0 +1,8 @@
+package disk
+
+type Platform string
+
+const (
+ PlatformLinux Platform = "linux"
+ PlatformWindows Platform = "windows"
+)
diff --git a/backend/internal/features/disk/service.go b/backend/internal/features/disk/service.go
new file mode 100644
index 00000000..bc05360f
--- /dev/null
+++ b/backend/internal/features/disk/service.go
@@ -0,0 +1,41 @@
+package disk
+
+import (
+ "fmt"
+ "runtime"
+
+ "github.com/shirou/gopsutil/v4/disk"
+)
+
+type DiskService struct{}
+
+func (s *DiskService) GetDiskUsage() (*DiskUsage, error) {
+ platform := s.detectPlatform()
+
+ // Set path based on platform
+ path := "/"
+ if platform == PlatformWindows {
+ path = "C:\\"
+ }
+
+ diskUsage, err := disk.Usage(path)
+ if err != nil {
+ return nil, fmt.Errorf("failed to get disk usage for path %s: %w", path, err)
+ }
+
+ return &DiskUsage{
+ Platform: platform,
+ TotalSpaceBytes: int64(diskUsage.Total),
+ UsedSpaceBytes: int64(diskUsage.Used),
+ FreeSpaceBytes: int64(diskUsage.Free),
+ }, nil
+}
+
+func (s *DiskService) detectPlatform() Platform {
+ switch runtime.GOOS {
+ case "windows":
+ return PlatformWindows
+ default:
+ return PlatformLinux
+ }
+}
diff --git a/backend/internal/features/healthcheck/controller.go b/backend/internal/features/healthcheck/controller.go
new file mode 100644
index 00000000..ac58e333
--- /dev/null
+++ b/backend/internal/features/healthcheck/controller.go
@@ -0,0 +1,39 @@
+package healthcheck
+
+import (
+ "net/http"
+
+ "github.com/gin-gonic/gin"
+)
+
+type HealthcheckController struct {
+ healthcheckService *HealthcheckService
+}
+
+func (c *HealthcheckController) RegisterRoutes(router *gin.RouterGroup) {
+ router.GET("/health", c.CheckHealth)
+}
+
+// CheckHealth
+// @Summary Check system health
+// @Description Check if the system is healthy by testing database connection
+// @Tags healthcheck
+// @Produce json
+// @Success 200 {object} HealthcheckResponse
+// @Failure 503 {object} HealthcheckResponse
+// @Router /health [get]
+func (c *HealthcheckController) CheckHealth(ctx *gin.Context) {
+ err := c.healthcheckService.IsHealthy()
+
+ if err == nil {
+ ctx.JSON(
+ http.StatusOK,
+ HealthcheckResponse{
+ Status: "Application is healthy, internal DB working fine and disk usage is below 95%. You can connect downdetector to this endpoint",
+ },
+ )
+ return
+ }
+
+ ctx.JSON(http.StatusServiceUnavailable, HealthcheckResponse{Status: err.Error()})
+}
diff --git a/backend/internal/features/healthcheck/di.go b/backend/internal/features/healthcheck/di.go
new file mode 100644
index 00000000..e5c0dd58
--- /dev/null
+++ b/backend/internal/features/healthcheck/di.go
@@ -0,0 +1,22 @@
+package healthcheck
+
+import "postgresus-backend/internal/features/disk"
+
+var (
+ healthcheckService *HealthcheckService
+ healthcheckController *HealthcheckController
+)
+
+func init() {
+ healthcheckService = &HealthcheckService{
+ disk.GetDiskService(),
+ }
+
+ healthcheckController = &HealthcheckController{
+ healthcheckService,
+ }
+}
+
+func GetHealthcheckController() *HealthcheckController {
+ return healthcheckController
+}
diff --git a/backend/internal/features/healthcheck/dto.go b/backend/internal/features/healthcheck/dto.go
new file mode 100644
index 00000000..3180f2bf
--- /dev/null
+++ b/backend/internal/features/healthcheck/dto.go
@@ -0,0 +1,5 @@
+package healthcheck
+
+type HealthcheckResponse struct {
+ Status string `json:"status"`
+}
diff --git a/backend/internal/features/healthcheck/service.go b/backend/internal/features/healthcheck/service.go
new file mode 100644
index 00000000..83a5b919
--- /dev/null
+++ b/backend/internal/features/healthcheck/service.go
@@ -0,0 +1,31 @@
+package healthcheck
+
+import (
+ "errors"
+ "postgresus-backend/internal/features/disk"
+ "postgresus-backend/internal/storage"
+)
+
+type HealthcheckService struct {
+ diskService *disk.DiskService
+}
+
+func (s *HealthcheckService) IsHealthy() error {
+ diskUsage, err := s.diskService.GetDiskUsage()
+ if err != nil {
+ return errors.New("cannot get disk usage")
+ }
+
+ if float64(diskUsage.UsedSpaceBytes) >= float64(diskUsage.TotalSpaceBytes)*0.95 {
+ return errors.New("more than 95% of the disk is used")
+ }
+
+ db := storage.GetDb()
+ err = db.Raw("SELECT 1").Error
+
+ if err != nil {
+ return errors.New("cannot connect to the database")
+ }
+
+ return nil
+}
diff --git a/frontend/src/entity/disk/api/diskApi.ts b/frontend/src/entity/disk/api/diskApi.ts
new file mode 100644
index 00000000..d55ec7e3
--- /dev/null
+++ b/frontend/src/entity/disk/api/diskApi.ts
@@ -0,0 +1,15 @@
+import { getApplicationServer } from '../../../constants';
+import RequestOptions from '../../../shared/api/RequestOptions';
+import { apiHelper } from '../../../shared/api/apiHelper';
+import type { DiskUsage } from '../model/DiskUsage';
+
+export const diskApi = {
+ async getDiskUsage() {
+ const requestOptions: RequestOptions = new RequestOptions();
+ return apiHelper.fetchGetJson<DiskUsage>(
+ `${getApplicationServer()}/api/v1/disk/usage`,
+ requestOptions,
+ true,
+ );
+ },
+};
diff --git a/frontend/src/entity/disk/index.ts b/frontend/src/entity/disk/index.ts
new file mode 100644
index 00000000..7847d6f4
--- /dev/null
+++ b/frontend/src/entity/disk/index.ts
@@ -0,0 +1,3 @@
+export { diskApi } from './api/diskApi';
+export { type DiskUsage } from './model/DiskUsage';
+export { type Platform } from './model/Platform';
diff --git a/frontend/src/entity/disk/model/DiskUsage.ts b/frontend/src/entity/disk/model/DiskUsage.ts
new file mode 100644
index 00000000..e428735d
--- /dev/null
+++ b/frontend/src/entity/disk/model/DiskUsage.ts
@@ -0,0 +1,8 @@
+import type { Platform } from './Platform';
+
+export type DiskUsage = {
+ platform: Platform;
+ totalSpaceBytes: number;
+ usedSpaceBytes: number;
+ freeSpaceBytes: number;
+};
diff --git a/frontend/src/entity/disk/model/Platform.ts b/frontend/src/entity/disk/model/Platform.ts
new file mode 100644
index 00000000..8bae64df
--- /dev/null
+++ b/frontend/src/entity/disk/model/Platform.ts
@@ -0,0 +1,4 @@
+export enum Platform {
+ Linux = 'linux',
+ Windows = 'windows',
+}
diff --git a/frontend/src/widgets/main/MainScreenComponent.tsx b/frontend/src/widgets/main/MainScreenComponent.tsx
index 7400c03e..e1dd06ac 100644
--- a/frontend/src/widgets/main/MainScreenComponent.tsx
+++ b/frontend/src/widgets/main/MainScreenComponent.tsx
@@ -1,19 +1,36 @@
-import { useState } from 'react';
+import { Tooltip } from 'antd';
+import { useEffect, useState } from 'react';
+import GitHubButton from 'react-github-btn';
+import { getApplicationServer } from '../../constants';
+import { type DiskUsage, diskApi } from '../../entity/disk';
import { DatabasesComponent } from '../../features/databases/ui/DatabasesComponent';
import { NotifiersComponent } from '../../features/notifiers/ui/NotifiersComponent';
import { StoragesComponent } from '../../features/storages/StoragesComponent';
import { useScreenHeight } from '../../shared/hooks';
-import GitHubButton from 'react-github-btn';
export const MainScreenComponent = () => {
const screenHeight = useScreenHeight();
+ const contentHeight = screenHeight - 95;
const [selectedTab, setSelectedTab] = useState<'notifiers' | 'storages' | 'databases'>(
'databases',
);
+ const [diskUsage, setDiskUsage] = useState<DiskUsage | undefined>(undefined);
- const contentHeight = screenHeight - 95;
+ useEffect(() => {
+ diskApi
+ .getDiskUsage()
+ .then((diskUsage) => {
+ setDiskUsage(diskUsage);
+ })
+ .catch((error) => {
+ alert(error.message);
+ });
+ }, []);
+
+ const isUsedMoreThan95Percent =
+ diskUsage && diskUsage.usedSpaceBytes / diskUsage.totalSpaceBytes > 0.95;
return (
<div style={{ height: screenHeight }} className="bg-[#f5f5f5] p-3">
@@ -37,6 +54,15 @@ export const MainScreenComponent = () => {
</div>
<div className="mr-3 ml-auto flex items-center gap-5">
+ <a
+ className="hover:opacity-80"
+ href={`${getApplicationServer()}/api/v1/health`}
+ target="_blank"
+ rel="noreferrer"
+ >
+ Healthcheck
+ </a>
+
<a
className="hover:opacity-80"
href="https://t.me/postgresus_community"
@@ -57,6 +83,20 @@ export const MainScreenComponent = () => {
&nbsp;Star on GitHub
</GitHubButton>
</div>
+
+ {diskUsage && (
+ <Tooltip title="To make backups locally and restore them, you need to have enough space on your disk. For restore, you need to have same amount of space that the backup size.">
+ <div
+ className={`cursor-pointer text-center text-xs ${isUsedMoreThan95Percent ? 'text-red-500' : 'text-gray-500'}`}
+ >
+ {(diskUsage.usedSpaceBytes / 1024 ** 3).toFixed(1)} of{' '}
+ {(diskUsage.totalSpaceBytes / 1024 ** 3).toFixed(1)} GB
+ <br />
+ ROM used (
+ {((diskUsage.usedSpaceBytes / diskUsage.totalSpaceBytes) * 100).toFixed(1)}%)
+ </div>
+ </Tooltip>
+ )}
</div>
</div>
{/* ===================== END NAVBAR ===================== */}