From c4fd3f6ff60189ba18446d538523b646f37f930b Mon Sep 17 00:00:00 2001
From: Ferenc Szabo
For brevity, let's ignore POSTs and assume HTTP requests are always -GETs; that simplification does not affect the way the handlers are -set up. Here's a trivial but complete implementation of a handler to -count the number of times the -page is visited. +GETs; that simplification does not affect the way the handlers are set up. +Here's a trivial implementation of a handler to count the number of times +the page is visited.
// Simple counter server. @@ -2355,6 +2354,11 @@ func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {(Keeping with our theme, note how
+Fprintf
can print to anhttp.ResponseWriter
.) +In a real server, access toctr.n
would need protection from +concurrent access. +See thesync
andatomic
packages for suggestions. +For reference, here's how to attach such a server to a node on the URL tree.