package main
import(
"net/http"
"log"
)
func main(){//注册某个函数专门响应某个路由"/",函数签名符合
http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request){
w.Write([]byte("Hello,this is version 1!"))
})
http.HandleFunc("/bye",sayBye)
log.Println("Starting server ... v1")
log.Fatal(http.ListenAndServe(":4000",nil))
}
func sayBye( w http.ResponseWriter,r *http.Request){
w.Write([]byte("Bye bye,this is version 1!"))
}
go run test.go
2018/07/09 14:32:08 Starting server ... v1
浏览器打开访问:http://localhost:4000/
浏览器打开访问:http://localhost:4000/bye
如何使用mux自定义ServeHTTP,实现http server呢?
package main
import(
"net/http"
"log"
)
func main(){
mux := http.NewServeMux()
mux.Handle("/",&myHandler{})
mux.HandleFunc("bye",sayBye)
log.Println("Starting server ... v2")
log.Fatal(http.ListenAndServe(":4000",mux))
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter,r *http.Request){
w.Write([]byte("Hello,this is version 2!"))
}
func sayBye(w http.ResponseWriter,r *http.Request){
w.Write([]byte("Bye bye,this is version 2!"))
}
go run test.go
2018/07/09 15:37:35 Starting server ... v2
浏览器打开访问:http://localhost:4000/
浏览器打开访问:http://localhost:4000/bye
浏览器打开访问:http://localhost:4000/abcdef 默认也匹配到这里
所有未匹配到的路由都会匹配到跟 "/" 路由
package main
import(
"net/http"
"log"
)
func main(){
mux := http.NewServeMux()
mux.Handle("/",&myHandler{}) //所有未匹配到的路由都会匹配到跟路由
mux.HandleFunc("bye",sayBye)
log.Println("Starting server ... v2")
log.Fatal(http.ListenAndServe(":4000",mux))
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter,r *http.Request){
w.Write([]byte("Hello v2,the request url is:"+ r.URL.String()))
}
func sayBye(w http.ResponseWriter,r *http.Request){
w.Write([]byte("Bye bye,this is version 2!"))
}
go run test.go
2018/07/09 15:50:23 Starting server ... v2
浏览器打开访问:http://localhost:4000/nihao 默认也匹配到/这里
如何自定义server信息呢?
import(
"net/http"
"log"
"time"
)
func main(){
server :=&http.Server{
Addr: ":4000",
WriteTimeout:2 * time.Second,
}
mux := http.NewServeMux()
mux.Handle("/",&myHandler{}) //所有未匹配到的路由都会匹配到跟路由
mux.HandleFunc("bye",sayBye)
server.Handler=mux
log.Println("Starting server ... v2")
log.Fatal(server.ListenAndServe)
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter,r *http.Request){
w.Write([]byte("Hello v2,the request url is:"+ r.URL.String()))
}
func sayBye(w http.ResponseWriter,r *http.Request){
w.Write([]byte("Bye bye,this is version 2!"))
}
go run test.go
2018/07/09 15:57:46 Starting server ... v2
2018/07/09 15:57:46 0x120ef70
浏览器打开访问:http://localhost:4000 访问正常
客户端可以主动断开
package main
import(
"net/http"
"log"
"time"
"os"
"os/signal"
)
func main(){
server :=&http.Server{
Addr: ":4000",
WriteTimeout:4 * time.Second,
}
quit := make(chan os.Signal)
signal.Notify(quit,os.Interrupt)
mux := http.NewServeMux()
mux.Handle("/",&myHandler{}) //所有未匹配到的路由都会匹配到跟路由
mux.HandleFunc("bye",sayBye)
server.Handler=mux
go func(){
<-quit
if err:=server.Close(); err != nil {
log.Fatal("Close server:",err)
}
}()
log.Println("Starting server ... v3")
err := server.ListenAndServe()
if err !=nil{
if err == http.ErrServerClosed{
log.Print("Server closed under request")
}else{
log.Fatal("Server closed unexpected")
}
}
log.Println("Server exit")
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter,r *http.Request){
w.Write([]byte("Hello v2,the request url is:"+ r.URL.String()))
}
func sayBye(w http.ResponseWriter,r *http.Request){
time.Sleep(3*time.Second)
w.Write([]byte("Bye bye,this is version 2!"))
}
2018/07/09 20:53:41 Starting server ... v3
此时暂停程序,ctrl+c
2018/07/09 20:54:01 Server closed under request
2018/07/09 20:54:01 Server exit
如何实现不同的路由转发呢?
package main
import (
"log"
"net/http"
"time"
)
//创建一个map,进行路由的转发
var mux map[string]func(http.ResponseWriter, *http.Request)
func main() {
server := http.Server{
Addr: ":8090",
Handler: &myHandler{},
ReadTimeout: 5 * time.Second,
}
//通过路由注册,/hello 和 /bye 是不同的录用,调用不同的func
mux = make(map[string]func(http.ResponseWriter, *http.Request)) //分配点内存空间
mux["/hello"] = sayHello
mux["/bye"] = sayBye
err := server.ListenAndServe()
if err != nil {
log.Fatal(err)
}
}
type myHandler struct{}
func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if h, ok := mux[r.URL.String()]; ok { //map的妙用,判断有没有注册路由,如果有,找到,ok=true
h(w, r)
return
}
}
func sayHello(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello,this is version 3!"))
}
func sayBye(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Bye,this is version 4!"))
}
go run test.go
浏览器访问:http://localhost:8090/hello
返回:Hello,this is version 3!
浏览器访问:http://localhost:8090/bye
返回:Bye,this is version 4!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。