import (
"fmt";
+ "http";
+ "io";
)
// If mismatched names are used (e.g. calling IncrementInt on a mapVar), the
});
return s
}
+
+// ExvarHandler is a HTTP handler that displays exported variables.
+// Use it like this:
+// http.Handle("/exvar", http.HandlerFunc(exvar.ExvarHandler));
+func ExvarHandler(c *http.Conn, req *http.Request) {
+ // TODO(dsymonds): Support different output= args.
+ c.SetHeader("content-type", "text/plain; charset=utf-8");
+ io.WriteString(c, String());
+}
// hello world, the web server
func HelloServer(c *http.Conn, req *http.Request) {
- exvar.Increment("hello-requests", 1);
+ exvar.IncrementInt("hello-requests", 1);
io.WriteString(c, "hello, world!\n");
}
-// Handler for /exvar requests.
-func ExvarServer(c *http.Conn, req *http.Request) {
- c.SetHeader("content-type", "text/plain; charset=utf-8");
- io.WriteString(c, exvar.String());
-}
-
// simple counter server
type Counter struct {
n int;
}
func (ctr *Counter) ServeHTTP(c *http.Conn, req *http.Request) {
- exvar.Increment("counter-requests", 1);
+ exvar.IncrementInt("counter-requests", 1);
fmt.Fprintf(c, "counter = %d\n", ctr.n);
ctr.n++;
}
http.Handle("/args", http.HandlerFunc(ArgServer));
http.Handle("/go/hello", http.HandlerFunc(HelloServer));
http.Handle("/chan", ChanCreate());
- http.Handle("/exvar", http.HandlerFunc(ExvarServer));
+ http.Handle("/exvar", http.HandlerFunc(exvar.ExvarHandler));
err := http.ListenAndServe(":12345", nil);
if err != nil {
panic("ListenAndServe: ", err.String())