-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
168 lines (141 loc) · 2.71 KB
/
routes.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"errors"
"github.com/gin-gonic/gin"
"net/http"
"os"
)
func create(c *gin.Context) {
var payload resource
c.Bind(&payload)
if payload.Dir == "" {
json(c, nil, errors.New("dir cannot be empty"))
return
}
if err := payload.commit(); err != nil {
json(c, nil, err)
return
}
c.JSON(http.StatusOK, payload)
}
func get(c *gin.Context) {
dir := c.Query("dir")
if dir == "" {
json(c, nil, errors.New("dir cannot be empty"))
return
}
id := c.Param("id")
r, err := getResource(dir, id)
if err != nil {
json(c, nil, err)
return
}
json(c, r)
}
func deleteHandler(c *gin.Context) {
dir := c.Query("dir")
if dir == "" {
json(c, nil, errors.New("dir cannot be empty"))
return
}
id := c.Param("id")
err := deleteResource(dir, id)
json(c, nil, err)
}
func update(c *gin.Context) {
// get id too
var payload resource
err := c.Bind(&payload)
if err != nil {
json(c, err)
return
}
payload.ID = c.Param("id")
if payload.ID == "" || payload.Dir == "" {
json(c, nil, errors.New("dir or id cannot be empty"))
return
}
err = payload.update()
if err != nil {
json(c, err)
return
}
c.JSON(http.StatusOK, payload)
}
func totalSubDir(c *gin.Context) {
dir := c.Query("dir")
if dir == "" {
json(c, nil, errors.New("dir cannot be empty"))
return
}
count, err := includedFolderCount(dir)
response := struct {
TotalFiles int
}{count}
json(c, response, err)
}
func alphanumericStatics(c *gin.Context) {
dir := c.Query("dir")
if dir == "" {
json(c, nil, errors.New("dir cannot be empty"))
return
}
meanAN, stdDev, err := alphanumericStatic(dir)
if err != nil {
json(c, nil, err)
return
}
resp := Stat{meanAN, stdDev}
json(c, resp, nil)
}
func wordLength(c *gin.Context) {
dir := c.Query("dir")
if dir == "" {
json(c, nil, errors.New("dir cannot be empty"))
return
}
mean, dev, err := averageWorldLength(dir)
if err != nil {
json(c, nil, err)
return
}
resp := Stat{mean, dev}
json(c, resp, nil)
}
func size(c *gin.Context) {
dir := c.Query("dir")
if dir == "" {
json(c, nil, errors.New("dir cannot be empty"))
return
}
size, err := directorySize(dir)
if err != nil {
json(c, nil, err)
return
}
resp := struct {
Size int64
}{size}
json(c, resp, nil)
}
func json(c *gin.Context, data interface{}, err ...error) {
var e error
if len(err) != 0 && err[0] != nil {
e = err[0]
}
// ugly
statusCode := http.StatusOK
if e == os.ErrNotExist {
statusCode = http.StatusNotFound
} else if e != nil {
statusCode = http.StatusBadRequest
}
if e == nil {
e = errors.New("")
}
c.JSON(http.StatusOK, struct {
StatusCode int
ErrorMessage string
Data interface{} `json:",omitempty"`
}{statusCode, e.Error(), data})
}