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)
}
}
*
>
< 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: *
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步