]> Cypherpunks repositories - gostls13.git/commitdiff
Add a HTTP handler to the exvar package.
authorDavid Symonds <dsymonds@golang.org>
Tue, 28 Apr 2009 11:26:07 +0000 (04:26 -0700)
committerDavid Symonds <dsymonds@golang.org>
Tue, 28 Apr 2009 11:26:07 +0000 (04:26 -0700)
R=r
APPROVED=r
DELTA=20  (11 added, 6 deleted, 3 changed)
OCL=27782
CL=27950

src/lib/exvar.go
src/lib/http/triv.go

index a5d91f334b55fc1c43fb01420398a098cbbb289f..6f69614ebda52f5dff603fa86fd0424c8562da4e 100644 (file)
@@ -8,6 +8,8 @@ package exvar
 
 import (
        "fmt";
+       "http";
+       "io";
 )
 
 // If mismatched names are used (e.g. calling IncrementInt on a mapVar), the
@@ -220,3 +222,12 @@ func String() string {
        });
        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());
+}
index 48e345e5e83f9f499f21e7519f53a2bbb4ca790e..7678b3fff8f077a6c348392eef53cec6a10bd4fc 100644 (file)
@@ -18,23 +18,17 @@ import (
 
 // 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++;
 }
@@ -101,7 +95,7 @@ func main() {
        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())