Source file
src/net/http/triv.go
1
2
3
4
5
6
7
8 package main
9
10 import (
11 "bytes"
12 "expvar"
13 "flag"
14 "fmt"
15 "io"
16 "log"
17 "net/http"
18 "os"
19 "os/exec"
20 "strconv"
21 "sync"
22 )
23
24
25 var helloRequests = expvar.NewInt("hello-requests")
26
27 func HelloServer(w http.ResponseWriter, req *http.Request) {
28 helloRequests.Add(1)
29 io.WriteString(w, "hello, world!\n")
30 }
31
32
33 type Counter struct {
34 mu sync.Mutex
35 n int
36 }
37
38
39
40 func (ctr *Counter) String() string {
41 ctr.mu.Lock()
42 defer ctr.mu.Unlock()
43 return fmt.Sprintf("%d", ctr.n)
44 }
45
46 func (ctr *Counter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
47 ctr.mu.Lock()
48 defer ctr.mu.Unlock()
49 switch req.Method {
50 case "GET":
51 ctr.n++
52 case "POST":
53 buf := new(bytes.Buffer)
54 io.Copy(buf, req.Body)
55 body := buf.String()
56 if n, err := strconv.Atoi(body); err != nil {
57 fmt.Fprintf(w, "bad POST: %v\nbody: [%v]\n", err, body)
58 } else {
59 ctr.n = n
60 fmt.Fprint(w, "counter reset\n")
61 }
62 }
63 fmt.Fprintf(w, "counter = %d\n", ctr.n)
64 }
65
66
67 var booleanflag = flag.Bool("boolean", true, "another flag for testing")
68
69 func FlagServer(w http.ResponseWriter, req *http.Request) {
70 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
71 fmt.Fprint(w, "Flags:\n")
72 flag.VisitAll(func(f *flag.Flag) {
73 if f.Value.String() != f.DefValue {
74 fmt.Fprintf(w, "%s = %s [default = %s]\n", f.Name, f.Value.String(), f.DefValue)
75 } else {
76 fmt.Fprintf(w, "%s = %s\n", f.Name, f.Value.String())
77 }
78 })
79 }
80
81
82 func ArgServer(w http.ResponseWriter, req *http.Request) {
83 for _, s := range os.Args {
84 fmt.Fprint(w, s, " ")
85 }
86 }
87
88
89 type Chan chan int
90
91 func ChanCreate() Chan {
92 c := make(Chan)
93 go func(c Chan) {
94 for x := 0; ; x++ {
95 c <- x
96 }
97 }(c)
98 return c
99 }
100
101 func (ch Chan) ServeHTTP(w http.ResponseWriter, req *http.Request) {
102 io.WriteString(w, fmt.Sprintf("channel send #%d\n", <-ch))
103 }
104
105
106 func DateServer(rw http.ResponseWriter, req *http.Request) {
107 rw.Header().Set("Content-Type", "text/plain; charset=utf-8")
108
109 date, err := exec.Command("/bin/date").Output()
110 if err != nil {
111 http.Error(rw, err.Error(), http.StatusInternalServerError)
112 return
113 }
114 rw.Write(date)
115 }
116
117 func Logger(w http.ResponseWriter, req *http.Request) {
118 log.Print(req.URL)
119 http.Error(w, "oops", http.StatusNotFound)
120 }
121
122 var webroot = flag.String("root", os.Getenv("HOME"), "web root directory")
123
124 func main() {
125 flag.Parse()
126
127
128 ctr := new(Counter)
129 expvar.Publish("counter", ctr)
130 http.Handle("/counter", ctr)
131 http.Handle("/", http.HandlerFunc(Logger))
132 http.Handle("/go/", http.StripPrefix("/go/", http.FileServer(http.Dir(*webroot))))
133 http.Handle("/chan", ChanCreate())
134 http.HandleFunc("/flags", FlagServer)
135 http.HandleFunc("/args", ArgServer)
136 http.HandleFunc("/go/hello", HelloServer)
137 http.HandleFunc("/date", DateServer)
138 log.Fatal(http.ListenAndServe(":12345", nil))
139 }
140
View as plain text