From 424355dfb6c65e688057619a694542abd4623115 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Sat, 5 Oct 2024 17:54:56 -0400 Subject: Add endpoint for foundry container's logs --- api/api.go | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'api/api.go') diff --git a/api/api.go b/api/api.go index c74723d..957651e 100644 --- a/api/api.go +++ b/api/api.go @@ -3,9 +3,11 @@ package api import ( "context" "encoding/json" + "io" "log/slog" "net/http" "os" + "strconv" "time" ) @@ -40,7 +42,7 @@ func respondOnline(w http.ResponseWriter, status ServerStatus) { jsonData, err := json.Marshal(status) if err != nil { http.Error(w, "Error constructing response", http.StatusInternalServerError) - Logger.Error("Error marshalling site status data") + Logger.Error("Error marshalling site status data", slog.Any("err", err)) return } @@ -61,6 +63,31 @@ func VttRedirect(w http.ResponseWriter, req *http.Request) { http.Redirect(w, req, VTT_URL, http.StatusMovedPermanently) } +func VttLogs(w http.ResponseWriter, req *http.Request) { + var lines uint = 0 + + linesParam := req.URL.Query().Get("lines") + if linesParam != "" { + lines_64, err := strconv.ParseUint(linesParam, 10, 64) + if err != nil { + Logger.ErrorContext(req.Context(), "Invalid line count in vtt logs request", slog.Any("err", err)) + http.Error(w, "Invalid line count in request", http.StatusBadRequest) + return + } + + lines = uint(lines_64) + } + + logReader, err := vttLogs(req.Context(), lines) + if err != nil { + http.Error(w, "Error occured while getting logs", http.StatusInternalServerError) + return + } + defer logReader.Close() + + io.Copy(w, logReader) +} + func SiteOnline(w http.ResponseWriter, req *http.Request) { const URL string = "https://dnd.jpappel.xyz" -- cgit v1.2.3