-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-pipeline-logs.go
79 lines (61 loc) · 1.52 KB
/
api-pipeline-logs.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
package main
import (
"bufio"
"fmt"
"io"
"net/http"
"strings"
"github.com/gorilla/mux"
)
func HandlePipelineLogs(p *WebUIPlugin) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
workerGroup := vars["workerGroup"]
if workerGroup == "" {
http.Error(res, "missing worker group path parameter", http.StatusBadRequest)
return
}
id := vars["id"]
if id == "" {
http.Error(res, "missing id path parameter", http.StatusBadRequest)
return
}
entry, ok := p.History.Get(workerGroup, id)
if !ok {
http.Error(res, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
reader, err := entry.Logs.Reader()
if err != nil {
http.Error(res, fmt.Sprintf("error reading pipeline logs - %s", err), http.StatusInternalServerError)
return
}
headers := res.Header()
headers.Set("Content-Type", "text/plain")
headers.Set("X-Content-Type-Options", "nosniff")
flusher, hasFlusher := res.(http.Flusher)
if !hasFlusher {
headers.Set("Transfer-Encoding", "chunked")
}
res.WriteHeader(http.StatusOK)
if hasFlusher {
flusher.Flush()
}
io.WriteString(res, strings.Repeat("#:INIT:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", 13))
if hasFlusher {
flusher.Flush()
r := bufio.NewReader(reader)
for {
read, err := r.ReadBytes('\n')
if err != nil {
break
}
res.Write(read)
flusher.Flush()
}
} else {
io.Copy(res, reader)
}
reader.Close()
}
}