-
Notifications
You must be signed in to change notification settings - Fork 3
/
service.go
328 lines (292 loc) · 8.51 KB
/
service.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package main
import (
"archive/zip"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"github.com/gorilla/mux"
uuid "github.com/satori/go.uuid"
)
var jobMutex sync.Mutex
var jobs = make(map[string]*Job)
type SlicerService struct {
EndPoint string `yaml:"endPoint"`
Executable string `yaml:"executable"`
}
type Service struct {
EndPoint string `yaml:"endPoint" json:"end_point"`
CommandLine []string `yaml:"commandLine" json:"command_line"`
Description string `json:"description"`
Defaults map[string]string `json:"defaults" yaml:"defaults"`
CreateEmptyOutput bool `json:"create_empty_output" yaml:"create_empty_output"`
Arguments []string `json:"arguments"`
Parameters []string `json:"parameters"`
InputFiles []string `json:"input_files"`
OutputFiles []string `json:"output_files"`
InputZip []string `json:"input_directories"`
OutputZip []string `json:"output_directories"`
ParameterDescriptions map[string]string `json:"parameter_descriptions" yaml:"parameter_descriptions"`
isSetup bool
}
// Parse our argements
func NewService() *Service {
var service Service
service.Defaults = make(map[string]string)
service.Arguments = make([]string, 0)
service.Parameters = make([]string, 0)
service.InputFiles = make([]string, 0)
service.OutputFiles = make([]string, 0)
service.ParameterDescriptions = make(map[string]string)
return &service
}
func (service *Service) setup() *Service {
if service.isSetup {
return service
}
if service.Defaults == nil {
service.Defaults = make(map[string]string)
}
if service.Arguments == nil {
service.Arguments = make([]string, 0)
}
if service.Parameters == nil {
service.Parameters = make([]string, 0)
}
if service.InputFiles == nil {
service.InputFiles = make([]string, 0)
}
if service.OutputFiles == nil {
service.OutputFiles = make([]string, 0)
}
if service.InputZip == nil {
service.InputZip = make([]string, 0)
}
if service.OutputZip == nil {
service.OutputZip = make([]string, 0)
}
for _, arg := range service.CommandLine {
// Do we start with an # or other prefix? If so, add to proper
// structure and argument list
key := arg[1:]
prefix := arg[0]
isArg := false
if prefix == '#' {
isArg = true
service.Parameters = append(service.Parameters, key)
} else if prefix == '<' {
isArg = true
service.InputFiles = append(service.InputFiles, key)
} else if prefix == '>' {
isArg = true
service.OutputFiles = append(service.OutputFiles, key)
} else if prefix == '^' {
isArg = true
service.InputZip = append(service.InputZip, key)
} else if prefix == '~' {
isArg = true
service.OutputZip = append(service.OutputZip, key)
}
if isArg {
service.Arguments = append(service.Arguments, key)
}
}
service.isSetup = true
return service
}
// Return all services as JSON
func GetServices(w http.ResponseWriter, request *http.Request) {
json.NewEncoder(w).Encode(config)
}
// Particular info about a service as JSON
func GetService(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
service := config.ServiceMap[vars["id"]]
json.NewEncoder(w).Encode(service)
}
// Start up the service
func StartService(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
service := config.ServiceMap[vars["id"]]
if service == nil {
http.Error(w, "service not found", http.StatusNotFound)
return
}
log.Printf("Found service %v on endpoint %v", vars["id"], service.EndPoint)
// Pull out our arguments
err := request.ParseMultipartForm(10 * 1024 * 1024)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
job := Job{
UUID: uuid.NewV4().String(),
Service: service,
CommandLine: service.CommandLine,
FileMap: make(map[string]string),
ZipMap: make(map[string]string),
EndPoint: service.EndPoint,
Host: advertisedHost,
Port: advertisedPort,
Address: make([]string, 0),
}
// do we have an email address?
if request.MultipartForm.Value["mail"] != nil {
job.Address = request.MultipartForm.Value["mail"]
}
// Make a working directory
job.WorkingDirectory = filepath.Join(config.Directory, service.EndPoint, job.UUID)
err = os.MkdirAll(job.WorkingDirectory, 0755)
if err != nil {
log.Printf("Error making working directory: %v", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = job.ParseCommandLine(request)
if err != nil {
log.Printf("Error parsing command line: %v", err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
job.Start()
json.NewEncoder(w).Encode(&job)
jobs[job.UUID] = &job
}
func GetJob(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
job := jobs[vars["id"]]
json.NewEncoder(w).Encode(job)
}
func WaitForJob(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
job := jobs[vars["id"]]
job.Wait()
json.NewEncoder(w).Encode(job)
}
func GetJobFile(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
key := vars["id"]
job := jobs[key]
if job == nil {
http.Error(w, fmt.Sprintf("job %v does not exist", key), http.StatusInternalServerError)
return
}
file, ok := job.FileMap[vars["filename"]]
if ok {
fileToSend := filepath.Join(job.WorkingDirectory, file)
log.Printf("Request for %s, sending %s", vars["filename"], fileToSend)
w.Header().Set("Content-Disposition", "attachment;filename="+filepath.Base(file))
http.ServeFile(w, request, fileToSend)
return
}
file, ok = job.ZipMap[vars["filename"]]
if ok {
outputDirectory := filepath.Join(job.WorkingDirectory, file)
zipFilename := filepath.Base(vars["filename"])
if !strings.HasSuffix(zipFilename, ".zip") {
zipFilename += ".zip"
}
log.Printf("Requested %v: sending directory %v as a zip file", vars["filename"], outputDirectory)
w.Header().Set("Content-Disposition", "attachment; filename=\""+zipFilename+"\"")
gz := zip.NewWriter(w)
defer gz.Close()
filepath.Walk(outputDirectory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Don't create an entry for the root of the directory
if path == outputDirectory {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
// Take off the path, and a leading "/" to make the output relative
header.Name = strings.TrimPrefix(path, outputDirectory)
header.Name = strings.TrimPrefix(header.Name, "/")
// log.Printf("Adding %v from %v", header.Name, path)
if info.IsDir() {
header.Name += "/"
} else {
header.Method = zip.Deflate
}
writer, err := gz.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
return err
})
return
}
}
func GetJobZip(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
key := vars["id"]
job := jobs[key]
if job == nil {
http.Error(w, fmt.Sprintf("job %v does not exist", key), http.StatusNotFound)
return
}
w.Header().Set("Content-Disposition", "attachment;filename="+job.EndPoint+"-"+job.UUID+".zip")
gz := zip.NewWriter(w)
defer gz.Close()
filepath.Walk(job.WorkingDirectory, getDirectoryStreamer(job, gz))
}
func getDirectoryStreamer(job *Job, gz *zip.Writer) func(path string, info os.FileInfo, err error) error {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
log.Printf("Walking %v", path)
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = filepath.Join(job.UUID, strings.TrimPrefix(path, job.WorkingDirectory))
if info.IsDir() {
header.Name += "/"
} else {
header.Method = zip.Deflate
}
writer, err := gz.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(writer, file)
return err
}
}
func GetHealth(w http.ResponseWriter, request *http.Request) {
numberOfJobs := len(jobs)
if numberOfJobs <= config.WarnLevel {
w.WriteHeader(200)
} else if numberOfJobs <= config.CriticalLevel {
w.WriteHeader(429)
} else {
w.WriteHeader(500)
}
json.NewEncoder(w).Encode(jobs)
}