/
handlers.go
147 lines (123 loc) · 3.39 KB
/
handlers.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
package server
import (
"bytes"
"fmt"
"net/http"
"strings"
"time"
"github.com/cloudcloud/roadie/pkg/data"
dest "github.com/cloudcloud/roadie/pkg/destinations"
"github.com/cloudcloud/roadie/pkg/info"
sour "github.com/cloudcloud/roadie/pkg/sources"
"github.com/cloudcloud/roadie/pkg/types"
"github.com/gin-gonic/gin"
)
func config(c *gin.Context) {
wrap(c, func(ctx *gin.Context, d *data.Data) (interface{}, []string) {
paths := []string{}
for _, p := range d.GetDestinations() {
if p.Type == dest.DestinationLocalPath {
paths = append(paths, p.Store.GetLocation())
}
}
i := info.DiskDetails(paths)
return gin.H{
"disk_info": i,
}, []string{}
})
}
func destination(c *gin.Context) {
wrap(c, func(ctx *gin.Context, d *data.Data) (interface{}, []string) {
n := dest.FromURL(ctx.Param("name"))
return gin.H{
"destination": d.GetDestination(n),
"entries": d.GetDestinationRefs(n),
}, []string{}
})
}
func destinations(c *gin.Context) {
wrap(c, func(ctx *gin.Context, d *data.Data) (interface{}, []string) {
return dest.PrepareList(d.GetDestinations()), []string{}
})
}
func execute(c *gin.Context) {
wrap(c, func(ctx *gin.Context, d *data.Data) (interface{}, []string) {
body := types.ExecutePayload{}
ctx.BindJSON(&body)
body.SourceName = sour.FromURL(body.SourceName)
body.DestinationName = dest.FromURL(body.DestinationName)
return d.Copy(body), []string{}
})
}
func historical(c *gin.Context) {
wrap(c, func(ctx *gin.Context, d *data.Data) (interface{}, []string) {
h := d.GetHistories()
for a, x := range h {
h[a].Destination = dest.PrepareDestination(x.Destination)
h[a].Source = sour.PrepareSource(x.Source)
}
return h, []string{}
})
}
func index(c *gin.Context) {
s := strings.Replace(
MustAssetString("index.html"),
"<head>",
"<head><script id=\"config\">{\"hostname\":\""+c.MustGet("config").(types.Configer).GetHostname()+"\"}</script>",
1,
)
r := bytes.NewReader([]byte(s))
c.DataFromReader(
http.StatusOK,
int64(len(s)),
"text/html",
r,
map[string]string{},
)
}
func remove(c *gin.Context) {
wrap(c, func(ctx *gin.Context, d *data.Data) (interface{}, []string) {
body := types.RemovePayload{}
ctx.BindJSON(&body)
body.DestinationName = dest.FromURL(body.DestinationName)
return d.RemoveFile(body), []string{}
})
}
func source(c *gin.Context) {
wrap(c, func(ctx *gin.Context, d *data.Data) (interface{}, []string) {
n := sour.FromURL(ctx.Param("name"))
return gin.H{
"source": sour.PrepareSource(d.GetSource(n)),
"entries": d.GetSourceRefs(n),
}, []string{}
})
}
func sources(c *gin.Context) {
wrap(c, func(ctx *gin.Context, d *data.Data) (interface{}, []string) {
return sour.PrepareList(d.GetSources()), []string{}
})
}
func subSource(c *gin.Context) {
wrap(c, func(ctx *gin.Context, d *data.Data) (interface{}, []string) {
n := sour.FromURL(ctx.Param("name"))
s := sour.FromURL(ctx.Param("sub"))
return gin.H{
"source": sour.PrepareSource(d.GetSource(n)),
"entries": d.GetSubSourceRefs(n, s),
}, []string{}
})
}
func wrap(c *gin.Context, f func(*gin.Context, *data.Data) (interface{}, []string)) {
begin := time.Now()
d := c.MustGet("data").(*data.Data)
out, errs := f(c, d)
latency := time.Since(begin)
c.JSON(http.StatusOK, gin.H{
"items": out,
"errors": errs,
"meta": map[string]interface{}{
"latency": fmt.Sprintf("%v", latency),
"errors": len(errs),
},
})
}