package main
import (
"fmt"
"log"
"net/http"
)
func main() {
log.Println("Server is starting...")
http.Handle("/secured/handle", http.RedirectHandler("/login", http.StatusTemporaryRedirect))
http.HandleFunc("/secured/hadlefunc", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/login", http.StatusTemporaryRedirect)
})
http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome user! Please login!\n")
})
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
/*
➜ recipe08 curl -v -L http://127.0.0.1:8080/secured/handle
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /secured/handle HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 307 Temporary Redirect
< Location: /login
< Date: Mon, 26 Mar 2018 16:12:53 GMT
< Content-Length: 42
< Content-Type: text/html; charset=utf-8
<
* Ignoring the response-body
* Connection #0 to host 127.0.0.1 left intact
* Issue another request to this URL: 'http://127.0.0.1:8080/login'
* Found bundle for host 127.0.0.1: 0x7f95e9611990 [can pipeline]
* Re-using existing connection! (#0) with host 127.0.0.1
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /login HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Mon, 26 Mar 2018 16:12:53 GMT
< Content-Length: 28
< Content-Type: text/plain; charset=utf-8
<
Welcome user! Please login!
* Connection #0 to host 127.0.0.1 left intact
*/