Skip to content

Commit 1cdaa1d

Browse files
authored
add a simple /logs endpoint (#7)
## 📝 Summary Convenience `/logs` endpoint that's easier to communicate and remember. `curl localhost:3535/logs` does the same thing as `curl localhost:3535/api/v1/events?format=text`. A user experience boost. --- ## ✅ I have run these commands * [x] `make lint` * [x] `make test` * [x] `go mod tidy`
1 parent a635aa5 commit 1cdaa1d

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

systemapi/server.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (s *Server) getRouter() http.Handler {
8888
mux.Get("/livez", s.handleLivenessCheck)
8989
mux.Get("/api/v1/new_event", s.handleNewEvent)
9090
mux.Get("/api/v1/events", s.handleGetEvents)
91+
mux.Get("/logs", s.handleGetLogs)
9192
mux.Get("/api/v1/actions/{action}", s.handleAction)
9293
mux.Post("/api/v1/file-upload/{file}", s.handleFileUpload)
9394

@@ -174,26 +175,32 @@ func (s *Server) handleNewEvent(w http.ResponseWriter, r *http.Request) {
174175
w.WriteHeader(http.StatusOK)
175176
}
176177

177-
func (s *Server) handleGetEvents(w http.ResponseWriter, r *http.Request) {
178+
func (s *Server) writeEventsAsText(w http.ResponseWriter) {
178179
s.eventsLock.RLock()
179180
defer s.eventsLock.RUnlock()
180181

182+
w.Header().Set("Content-Type", "text/plain")
183+
for _, event := range s.events {
184+
_, err := w.Write([]byte(event.ReceivedAt.Format(time.RFC3339) + " \t " + event.Message + "\n"))
185+
if err != nil {
186+
s.log.Error("Failed to write event", "err", err)
187+
w.WriteHeader(http.StatusInternalServerError)
188+
return
189+
}
190+
}
191+
}
192+
193+
func (s *Server) handleGetEvents(w http.ResponseWriter, r *http.Request) {
181194
// respond either as JSON or plain text
182195
if r.URL.Query().Get("format") == "text" {
183196
// write events as plain text response
184-
w.Header().Set("Content-Type", "text/plain")
185-
for _, event := range s.events {
186-
_, err := w.Write([]byte(event.ReceivedAt.Format(time.RFC3339) + " \t " + event.Message + "\n"))
187-
if err != nil {
188-
s.log.Error("Failed to write event", "err", err)
189-
w.WriteHeader(http.StatusInternalServerError)
190-
return
191-
}
192-
}
197+
s.writeEventsAsText(w)
193198
return
194199
}
195200

196201
// write events as JSON response
202+
s.eventsLock.RLock()
203+
defer s.eventsLock.RUnlock()
197204
w.Header().Set("Content-Type", "application/json")
198205
err := json.NewEncoder(w).Encode(s.events)
199206
if err != nil {
@@ -203,6 +210,10 @@ func (s *Server) handleGetEvents(w http.ResponseWriter, r *http.Request) {
203210
}
204211
}
205212

213+
func (s *Server) handleGetLogs(w http.ResponseWriter, r *http.Request) {
214+
s.writeEventsAsText(w)
215+
}
216+
206217
func (s *Server) handleAction(w http.ResponseWriter, r *http.Request) {
207218
action := chi.URLParam(r, "action")
208219
s.log.Info("Received action", "action", action)

0 commit comments

Comments
 (0)