diff options
-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++ + } +} |