From 033217721725f1824cf6c39b7daf232d3cc44a93 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Mon, 4 Aug 2025 18:15:43 -0400 Subject: Add logs endpoint with dynamic values --- main.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index b9952b3..e6d3c44 100644 --- a/main.go +++ b/main.go @@ -8,14 +8,16 @@ import ( func main() { fs := http.FileServer(http.Dir("static")) + var logs [100]string + n := 0 + // Define a handler function for the root path http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + addRotLog(&logs, &n, fmt.Sprintf("Accessed %s", r.URL.Path)) // Serve the index.html file from the static directory http.StripPrefix("/", fs).ServeHTTP(w, r) }) - var logs [100]string - http.HandleFunc("/logs", createGetLogs(&logs)) http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { @@ -32,7 +34,19 @@ func main() { func createGetLogs(logs *[100]string) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - + for _, s := range logs { + fmt.Fprintln(w, s) + } } } +func addRotLog(logs *[100]string, last *int, value string) { + if *last == 100 { + for i := 0 ; i < 99; i++ { + logs[i] = logs[i+1] + } + } else { + logs[*last] = value + *last++ + } +} -- cgit