Repository navigation
Expand file tree
/
Copy pathhandler.go
More file actions
65 lines (57 loc) · 1.63 KB
/
Copy pathhandler.go
File metadata and controls
65 lines (57 loc) · 1.63 KB
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
package main
import (
"context"
"net/http"
"encoding/json"
"github.com/swarmpit/agent/setup"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/errdefs"
"github.com/gorilla/mux"
"io"
"log"
)
func Info(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(setup.GetArgs())
}
func Logs(cli *client.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var options = container.LogsOptions{
ShowStdout: true,
ShowStderr: true,
Timestamps: true,
Details: true,
}
params := mux.Vars(r)
container := params["container"]
query := r.URL.Query()
since := query.Get("since")
if since != "" {
options.Since = since
}
resp, err := cli.ContainerLogs(context.Background(), container, options)
if err != nil {
// A container that has gone away is routine: swarmpit keeps tailing
// a task's logs for a while after the task is replaced. Report it
// without the ERROR level, which otherwise floods the log every
// couple of seconds for as long as the tail is open.
if errdefs.IsNotFound(err) {
if routerArg.Debug.Http {
log.Printf("DEBUG: Container gone, no logs to serve: %s\n", err)
}
} else {
log.Printf("ERROR: Cannot obtain container logs: %s\n", err)
}
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer resp.Close()
content, err := io.ReadAll(resp)
if err != nil {
log.Printf("ERROR: Cannot read container logs: %s\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(string(content))
}
}