all less than 60 characters

This commit is contained in:
Mark McGranaghan 2012-10-01 17:42:07 -07:00
parent 0829ebc996
commit 13f1925439
11 changed files with 53 additions and 36 deletions

View File

@ -12,4 +12,4 @@ func main() {
} }
} }
// todo: credit http://code.google.com/p/go-wiki/wiki/RateLimiting // todo: credit code.google.com/p/go-wiki/wiki/RateLimiting

View File

@ -2,8 +2,8 @@
package main package main
// Use the `os` package to list, set, and get // Use the `os` package to list, set, and get environment
// environment variables. // variables.
import "os" import "os"
import "strings" import "strings"
import "fmt" import "fmt"

View File

@ -27,4 +27,4 @@ func main() {
// todo: missing subject, cc, bcc // todo: missing subject, cc, bcc
// todo: attachements // todo: attachements
// todo: plain/multi-type emails // todo: plain/multi-type emails
// todo: https://github.com/mtoader/google-go-lang-idea-plugin/issues/112 // todo: mtoader/google-go-lang-idea-plugin/issues/112

View File

@ -6,23 +6,27 @@ import "net/http"
import "strings" import "strings"
import "fmt" import "fmt"
func hello(res http.ResponseWriter, req *http.Request) { type handler http.HandlerFunc
res.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(res, "Hello canonical world") func hello(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Hello canonical world")
} }
func wrapCanonicalHost(f http.HandlerFunc, host string) http.HandlerFunc { func wrapCanonicalHost(f handler, chost string) handler {
return func(res http.ResponseWriter, req *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
hostPort := strings.Split(req.Host, ":") hostPort := strings.Split(r.Host, ":")
host := hostPort[0] host := hostPort[0]
if host != canonicalHost { if host != chost {
fmt.Println("redirecting from", host, "to", canonicalHost) fmt.Println("redirect to", chost)
hostPort[0] = canonicalHost hostPort[0] = chost
url := "http://" + strings.Join(hostPort, ":") + req.URL.String() url := "http://" +
http.Redirect(res, req, url, 301) strings.Join(hostPort, ":") +
r.URL.String()
http.Redirect(w, r, url, 301)
return return
} }
f(res, req) f(w, r)
} }
} }

View File

@ -5,16 +5,15 @@ package main
import "net/http" import "net/http"
import "fmt" import "fmt"
func hello(res http.ResponseWriter, req *http.Request) { func hello(w http.ResponseWriter, r *http.Request) {
res.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(res, "Hello wrapped world") fmt.Fprintln(w, "Hello wrapped world")
} }
func wrapMiddleware(f http.HandlerFunc) http.HandlerFunc { func wrapMiddleware(f http.HandlerFunc) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
fmt.Println("before") w.Header().Set("X-Wrapped", "true")
f(res, req) f(w, r)
fmt.Println("after")
} }
} }

View File

@ -0,0 +1,10 @@
$ 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

View File

@ -82,10 +82,12 @@ func main() {
for { for {
connCountCurrent := atomic.LoadInt64(&connCount) connCountCurrent := atomic.LoadInt64(&connCount)
if connCountCurrent > 0 { if connCountCurrent > 0 {
fmt.Println("wait at=pending remaining=", connCountCurrent) fmt.Println("wait at=pending remaining=",
connCountCurrent)
time.Sleep(time.Second) time.Sleep(time.Second)
} else { } else {
fmt.Println("wait at=finish remaining=", connCountCurrent) fmt.Println("wait at=finish remaining=",
connCountCurrent)
return return
} }
} }

View File

@ -11,7 +11,8 @@ func handler(res http.ResponseWriter, req *http.Request) {
func main() { func main() {
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil) err := http.ListenAndServeTLS(":5000",
"/tmp/server.crt", "/tmp/server.key", nil)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -3,7 +3,8 @@ $ rm -f server.*
$ openssl genrsa -des3 -out server.orig.key 2048 $ openssl genrsa -des3 -out server.orig.key 2048
$ openssl rsa -in server.orig.key -out server.key $ openssl rsa -in server.orig.key -out server.key
$ openssl req -new -key server.key -out server.csr $ openssl req -new -key server.key -out server.csr
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt $ openssl x509 -req -days 365 -in server.csr \
-signkey server.key -out server.crt
$ go run https-servers.go $ go run https-servers.go