diff options
Diffstat (limited to 'api/api.go')
| -rw-r--r-- | api/api.go | 29 |
1 files changed, 28 insertions, 1 deletions
@@ -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" |
