diff options
author | Robby Zambito <contact@robbyzambito.me> | 2025-08-04 18:15:43 -0400 |
---|---|---|
committer | Robby Zambito <contact@robbyzambito.me> | 2025-08-04 18:15:43 -0400 |
commit | 033217721725f1824cf6c39b7daf232d3cc44a93 (patch) | |
tree | 84ba69f2faabe6f658e1fc2843102cc8d47e89f5 /main.go | |
parent | 844f11df7a2cc74ff6dffd50e20095e8e9f3b2f4 (diff) |
Add logs endpoint with dynamic values
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 20 |
1 files changed, 17 insertions, 3 deletions
@@ -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++ + } +} |