Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "golang.org/x/net/trace"
  5. "net/http"
  6.  
  7. )
  8.  
  9.  
  10. var p *Fetcher
  11. type Fetcher struct {
  12. domain string
  13. events trace.EventLog
  14. }
  15.  
  16. func NewFetcher(domain string) *Fetcher {
  17. return &Fetcher{
  18. domain,
  19. trace.NewEventLog("mypkg.Fetcher", domain),
  20. }
  21. }
  22.  
  23. func (f *Fetcher) Fetch(path string) (string, error) {
  24. resp, err := http.Get("http://" + f.domain + "/" + path)
  25. if err != nil {
  26. f.events.Errorf("Get(%q) = %v", path, err)
  27. return "", err
  28. }
  29. f.events.Printf("Get(%q) = %s", path, resp.Status)
  30. return "",err
  31. }
  32.  
  33. func (f *Fetcher) Close() error {
  34. f.events.Finish()
  35. return nil
  36. }
  37.  
  38. func fooHandler(w http.ResponseWriter, req *http.Request) {
  39. trace.Traces(w,req)
  40. tr := trace.New("mypkg.Foo", "www.google.com")
  41. defer tr.Finish()
  42.  
  43. p=NewFetcher("www.google.com")
  44. str,err := p.Fetch("")
  45. if err != nil {
  46. panic(err)
  47. }
  48. tr.LazyPrintf("some event %q happened", str)
  49. }
  50.  
  51. func main(){
  52. defer p.Close()
  53.  
  54. http.HandleFunc("/",fooHandler)
  55. err:= http.ListenAndServe(":7889",nil)
  56. if err != nil {
  57. panic(err)
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement