/
cache.go
153 lines (142 loc) · 2.93 KB
/
cache.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package api
import (
"github.com/gin-gonic/gin"
liberr "github.com/jortel/go-utils/error"
"github.com/konveyor/tackle2-hub/nas"
"net/http"
"os"
"os/exec"
pathlib "path"
"strings"
)
//
// Routes
const (
CacheRoot = "/cache"
CacheDirRoot = CacheRoot + "/*" + Wildcard
)
//
// CacheHandler handles cache routes.
type CacheHandler struct {
BaseHandler
}
//
// AddRoutes adds routes.
func (h CacheHandler) AddRoutes(e *gin.Engine) {
routeGroup := e.Group("/")
routeGroup.Use(Required("cache"))
routeGroup.GET(CacheRoot, h.Get)
routeGroup.GET(CacheDirRoot, h.Get)
routeGroup.DELETE(CacheDirRoot, h.Delete)
}
// Get godoc
// @summary Get the cache.
// @description Get the cache.
// @tags cache
// @produce json
// @success 200 {object} api.Cache
// @router /caches/{wildcard} [get]
// @param wildcard path string true "Cache DIR"
func (h CacheHandler) Get(ctx *gin.Context) {
dir := ctx.Param(Wildcard)
r, err := h.cache(dir)
if err != nil {
if os.IsNotExist(err) {
h.Status(ctx, http.StatusNotFound)
} else {
_ = ctx.Error(err)
}
return
}
h.Respond(ctx, http.StatusOK, r)
}
// Delete godoc
// @summary Delete a directory within the cache.
// @description Delete a directory within the cache.
// @tags cache
// @produce json
// @success 204
// @router /cache [delete]
func (h CacheHandler) Delete(ctx *gin.Context) {
dir := ctx.Param(Wildcard)
if dir == "" {
h.Status(ctx, http.StatusForbidden)
return
}
path := pathlib.Join(
Settings.Cache.Path,
dir)
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
h.Status(ctx, http.StatusNoContent)
} else {
_ = ctx.Error(err)
}
return
}
err = nas.RmDir(path)
if err != nil {
_ = ctx.Error(err)
return
}
h.Status(ctx, http.StatusNoContent)
}
//
// cache builds the resource.
func (h *CacheHandler) cache(dir string) (cache *Cache, err error) {
cache = &Cache{}
cache.Path = pathlib.Join(
Settings.Cache.Path,
dir)
_, err = os.Stat(Settings.Cache.Path)
if err != nil {
return
}
cmd := exec.Command("/usr/bin/df", "-h", Settings.Cache.Path)
stdout, err := cmd.Output()
if err != nil {
err = liberr.Wrap(err)
return
}
output := string(stdout)
lines := strings.Split(output, "\n")
fields := strings.Fields(lines[1])
if len(fields) < 6 {
return
}
cache.Capacity = fields[1]
cache.Used = fields[2]
cache.Exists = true
if dir == "" {
return
}
_, err = os.Stat(cache.Path)
if err != nil {
cache.Used = "0"
cache.Exists = false
if os.IsNotExist(err) {
err = nil
}
return
}
cmd = exec.Command("/usr/bin/du", "-h", "-d0", cache.Path)
stdout, err = cmd.Output()
if err != nil {
err = liberr.Wrap(err)
return
}
output = string(stdout)
lines = strings.Split(output, "\n")
fields = strings.Fields(lines[0])
cache.Used = fields[0]
return
}
//
// Cache REST resource.
type Cache struct {
Path string `json:"path"`
Capacity string `json:"capacity"`
Used string `json:"used"`
Exists bool `json:"exists"`
}