From 3cc702ba60587aaefbc953e93eedbe1f9dd9166e Mon Sep 17 00:00:00 2001 From: David Symonds Date: Sun, 19 Apr 2009 21:17:27 -0700 Subject: Initial cut at an "exported variables" (exvar) package. This handles integer-valued vars in a singleton struct, and exports functions for incrementing, setting and getting those vars, as well as rendering all the vars in a standard format. Demonstrate the use of the exvar package in the http/triv server. R=dcross,r APPROVED=r DELTA=122 (122 added, 0 deleted, 0 changed) OCL=27617 CL=27622 --- src/lib/http/triv.go | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/lib/http') diff --git a/src/lib/http/triv.go b/src/lib/http/triv.go index daf5eb8c0c..48e345e5e8 100644 --- a/src/lib/http/triv.go +++ b/src/lib/http/triv.go @@ -6,6 +6,7 @@ package main import ( "bufio"; + "exvar"; "flag"; "fmt"; "http"; @@ -17,15 +18,23 @@ import ( // hello world, the web server func HelloServer(c *http.Conn, req *http.Request) { + exvar.Increment("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); fmt.Fprintf(c, "counter = %d\n", ctr.n); ctr.n++; } @@ -92,6 +101,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)); err := http.ListenAndServe(":12345", nil); if err != nil { panic("ListenAndServe: ", err.String()) -- cgit v1.3