-
Notifications
You must be signed in to change notification settings - Fork 50
/
executor_routes.go
80 lines (63 loc) · 1.74 KB
/
executor_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
package executor
import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"io"
"net/http"
"time"
"github.com/codedellemc/libstorage/api/server/executors"
"github.com/codedellemc/libstorage/api/server/httputils"
"github.com/codedellemc/libstorage/api/types"
)
func (r *router) executors(
ctx types.Context,
w http.ResponseWriter,
req *http.Request,
store types.Store) error {
var reply types.ExecutorsMap = map[string]*types.ExecutorInfo{}
for ei := range executors.ExecutorInfos() {
reply[ei.Name] = &ei.ExecutorInfo
}
return httputils.WriteJSON(w, http.StatusOK, reply)
}
func (r *router) executorInspect(
ctx types.Context,
w http.ResponseWriter,
req *http.Request,
store types.Store) error {
ei, err := executors.ExecutorInfoInspect(store.GetString("executor"), true)
if err != nil {
return err
}
return writeFile(w, ei)
}
func (r *router) executorHead(
ctx types.Context,
w http.ResponseWriter,
req *http.Request,
store types.Store) error {
ei, err := executors.ExecutorInfoInspect(store.GetString("executor"), false)
if err != nil {
return err
}
return writeFile(w, ei)
}
const lastModTimeFmt = "Mon, 02 Jan 2006 15:04:05 MST"
func writeFile(w http.ResponseWriter, ei *executors.ExecutorInfoEx) error {
w.Header().Add("Accept-Ranges", "bytes")
w.Header().Add("Content-Length", fmt.Sprintf("%d", ei.Size))
w.Header().Add("Content-Type", "application/octet-stream")
w.Header().Add("Last-Modified",
time.Unix(ei.LastModified, 0).Format(lastModTimeFmt))
hexBuf, _ := hex.DecodeString(ei.MD5Checksum)
b64str := base64.StdEncoding.EncodeToString(hexBuf)
w.Header().Add("Content-MD5", b64str)
if len(ei.Data) > 0 {
if _, err := io.Copy(w, bytes.NewReader(ei.Data)); err != nil {
return err
}
}
return nil
}