goji 是很棒的golang web 框架,下面是自定义不同子域名指向不同的handler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main
import (
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
func main() {
mux1 := web.New()
mux1.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("domain one mux\n"))
})
http.Handle("domainone.com/", mux1)
mux2 := web.New()
mux2.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("domain two mux\n"))
})
http.Handle("domaintwo.com/", mux2)
goji.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("main mux\n"))
})
goji.Serve()
}
测试:
1
2
3
4
5
6
$ curl -H "Host: domainone.com" localhost:8000
domain one mux
$ curl -H "Host: domaintwo.com" localhost:8000
domain two mux
$ curl localhost:8000
main mux
参考 https://godoc.org/net/url#URL
本文网址: https://golangnote.com/topic/103.html 转摘请注明来源