From 346dabf82308c1a606e057cf8beb81c2b5459779 Mon Sep 17 00:00:00 2001 From: Chris Hager Date: Wed, 23 Oct 2024 13:54:58 +0200 Subject: [PATCH] support plain text message api --- cmd/system-api/server.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmd/system-api/server.go b/cmd/system-api/server.go index 2a2d2f6..a18d3fc 100644 --- a/cmd/system-api/server.go +++ b/cmd/system-api/server.go @@ -162,6 +162,21 @@ func (s *Server) handleGetEvents(w http.ResponseWriter, r *http.Request) { s.eventsLock.RLock() defer s.eventsLock.RUnlock() + // respond either as JSON or plain text + if r.URL.Query().Get("format") == "text" { + // write events as plain text response + w.Header().Set("Content-Type", "text/plain") + for _, event := range s.events { + _, err := w.Write([]byte(event.ReceivedAt.Format("2006-01-02 15:04:05 UTC") + " \t " + event.Message + "\n")) + if err != nil { + s.log.Error("Failed to write event", "err", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + } + return + } + // write events as JSON response w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(s.events)