From b36fbd2a0ef06fc25e6d2eb2b4a5aca2ecdbd970 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 27 Dec 2016 12:51:36 -0800 Subject: [PATCH] Clean out examples not used on the site --- .../basic-authentication.go | 58 ----------- examples/canonical-hosts/canonical-hosts.go | 37 ------- examples/canonical-hosts/canonical-hosts.sh | 4 - .../graceful-shutdown/graceful-shutdown.go | 98 ------------------- .../graceful-shutdown/graceful-shutdown.sh | 6 -- examples/hello-web/hello-web.go | 13 --- examples/http-client/http-client.go | 15 --- examples/middleware/middleware.go | 22 ----- examples/middleware/middleware.sh | 10 -- examples/postgres/postgres.go | 79 --------------- examples/postgres/postgres.sh | 10 -- examples/redis/redis.go | 77 --------------- examples/redis/redis.sh | 6 -- examples/request-logging/request-logging.go | 40 -------- examples/request-routing/request-routing.go | 19 ---- examples/request-routing/request-routing.sh | 4 - examples/responses/responses.go | 19 ---- examples/responses/responses.sh | 5 - .../short-declarations/short-declarations.go | 9 -- .../short-declarations/short-declarations.sh | 2 - examples/static-content/static-content.go | 12 --- examples/static-content/static-content.sh | 5 - .../streaming-http-servers.go | 28 ------ 23 files changed, 578 deletions(-) delete mode 100644 examples/basic-authentication/basic-authentication.go delete mode 100644 examples/canonical-hosts/canonical-hosts.go delete mode 100644 examples/canonical-hosts/canonical-hosts.sh delete mode 100644 examples/graceful-shutdown/graceful-shutdown.go delete mode 100644 examples/graceful-shutdown/graceful-shutdown.sh delete mode 100644 examples/hello-web/hello-web.go delete mode 100644 examples/http-client/http-client.go delete mode 100644 examples/middleware/middleware.go delete mode 100644 examples/middleware/middleware.sh delete mode 100644 examples/postgres/postgres.go delete mode 100644 examples/postgres/postgres.sh delete mode 100644 examples/redis/redis.go delete mode 100644 examples/redis/redis.sh delete mode 100644 examples/request-logging/request-logging.go delete mode 100644 examples/request-routing/request-routing.go delete mode 100644 examples/request-routing/request-routing.sh delete mode 100644 examples/responses/responses.go delete mode 100644 examples/responses/responses.sh delete mode 100644 examples/short-declarations/short-declarations.go delete mode 100644 examples/short-declarations/short-declarations.sh delete mode 100644 examples/static-content/static-content.go delete mode 100644 examples/static-content/static-content.sh delete mode 100644 examples/streaming-http-servers/streaming-http-servers.go diff --git a/examples/basic-authentication/basic-authentication.go b/examples/basic-authentication/basic-authentication.go deleted file mode 100644 index efe193a..0000000 --- a/examples/basic-authentication/basic-authentication.go +++ /dev/null @@ -1,58 +0,0 @@ -package main - -import ( - "encoding/base64" - "fmt" - "net/http" - "strings" -) - -type Auth func(string, string) bool -type handler http.HandlerFunc - -func testAuth(r *http.Request, auth Auth) bool { - header := r.Header.Get("Authorization") - s := strings.SplitN(header, " ", 2) - if len(s) != 2 || s[0] != "Basic" { - return false - } - b, err := base64.StdEncoding.DecodeString(s[1]) - if err != nil { - return false - } - pair := strings.SplitN(string(b), ":", 2) - if len(pair) != 2 { - return false - } - return auth(pair[0], pair[1]) -} - -func requireAuth(w http.ResponseWriter, r *http.Request) { - w.Header().Set("WWW-Authenticate", - "Basic realm=\"private\"") - w.WriteHeader(401) - w.Write([]byte("401 Unauthorized\n")) -} - -func wrapAuth(h handler, a Auth) handler { - return func(w http.ResponseWriter, r *http.Request) { - if testAuth(r, a) { - h(w, r) - } else { - requireAuth(w, r) - } - } -} - -func hello(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, "Hello secret world!") -} - -func main() { - checkPassword := func(_, password string) bool { - return password == "supersecret" - } - handler1 := http.HanderFunc(hello) - handler2 := wrapAuth(handler1, checkPassword) - http.ListenAndServe(":5000", handler2) -} diff --git a/examples/canonical-hosts/canonical-hosts.go b/examples/canonical-hosts/canonical-hosts.go deleted file mode 100644 index 5a2574c..0000000 --- a/examples/canonical-hosts/canonical-hosts.go +++ /dev/null @@ -1,37 +0,0 @@ -package main - -import "net/http" -import "strings" -import "fmt" - -type handler http.HandlerFunc - -func hello(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/plain") - fmt.Fprintln(w, "Hello canonical world") -} - -func wrapCanonicalHost(f handler, chost string) handler { - return func(w http.ResponseWriter, r *http.Request) { - hostPort := strings.Split(r.Host, ":") - host := hostPort[0] - if host != chost { - fmt.Println("redirect to", chost) - hostPort[0] = chost - url := "http://" + - strings.Join(hostPort, ":") + - r.URL.String() - http.Redirect(w, r, url, 301) - return - } - f(w, r) - } -} - -func main() { - handler := wrapCanonicalHost(hello, "localhost") - http.HandleFunc("/", handler) - http.ListenAndServe(":5000", nil) -} - -// todo: comment about https canonicalization diff --git a/examples/canonical-hosts/canonical-hosts.sh b/examples/canonical-hosts/canonical-hosts.sh deleted file mode 100644 index f2582cc..0000000 --- a/examples/canonical-hosts/canonical-hosts.sh +++ /dev/null @@ -1,4 +0,0 @@ -$ go run canonical-hosts.go - -$ curl -i -L http://127.0.0.1:5000/go -$ curl -i -L http://127.0.0.1:5000/go diff --git a/examples/graceful-shutdown/graceful-shutdown.go b/examples/graceful-shutdown/graceful-shutdown.go deleted file mode 100644 index 07feac2..0000000 --- a/examples/graceful-shutdown/graceful-shutdown.go +++ /dev/null @@ -1,98 +0,0 @@ -package main - -import ( - "fmt" - "net" - "net/http" - "os" - "os/signal" - "sync/atomic" - "syscall" - "time" -) - -func slow(res http.ResponseWriter, req *http.Request) { - fmt.Println("respond at=start") - time.Sleep(time.Second * 5) - res.Header().Set("Content-Type", "text/plain") - fmt.Fprintln(res, "Finally.") - fmt.Println("respond at=finish") -} - -var connCount int64 = 0 - -type watchedConn struct { - net.Conn -} - -func (w *watchedConn) Close() error { - atomic.AddInt64(&connCount, -1) - return w.Conn.Close() -} - -type watchedListener struct { - net.Listener -} - -func (l *watchedListener) Accept() (net.Conn, error) { - conn, err := l.Listener.Accept() - if err != nil { - return nil, err - } - atomic.AddInt64(&connCount, 1) - return &watchedConn{Conn: conn}, nil -} - -func main() { - stop := make(chan bool, 1) - sig := make(chan os.Signal, 1) - - handler := http.HandlerFunc(slow) - server := &http.Server{Handler: handler} - fmt.Println("listen at=start") - listener, listenErr := net.Listen("tcp", ":5000") - if listenErr != nil { - panic(listenErr) - } - wListener := &watchedListener{Listener: listener} - fmt.Println("listen at=finish") - - go func() { - <-stop - fmt.Println("close at=start") - closeErr := wListener.Close() - if closeErr != nil { - panic(closeErr) - } - fmt.Println("close at=finish") - }() - - go func() { - signal.Notify( - sig, syscall.SIGINT, - syscall.SIGTERM) - fmt.Println("trap at=start") - <-sig - stop <- true - fmt.Println("trap at=finish") - }() - - fmt.Println("serve at=start") - server.Serve(wListener) - fmt.Println("serve at=finish") - for { - connCountCurrent := atomic.LoadInt64(&connCount) - if connCountCurrent > 0 { - fmt.Println("wait at=pending remaining=", - connCountCurrent) - time.Sleep(time.Second) - } else { - fmt.Println("wait at=finish remaining=", - connCountCurrent) - return - } - } -} - -// todo: pull in work from gobyexample-server -// todo: factor out to cut-and-pastable against normal app diff --git a/examples/graceful-shutdown/graceful-shutdown.sh b/examples/graceful-shutdown/graceful-shutdown.sh deleted file mode 100644 index 824b09b..0000000 --- a/examples/graceful-shutdown/graceful-shutdown.sh +++ /dev/null @@ -1,6 +0,0 @@ -$ go build graceful-shutdown.go -$ ./graceful-shutdown - -$ curl -i http://127.0.0.1:5000/ - -^C diff --git a/examples/hello-web/hello-web.go b/examples/hello-web/hello-web.go deleted file mode 100644 index 0433f2c..0000000 --- a/examples/hello-web/hello-web.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import "net/http" - -func hello(res http.ResponseWriter, req *http.Request) { - res.Header().Set("Content-Type", "text/plain") - res.Write([]byte("Hello web\n")) -} - -func main() { - http.HandleFunc("/", hello) - http.ListenAndServe(":5000", nil) -} diff --git a/examples/http-client/http-client.go b/examples/http-client/http-client.go deleted file mode 100644 index fdcc352..0000000 --- a/examples/http-client/http-client.go +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import "net/http" -import "io/ioutil" -import "fmt" - -func main() { - resp, err := http.Get("http://127.0.0.1:5000/") - if err != nil { - panic(err) - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - fmt.Print(string(body)) -} diff --git a/examples/middleware/middleware.go b/examples/middleware/middleware.go deleted file mode 100644 index 55d88e4..0000000 --- a/examples/middleware/middleware.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import "net/http" -import "fmt" - -func hello(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/plain") - fmt.Fprintln(w, "Hello wrapped world") -} - -func wrapMiddleware(f http.HandlerFunc) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("X-Wrapped", "true") - f(w, r) - } -} - -func main() { - handler := wrapMiddleware(hello) - http.HandleFunc("/", handler) - http.ListenAndServe(":5000", nil) -} diff --git a/examples/middleware/middleware.sh b/examples/middleware/middleware.sh deleted file mode 100644 index a7bd6b9..0000000 --- a/examples/middleware/middleware.sh +++ /dev/null @@ -1,10 +0,0 @@ -$ go run middleware.go - -$ curl -i http://127.0.0.1:5000/ -HTTP/1.1 200 OK -Content-Type: text/plain -Date: Tue, 02 Oct 2012 00:37:18 GMT -Transfer-Encoding: chunked -X-Wrapped: true - -Hello wrapped world diff --git a/examples/postgres/postgres.go b/examples/postgres/postgres.go deleted file mode 100644 index 37f2186..0000000 --- a/examples/postgres/postgres.go +++ /dev/null @@ -1,79 +0,0 @@ -package main - -import _ "github.com/bmizerany/pq" -import "database/sql" -import "time" -import "fmt" - -func main() { - conf := "dbname=gobyexample sslmode=disable" - db, openErr := sql.Open("postgres", conf) - if openErr != nil { - panic(openErr) - } - defer db.Close() - fmt.Println(db) - - createRep, createErr := db.Exec( - `CREATE TABLE items - (a int, b float, c boolean, - d text, e timestamp with time zone)`) - if createErr != nil { - panic(createErr) - } - fmt.Println(createRep) - - insertRep, insertErr := db.Exec( - `INSERT INTO items VALUES - (1, 2.0, false, - 'string', '2000-01-01T01:02:03Z')`) - if insertErr != nil { - panic(insertErr) - } - fmt.Println(insertRep) - - timeFmt := time.RFC3339 - t1, _ := time.Parse(timeFmt, "2000-04-08T03:02:01Z") - t2, _ := time.Parse(timeFmt, "2007-03-02T10:15:45Z") - minsertRep, minsertErr := db.Exec( - `Insert INTO items VALUES - ($1, $2, $3, $4, $5), - ($6, $7, $8, $9, $10)`, - 3, 7.0, true, "more", t1, - 5, 1.0, false, "less", t2) - if minsertErr != nil { - panic(minsertErr) - } - num, _ := minsertRep.RowsAffected() - fmt.Println(num) - - rows, selectErr := db.Query("SELECT * FROM items") - if selectErr != nil { - panic(selectErr) - } - defer rows.Close() - for rows.Next() { - var r1 int - var r2 float64 - var r3 bool - var r4 string - var r5 time.Time - rows.Scan(&r1, &r2, &r3, &r4, &r5) - fmt.Println(r1, r2, r3, r4, r5) - } - rowsErr := rows.Err() - if rowsErr != nil { - panic(rowsErr) - } - - dropRep, dropErr := db.Exec("DROP TABLE items") - if dropErr != nil { - panic(dropErr) - } - fmt.Println(dropRep) -} - -// todo: connection pooling & concurrency -// todo: re-connection -// todo: errors -// todo: database_url diff --git a/examples/postgres/postgres.sh b/examples/postgres/postgres.sh deleted file mode 100644 index 6bc1a7b..0000000 --- a/examples/postgres/postgres.sh +++ /dev/null @@ -1,10 +0,0 @@ -# First, be sure that you've installed Postgres -# and have a server running locally at port 5432. - -# Then create an example database. -$ createdb gobyexample - -# Now install the dependencies for the postgres -# example and try running it. -$ go get github.com/bmizerany/pq -$ go run postgres.go diff --git a/examples/redis/redis.go b/examples/redis/redis.go deleted file mode 100644 index 7ca9572..0000000 --- a/examples/redis/redis.go +++ /dev/null @@ -1,77 +0,0 @@ -package main - -import "github.com/fzzbt/radix/redis" -import "time" -import "fmt" - -func main() { - // initialize - conf := redis.DefaultConfig() - client := redis.NewClient(conf) - fmt.Println(conf) - fmt.Println(client) - - // set & get - setRep := client.Set("foo", "bar") - if setRep.Err != nil { - panic(setRep.Err) - } - fmt.Println(setRep) - getRep := client.Get("foo") - if getRep.Err != nil { - panic(getRep.Err) - } - getStr, _ := getRep.Str() - fmt.Println(getRep) - fmt.Println(getStr) - - // variadic calls - client.Set("foo1", "bar1") - client.Set("foo2", "bar2") - client.Set("foo3", "bar3") - mgetRep := client.Mget("foo1", "foo2", "foo3") - fmt.Println(mgetRep) - - // multi calls - mcRep := client.MultiCall(func(mc *redis.MultiCall) { - mc.Set("k1", "v1") - mc.Get("k1") - }) - if mcRep.Err != nil { - panic(mcRep.Err) - } - mcVal, _ := mcRep.Elems[1].Str() - fmt.Println(mcVal) - - // transactional calls - tRep := client.Transaction(func(mc *redis.MultiCall) { - mc.Set("k2", "v2") - mc.Get("k2") - }) - if tRep.Err != nil { - panic(tRep.Err) - } - tStr, _ := tRep.Elems[1].Str() - fmt.Println(tStr) - - // pubsub - msgHdlr := func(msg *redis.Message) { - fmt.Println(msg) - } - sub, subErr := client.Subscription(msgHdlr) - if subErr != nil { - panic(subErr) - } - defer sub.Close() - sub.Subscribe("chan1", "chan2") - sub.Psubscribe("chan*") - client.Publish("chan1", "foo") - sub.Unsubscribe("chan1") - client.Publish("chan2", "bar") - time.Sleep(time.Second) -} - -// todo: connection pooling & concurrency? -// todo: reconnection? -// todo: errors? -// todo: redis_url diff --git a/examples/redis/redis.sh b/examples/redis/redis.sh deleted file mode 100644 index 503c43a..0000000 --- a/examples/redis/redis.sh +++ /dev/null @@ -1,6 +0,0 @@ -# Start a Redis server. -$ redis-server - -# In another terminal, run the redis client: -$ go get github.com/fzzbt/radix/redis -$ go run redis.go diff --git a/examples/request-logging/request-logging.go b/examples/request-logging/request-logging.go deleted file mode 100644 index f5a4d41..0000000 --- a/examples/request-logging/request-logging.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import "net/http" -import "time" -import "fmt" - -func runLogging(logs chan string) { - for log := range logs { - fmt.Println(log) - } -} - -func wrapLogging(f http.HandlerFunc) http.HandlerFunc { - logs := make(chan string, 10000) - go runLogging(logs) - return func(w http.ResponseWriter, r *http.Request) { - start := time.Now() - f(w, r) - method := r.Method - path := r.URL.Path - elapsed := float64(time.Since(start)) / 1000000.0 - logs <- fmt.Sprintf( - "method=%s path=%s elapsed=%f", - method, path, elapsed) - } -} - -func hello(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/plain") - time.Sleep(time.Millisecond * 50) - fmt.Fprintln(w, "Hello logged world") -} - -func main() { - handler := wrapLogging(hello) - http.HandleFunc("/", handler) - http.ListenAndServe(":5000", nil) -} - -// todo: logging status code? diff --git a/examples/request-routing/request-routing.go b/examples/request-routing/request-routing.go deleted file mode 100644 index b1360ff..0000000 --- a/examples/request-routing/request-routing.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import "github.com/bmizerany/pat" -import "net/http" -import "fmt" - -func hello(w http.ResponseWriter, req *http.Request) { - fmt.Fprintln(w, "hello "+req.URL.Query().Get(":name")) -} - -func main() { - p := pat.New() - p.Get("/hello/:name", http.HandlerFunc(hello)) - http.ListenAndServe(":5000", p) -} - -// todo: consider gorilla-web -// todo: defaults -// todo: fallthroughs diff --git a/examples/request-routing/request-routing.sh b/examples/request-routing/request-routing.sh deleted file mode 100644 index c358da8..0000000 --- a/examples/request-routing/request-routing.sh +++ /dev/null @@ -1,4 +0,0 @@ -$ go get github.com/bmizerany/pat -$ go run request-routing.go - -$ curl -i http://127.0.0.1:5000/hello/gopher diff --git a/examples/responses/responses.go b/examples/responses/responses.go deleted file mode 100644 index 1a4b52c..0000000 --- a/examples/responses/responses.go +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import "net/http" -import "fmt" - -func hello(res http.ResponseWriter, req *http.Request) { - res.Header().Set("Content-Type", "text/plain") - if req.URL.Path == "/protected" { - res.WriteHeader(401) - fmt.Fprintln(res, "Not allowed") - } else { - fmt.Fprintln(res, "Hello") - } -} - -func main() { - http.HandleFunc("/", hello) - http.ListenAndServe(":5000", nil) -} diff --git a/examples/responses/responses.sh b/examples/responses/responses.sh deleted file mode 100644 index ebae391..0000000 --- a/examples/responses/responses.sh +++ /dev/null @@ -1,5 +0,0 @@ -$ go run xx-http-server-status-code.go - -$ curl -i http://127.0.0.1:5000/ | grep HTTP - -$ curl -i http://127.0.0.1:5000/protected | grep HTTP diff --git a/examples/short-declarations/short-declarations.go b/examples/short-declarations/short-declarations.go deleted file mode 100644 index bdd1b81..0000000 --- a/examples/short-declarations/short-declarations.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -import "fmt" - -func main() { - // `x := val` is shorthand for `var x type = val`. - x := "Hello var" - fmt.Println(x) -} diff --git a/examples/short-declarations/short-declarations.sh b/examples/short-declarations/short-declarations.sh deleted file mode 100644 index 0044451..0000000 --- a/examples/short-declarations/short-declarations.sh +++ /dev/null @@ -1,2 +0,0 @@ -$ go run short-declarations.go -Hello var diff --git a/examples/static-content/static-content.go b/examples/static-content/static-content.go deleted file mode 100644 index 41b2d81..0000000 --- a/examples/static-content/static-content.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import "net/http" - -func main() { - handler := http.FileServer(http.Dir("./")) - http.ListenAndServe(":5000", handler) -} - -// todo: index pages -// todo: disable listing -// todo: custom 404 handling diff --git a/examples/static-content/static-content.sh b/examples/static-content/static-content.sh deleted file mode 100644 index 135fcc3..0000000 --- a/examples/static-content/static-content.sh +++ /dev/null @@ -1,5 +0,0 @@ -$ go run static-content.go - -$ curl http://127.0.0.1:5000/ -$ curl http://127.0.0.1:5000/http-server-static.go -$ curl http://127.0.0.1:5000/missing diff --git a/examples/streaming-http-servers/streaming-http-servers.go b/examples/streaming-http-servers/streaming-http-servers.go deleted file mode 100644 index 009a045..0000000 --- a/examples/streaming-http-servers/streaming-http-servers.go +++ /dev/null @@ -1,28 +0,0 @@ -package main - -import ( - "fmt" - "net/http" - "time" -) - -func stream(resp http.ResponseWriter, req *http.Request) { - respf, ok := resp.(http.Flusher) - if !ok { - panic("not flushable") - } - fmt.Println("stream") - resp.WriteHeader(200) - for i := 0; i < 10; i++ { - n, err := resp.Write([]byte("tick\n")) - respf.Flush() - fmt.Println("tick", n, err) - time.Sleep(time.Second * 1) - } -} - -func main() { - http.HandleFunc("/", stream) - fmt.Println("serve") - http.ListenAndServe(":5000", nil) -}