all less than 60 characters
This commit is contained in:
parent
0829ebc996
commit
13f1925439
@ -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
|
||||
|
@ -19,7 +19,7 @@ func main() {
|
||||
fmt.Println(rand.Float64())
|
||||
|
||||
// To make the psuedo-random generator deterministic,
|
||||
// give it a well-known seed.
|
||||
// give it a well-known seed.
|
||||
s1 := rand.NewSource(42)
|
||||
r1 := rand.New(s1)
|
||||
|
||||
|
@ -1,27 +1,27 @@
|
||||
// ## SHA1 Hashes
|
||||
// ## SHA1 Hashes
|
||||
|
||||
package main
|
||||
|
||||
// Package `crypto/sha1` computes SHA1 hashes.
|
||||
// Package `crypto/sha1` computes SHA1 hashes.
|
||||
import "crypto/sha1"
|
||||
import "encoding/hex"
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// The pattern is `sha1.New()`, `sha1.Write(bytes)`,
|
||||
// The pattern is `sha1.New()`, `sha1.Write(bytes)`,
|
||||
// then `sha1.Sum([]byte{})
|
||||
h := sha1.New()
|
||||
|
||||
// `Write` expects bytes. If you have a string `s`
|
||||
// use `[]byte(s)` to coerce it.
|
||||
// `Write` expects bytes. If you have a string `s`
|
||||
// use `[]byte(s)` to coerce it.
|
||||
h.Write([]byte("sha1 this string"))
|
||||
|
||||
// Get the result. The argument to `Sum` can be used
|
||||
// to append to an existing buffer: usually uneeded.
|
||||
// to append to an existing buffer: usually uneeded.
|
||||
bs := h.Sum(nil)
|
||||
|
||||
// SHA1 values are often printed in hex, for example
|
||||
// with git.
|
||||
// SHA1 values are often printed in hex, for example
|
||||
// with git.
|
||||
fmt.Println(hex.EncodeToString(bs))
|
||||
}
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
10
src/083-middleware/middleware.sh
Normal file
10
src/083-middleware/middleware.sh
Normal 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
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user