github.com/Kolosok86/http@v0.1.2/triv.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build ignore 6 7 package main 8 9 import ( 10 "expvar" 11 "flag" 12 "fmt" 13 "io" 14 "log" 15 "os" 16 "os/exec" 17 "strconv" 18 "strings" 19 "sync" 20 21 "github.com/Kolosok86/http" 22 ) 23 24 // hello world, the web server 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 // Simple counter server. POSTing to it will set the value. 33 type Counter struct { 34 mu sync.Mutex // protects n 35 n int 36 } 37 38 // This makes Counter satisfy the expvar.Var interface, so we can export 39 // it directly. 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 var buf strings.Builder 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 // simple flag server 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 // simple argument server 82 func ArgServer(w http.ResponseWriter, req *http.Request) { 83 for _, s := range os.Args { 84 fmt.Fprint(w, s, " ") 85 } 86 } 87 88 // a channel (just for the fun of it) 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 // exec a program, redirecting output. 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", "", "web root directory") 123 124 func main() { 125 flag.Parse() 126 127 // The counter is published as a variable directly. 128 ctr := new(Counter) 129 expvar.Publish("counter", ctr) 130 http.Handle("/counter", ctr) 131 http.Handle("/", http.HandlerFunc(Logger)) 132 if *webroot != "" { 133 http.Handle("/go/", http.StripPrefix("/go/", http.FileServer(http.Dir(*webroot)))) 134 } 135 http.Handle("/chan", ChanCreate()) 136 http.HandleFunc("/flags", FlagServer) 137 http.HandleFunc("/args", ArgServer) 138 http.HandleFunc("/go/hello", HelloServer) 139 http.HandleFunc("/date", DateServer) 140 log.Fatal(http.ListenAndServe("localhost:12345", nil)) 141 }