canonical host
This commit is contained in:
parent
b71b350973
commit
0f2d408137
5
README
5
README
@ -43,7 +43,6 @@ gobyexample.com signups
|
|||||||
* return status code
|
* return status code
|
||||||
* serve static files
|
* serve static files
|
||||||
* force https
|
* force https
|
||||||
* force canonical host
|
|
||||||
* not found page
|
* not found page
|
||||||
* error page
|
* error page
|
||||||
* string formatting
|
* string formatting
|
||||||
@ -66,6 +65,10 @@ gobyexample.com signups
|
|||||||
* init functions
|
* init functions
|
||||||
* using gofmt
|
* using gofmt
|
||||||
* scrolls style logging
|
* scrolls style logging
|
||||||
|
* range and close http://tour.golang.org/#66
|
||||||
|
* string replacement - sub & gsub
|
||||||
|
* benchmarking
|
||||||
|
* profiling
|
||||||
|
|
||||||
* setting up go env, hello world
|
* setting up go env, hello world
|
||||||
* deploying to heroku
|
* deploying to heroku
|
||||||
|
@ -33,3 +33,6 @@ func main() {
|
|||||||
rectangle := Rectangle {x1: 3, x2: 10, y1: 5, y2: 7}
|
rectangle := Rectangle {x1: 3, x2: 10, y1: 5, y2: 7}
|
||||||
fmt.Println(rectangle.area())
|
fmt.Println(rectangle.area())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// == todo
|
||||||
|
// pointer vs value receivers
|
||||||
|
34
src/xx-http-server-canonical-host.go
Normal file
34
src/xx-http-server-canonical-host.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import ("net/http"; "fmt"; "strings")
|
||||||
|
|
||||||
|
func hello(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.Header().Set("Content-Type", "text/plain")
|
||||||
|
fmt.Fprintln(res, "Hello canonical world")
|
||||||
|
}
|
||||||
|
|
||||||
|
func wrapCanonicalHost(f http.HandlerFunc, canonicalHost string) http.HandlerFunc {
|
||||||
|
return func(res http.ResponseWriter, req *http.Request) {
|
||||||
|
hostPort := strings.Split(req.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)
|
||||||
|
}
|
||||||
|
f(res, req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
handler := wrapCanonicalHost(hello, "localhost")
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
http.ListenAndServe(":5000", nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// == running
|
||||||
|
// $ go run xx-http-server-canonical-host.go
|
||||||
|
//
|
||||||
|
// $ curl -i -L http://127.0.0.1:5000/go
|
||||||
|
// $ curl -i -L http://127.0.0.1:5000/go
|
Loading…
x
Reference in New Issue
Block a user