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
// Use the `os` package to list, set, and get
// environment variables.
// Use the `os` package to list, set, and get environment
// variables.
import "os"
import "strings"
import "fmt"

View File

@ -27,4 +27,4 @@ func main() {
// todo: missing subject, cc, bcc
// todo: attachements
// 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 "fmt"
func hello(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(res, "Hello canonical world")
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 http.HandlerFunc, host string) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
hostPort := strings.Split(req.Host, ":")
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 != canonicalHost {
fmt.Println("redirecting from", host, "to", canonicalHost)
hostPort[0] = canonicalHost
url := "http://" + strings.Join(hostPort, ":") + req.URL.String()
http.Redirect(res, req, url, 301)
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(res, req)
f(w, r)
}
}

View File

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

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 {
connCountCurrent := atomic.LoadInt64(&connCount)
if connCountCurrent > 0 {
fmt.Println("wait at=pending remaining=", connCountCurrent)
fmt.Println("wait at=pending remaining=",
connCountCurrent)
time.Sleep(time.Second)
} else {
fmt.Println("wait at=finish remaining=", connCountCurrent)
fmt.Println("wait at=finish remaining=",
connCountCurrent)
return
}
}

View File

@ -11,7 +11,8 @@ func handler(res http.ResponseWriter, req *http.Request) {
func main() {
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 {
panic(err)
}

View File

@ -3,7 +3,8 @@ $ rm -f server.*
$ openssl genrsa -des3 -out server.orig.key 2048
$ openssl rsa -in server.orig.key -out server.key
$ 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