OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "expvar" | 9 "expvar" |
10 "flag" | 10 "flag" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 fmt.Fprint(w, "counter reset\n") | 49 fmt.Fprint(w, "counter reset\n") |
50 } | 50 } |
51 } | 51 } |
52 fmt.Fprintf(w, "counter = %d\n", ctr.n) | 52 fmt.Fprintf(w, "counter = %d\n", ctr.n) |
53 } | 53 } |
54 | 54 |
55 // simple flag server | 55 // simple flag server |
56 var booleanflag = flag.Bool("boolean", true, "another flag for testing") | 56 var booleanflag = flag.Bool("boolean", true, "another flag for testing") |
57 | 57 |
58 func FlagServer(w http.ResponseWriter, req *http.Request) { | 58 func FlagServer(w http.ResponseWriter, req *http.Request) { |
59 » w.SetHeader("content-type", "text/plain; charset=utf-8") | 59 » w.Header.Set("Content-Type", "text/plain; charset=utf-8") |
60 fmt.Fprint(w, "Flags:\n") | 60 fmt.Fprint(w, "Flags:\n") |
61 flag.VisitAll(func(f *flag.Flag) { | 61 flag.VisitAll(func(f *flag.Flag) { |
62 if f.Value.String() != f.DefValue { | 62 if f.Value.String() != f.DefValue { |
63 fmt.Fprintf(w, "%s = %s [default = %s]\n", f.Name, f.Val
ue.String(), f.DefValue) | 63 fmt.Fprintf(w, "%s = %s [default = %s]\n", f.Name, f.Val
ue.String(), f.DefValue) |
64 } else { | 64 } else { |
65 fmt.Fprintf(w, "%s = %s\n", f.Name, f.Value.String()) | 65 fmt.Fprintf(w, "%s = %s\n", f.Name, f.Value.String()) |
66 } | 66 } |
67 }) | 67 }) |
68 } | 68 } |
69 | 69 |
(...skipping 16 matching lines...) Expand all Loading... |
86 }(c) | 86 }(c) |
87 return c | 87 return c |
88 } | 88 } |
89 | 89 |
90 func (ch Chan) ServeHTTP(w http.ResponseWriter, req *http.Request) { | 90 func (ch Chan) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
91 io.WriteString(w, fmt.Sprintf("channel send #%d\n", <-ch)) | 91 io.WriteString(w, fmt.Sprintf("channel send #%d\n", <-ch)) |
92 } | 92 } |
93 | 93 |
94 // exec a program, redirecting output | 94 // exec a program, redirecting output |
95 func DateServer(rw http.ResponseWriter, req *http.Request) { | 95 func DateServer(rw http.ResponseWriter, req *http.Request) { |
96 » rw.SetHeader("content-type", "text/plain; charset=utf-8") | 96 » rw.Header().Set("Content-Type", "text/plain; charset=utf-8") |
97 r, w, err := os.Pipe() | 97 r, w, err := os.Pipe() |
98 if err != nil { | 98 if err != nil { |
99 fmt.Fprintf(rw, "pipe: %s\n", err) | 99 fmt.Fprintf(rw, "pipe: %s\n", err) |
100 return | 100 return |
101 } | 101 } |
102 p, err := os.StartProcess("/bin/date", []string{"date"}, os.Environ(), "
", []*os.File{nil, w, w}) | 102 p, err := os.StartProcess("/bin/date", []string{"date"}, os.Environ(), "
", []*os.File{nil, w, w}) |
103 defer r.Close() | 103 defer r.Close() |
104 w.Close() | 104 w.Close() |
105 if err != nil { | 105 if err != nil { |
106 fmt.Fprintf(rw, "fork/exec: %s\n", err) | 106 fmt.Fprintf(rw, "fork/exec: %s\n", err) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 http.Handle("/flags", http.HandlerFunc(FlagServer)) | 141 http.Handle("/flags", http.HandlerFunc(FlagServer)) |
142 http.Handle("/args", http.HandlerFunc(ArgServer)) | 142 http.Handle("/args", http.HandlerFunc(ArgServer)) |
143 http.Handle("/go/hello", http.HandlerFunc(HelloServer)) | 143 http.Handle("/go/hello", http.HandlerFunc(HelloServer)) |
144 http.Handle("/chan", ChanCreate()) | 144 http.Handle("/chan", ChanCreate()) |
145 http.Handle("/date", http.HandlerFunc(DateServer)) | 145 http.Handle("/date", http.HandlerFunc(DateServer)) |
146 err := http.ListenAndServe(":12345", nil) | 146 err := http.ListenAndServe(":12345", nil) |
147 if err != nil { | 147 if err != nil { |
148 log.Panicln("ListenAndServe:", err) | 148 log.Panicln("ListenAndServe:", err) |
149 } | 149 } |
150 } | 150 } |
OLD | NEW |