-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (94 loc) · 2.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"fmt"
"log"
"strings"
"github.com/adelowo/gulter"
storage "github.com/adelowo/gulter/storage"
human "github.com/dustin/go-humanize"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
"github.com/shirou/gopsutil/disk"
)
type EbsDevice struct {
MountPath string `json:"mountPath"`
Size string `json:"size"`
Used string `json:"used"`
UsePercent string `json:"usePercent"`
Available string `json:"available"`
}
func fetchDiskSpace() ([]EbsDevice, error) {
parts, err := disk.Partitions(false)
if err != nil {
return nil, err
}
var lvs []EbsDevice
for _, p := range parts {
device := p.Mountpoint
if strings.HasPrefix(device, "/home/ubuntu/mounts") {
s, err := disk.Usage(device)
if err != nil {
return nil, err
}
percent := fmt.Sprintf("%2.f%%", s.UsedPercent)
dvc := EbsDevice{
MountPath: device,
Size: human.Bytes(s.Used + s.Free),
Used: human.Bytes(s.Used),
Available: human.Bytes(s.Free),
UsePercent: percent,
}
lvs = append(lvs, dvc)
}
}
return lvs, nil
}
var mountList = []string{"lv1", "lv2"}
func main() {
app := fiber.New(fiber.Config{StreamRequestBody: true})
lv1DiskStore, err := storage.NewDiskStorage(fmt.Sprintf("/home/ubuntu/mounts/%s", mountList[0]))
if err != nil {
panic(err.Error())
}
lv2DiskStore, err := storage.NewDiskStorage(fmt.Sprintf("/home/ubuntu/mounts/%s", mountList[1]))
if err != nil {
panic(err.Error())
}
uploadLv1Handler, err := gulter.New(
gulter.WithMaxFileSize(10<<30),
gulter.WithStorage(lv1DiskStore),
)
if err != nil {
panic(err.Error())
}
uploadLv2Handler, err := gulter.New(
gulter.WithMaxFileSize(10<<30),
gulter.WithStorage(lv2DiskStore),
)
if err != nil {
panic(err.Error())
}
app.Use("/upload/lv1", adaptor.HTTPMiddleware(uploadLv1Handler.Upload("lv1"))).Post("/upload/lv1", func(c *fiber.Ctx) error {
devices, err := fetchDiskSpace()
if err != nil {
return err
}
return c.JSON(devices)
})
app.Use("/upload/lv2", adaptor.HTTPMiddleware(uploadLv2Handler.Upload("lv2"))).Post("/upload/lv2", func(c *fiber.Ctx) error {
devices, err := fetchDiskSpace()
if err != nil {
return err
}
return c.JSON(devices)
})
app.Get("/devices", func(c *fiber.Ctx) error {
devices, err := fetchDiskSpace()
if err != nil {
return err
}
return c.JSON(devices)
})
log.Println("Starting server on port 3000")
log.Fatal(app.Listen(":3000"))
}