PageRenderTime 23ms CodeModel.GetById 12ms RepoModel.GetById 0ms 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
  1. package main
  2. import (
  3. "fmt"
  4. "http"
  5. "flow-go.googlecode.com/hg/flow"
  6. )
  7. const flowPath = "/flow"
  8. var flowMux = flow.NewMux(flowPath)
  9. func main() {
  10. http.Handle(flowPath, flowMux)
  11. http.HandleFunc("/", handler)
  12. http.ListenAndServe("localhost:8080", nil)
  13. }
  14. func handler(w http.ResponseWriter, r *http.Request) {
  15. if r.URL.Path != "/" {
  16. http.Error(w, "not found", http.StatusNotFound)
  17. return
  18. }
  19. flowMux.Become(myFlow, w, r)
  20. }
  21. func myFlow(c flow.Context) {
  22. for n := 0; ; n++ {
  23. rw, _ := c.Next()
  24. fmt.Fprintf(rw, "n = %d<br>", n)
  25. if n > 5 {
  26. fmt.Fprint(rw, "Shutting down")
  27. break
  28. }
  29. fmt.Fprintf(rw, `<a href="%s">Continue</a>`, c.URL())
  30. }
  31. c.Close()
  32. }