Logs refactoring after #780

This commit is contained in:
Alex X
2023-12-11 18:07:38 +03:00
parent 1f3a32023f
commit d3bc18c369
4 changed files with 199 additions and 163 deletions

View File

@@ -254,35 +254,15 @@ func restartHandler(w http.ResponseWriter, r *http.Request) {
go shell.Restart()
}
// logHandler handles HTTP requests for log buffer operations.
// It supports two HTTP methods:
// - GET: Retrieves the content of in-memory log and sends it back to the client as plain text.
// - DELETE: Clear the in-memory log buffer.
//
// The function expects a valid http.ResponseWriter and an http.Request as parameters.
// For a GET request, it reads the log from in-memory buffer and writes
// the content to the response writer with a "text/plain" content type.
//
// For a DELETE request, it clears the in-memory buffer.
//
// For any other HTTP method, it responds with an HTTP 400 (Bad Request) status.
//
// Parameters:
// - w http.ResponseWriter: The response writer to write the HTTP response to.
// - r *http.Request: The HTTP request object containing the request details.
//
// No return values are provided since the function writes directly to the response writer.
func logHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
// Send current state of the log file immediately
data := app.LogCollector.Bytes()
Response(w, data, "text/plain")
w.Header().Set("Content-Type", "application/jsonlines")
_, _ = app.MemoryLog.WriteTo(w)
case "DELETE":
app.LogCollector.Reset()
Response(w, "Log truncated", "text/plain")
app.MemoryLog.Reset()
Response(w, "OK", "text/plain")
default:
http.Error(w, "Method not allowed", http.StatusBadRequest)
}