-
Notifications
You must be signed in to change notification settings - Fork 13
/
handler.go
54 lines (46 loc) · 1.18 KB
/
handler.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
package main
import (
"context"
"net/http"
"encoding/json"
"github.com/swarmpit/agent/setup"
"github.com/docker/docker/client"
"github.com/docker/docker/api/types"
"github.com/gorilla/mux"
"log"
"io/ioutil"
)
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 = types.ContainerLogsOptions{
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 {
log.Printf("ERROR: Cannot obtain container logs: %s\n", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
defer resp.Close()
content, err := ioutil.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))
}
}