// Homework 35, problem 3: A counter that remembers. // // Start from your visit counter. Save the count to visits.json on every visit, // and load it when the server starts, so that it carries on after a restart. A // missing file means a count of 0. A broken file stops the server at start, with // a clear message. If saving fails, the visitor gets status 500, and the count // stays as it was. The file is created in the folder you run the program from. // // Run with: go run exercises/35/homework/03-saved-counter.go package main import ( "fmt" "net/http" ) const visitsFile = "visits.json" // TODO: add the mutex, the count, loadVisits and nextVisit here. func handleHome(w http.ResponseWriter, r *http.Request) { // TODO: write your code here, in place of this placeholder line: fmt.Fprintln(w, "TODO: replace this line, and save to", visitsFile) } func main() { // TODO: load the count here. http.HandleFunc("GET /{$}", handleHome) fmt.Println("Server running at http://localhost:8080") fmt.Println("Press Ctrl+C to stop it.") err := http.ListenAndServe("localhost:8080", nil) if err != nil { fmt.Println("Server stopped:", err) } }