PageRenderTime 10ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/example/main.go

https://code.google.com/p/flow-go/
Go | 39 lines | 32 code | 7 blank | 0 comment | 4 complexity | 61a2e92a62e142cb0d2ec54553a7f05a MD5 | raw file
 1package main
 2
 3import (
 4	"fmt"
 5	"http"
 6
 7	"flow-go.googlecode.com/hg/flow"
 8)
 9
10const flowPath = "/flow"
11
12var flowMux = flow.NewMux(flowPath)
13
14func main() {
15	http.Handle(flowPath, flowMux)
16	http.HandleFunc("/", handler)
17	http.ListenAndServe("localhost:8080", nil)
18}
19
20func handler(w http.ResponseWriter, r *http.Request) {
21	if r.URL.Path != "/" {
22		http.Error(w, "not found", http.StatusNotFound)
23		return
24	}
25	flowMux.Become(myFlow, w, r)
26}
27
28func myFlow(c flow.Context) {
29	for n := 0; ; n++ {
30		rw, _ := c.Next()
31		fmt.Fprintf(rw, "n = %d<br>", n)
32		if n > 5 {
33			fmt.Fprint(rw, "Shutting down")
34			break
35		}
36		fmt.Fprintf(rw, `<a href="%s">Continue</a>`, c.URL())
37	}
38	c.Close()
39}