Add new functionality to download logs (#917)

This commit is contained in:
Amir Raminfar
2021-01-03 14:27:17 -08:00
committed by GitHub
parent b87b0d018f
commit ac008519bc
3 changed files with 58 additions and 0 deletions

View File

@@ -8,6 +8,14 @@
<div class="column is-clipped">
<container-stat :stat="container.stat" :state="container.state"></container-stat>
</div>
<div class="column is-narrow">
<a class="button is-small is-outlined" id="download" :href="`${base}/api/logs/download?id=${container.id}`">
<span class="icon">
<icon name="save"></icon>
</span>
Download
</a>
</div>
<div class="column is-narrow" v-if="closable">
<button class="delete is-medium" @click="$emit('close')"></button>
</div>
@@ -26,6 +34,8 @@ import LogViewerWithSource from "./LogViewerWithSource";
import ScrollableView from "./ScrollableView";
import ContainerTitle from "./ContainerTitle";
import ContainerStat from "./ContainerStat";
import Icon from "./Icon";
import config from "../store/config";
export default {
props: {
@@ -51,12 +61,16 @@ export default {
ScrollableView,
ContainerTitle,
ContainerStat,
Icon,
},
computed: {
...mapGetters(["allContainersById"]),
container() {
return this.allContainersById[this.id];
},
base() {
return config.base;
},
},
};
</script>
@@ -73,4 +87,16 @@ button.delete {
opacity: 1;
}
}
#download.button {
.icon {
margin-right: 5px;
height: 80%;
}
&:hover {
color: var(--primary-color);
border-color: var(--primary-color);
}
}
</style>

File diff suppressed because one or more lines are too long

View File

@@ -3,6 +3,7 @@ package web
import (
"bufio"
"context"
"compress/gzip"
"encoding/json"
"fmt"
"html/template"
@@ -55,6 +56,7 @@ func createRouter(h *handler) *mux.Router {
}
s := r.PathPrefix(base).Subrouter()
s.HandleFunc("/api/logs/stream", h.streamLogs)
s.HandleFunc("/api/logs/download", h.downloadLogs)
s.HandleFunc("/api/logs", h.fetchLogsBetweenDates)
s.HandleFunc("/api/events/stream", h.streamEvents)
s.HandleFunc("/version", h.version)
@@ -117,6 +119,33 @@ func (h *handler) fetchLogsBetweenDates(w http.ResponseWriter, r *http.Request)
io.Copy(w, reader)
}
func (h *handler) downloadLogs(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
container, err := h.client.FindContainer(id)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
now := time.Now()
from := time.Unix(container.Created, 0)
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%v.log.gz", container.ID))
w.Header().Set("Content-Type", "application/octet-stream")
zw := gzip.NewWriter(w)
defer zw.Close()
zw.Name = fmt.Sprintf("%v.log", container.ID)
zw.Comment = "Logs generated by Dozzle"
zw.ModTime = now
reader, err := h.client.ContainerLogsBetweenDates(r.Context(), container.ID, from, now)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
io.Copy(zw, reader)
}
func (h *handler) streamLogs(w http.ResponseWriter, r *http.Request) {
id := r.URL.Query().Get("id")
if id == "" {