处理动态子域
Handling dynamic sub-domains
假设我的网站是 http://soccer.com
,我想支持无限数量的子域,例如:
http://cronaldo.soccer.com
http://messi.soccer.com
http://neymar.soccer.com
http://muller.soccer.com
- ...
我也想保留几个子域名,例如:
http://admin.soccer.com
http://help.soccer.com
- ...
虽然玩家的子域将由相同的逻辑处理,但保留的子域不会。所以我需要 2 条路由或 2 台路由器?
这是我的:
package main
import (
"fmt"
"net/http"
"log"
"html/template"
"strings"
)
type Mux struct {
http.Handler
}
func (mux Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
domainParts := strings.Split(r.Host, ".")
fmt.Println("Here: " + domainParts[0])
if domainParts[0] == "admin" {
// assign route?
mux.ServeHTTP(w, r)
} else if domainParts[0] == "help" {
// assign route?
mux.ServeHTTP(w, r)
} else if isSubDomainValid(domainParts[0]) {
// assign route for player page?
mux.ServeHTTP(w, r)
} else {
// Handle 404
http.Error(w, "Not found", 404)
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", HomeHandler)
http.ListenAndServe(":8080", mux)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("index.html")
if err != nil {
log.Print("template parsing error: ", err)
}
err = t.Execute(w, nil)
if err != nil {
log.Print("template executing error: ", err)
}
}
但我认为我这样做不对。
fmt.Println("Here: " + domainParts[0])
永远不会出现,让我相信 ServeHTTP()
永远不会被调用。
我只是 Go 的初学者,所以我可能会遗漏一些概念
谢谢
你走在正确的轨道上。为每种域创建一个 http.ServeMux 并从 Mux.ServeHTTP 中的 if / else 语句调用这些多路复用器。此外,使用 Mux 作为根处理程序。
type Mux struct {
help, admin, sub, main *http.ServeMux
}
func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if isMainDomain(r.Host) {
mux.main.ServeHTTP(w, r)
return
}
domainParts := strings.Split(r.Host, ".")
if domainParts[0] == "admin" {
mux.admin.ServeHTTP(w, r)
} else if domainParts[0] == "help" {
mux.help.ServeHTTP(w, r)
} else if isSubDomainValid(domainParts[0]) {
mux.sub.ServeHTTP(w, r)
} else {
http.Error(w, "Not found", 404)
}
}
func main() {
mux := &Mux{
help: http.NewServeMux(),
admin: http.NewServeMux(),
sub: http.NewServeMux(),
main: http.NewServeMux(),
}
mux.help.HandleFunc("/", helpHomeHandler)
mux.admin.HandleFunc("/", adminHomeHandler)
mux.sub.HandleFunc("/", defaultSubdomainHomeHandler)
mux.main.HandleFunc("/", mainHomeHandler)
http.ListenAndServe(":8080", mux) // <-- use Mux value as root handler
}
假设我的网站是 http://soccer.com
,我想支持无限数量的子域,例如:
http://cronaldo.soccer.com
http://messi.soccer.com
http://neymar.soccer.com
http://muller.soccer.com
- ...
我也想保留几个子域名,例如:
http://admin.soccer.com
http://help.soccer.com
- ...
虽然玩家的子域将由相同的逻辑处理,但保留的子域不会。所以我需要 2 条路由或 2 台路由器?
这是我的:
package main
import (
"fmt"
"net/http"
"log"
"html/template"
"strings"
)
type Mux struct {
http.Handler
}
func (mux Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
domainParts := strings.Split(r.Host, ".")
fmt.Println("Here: " + domainParts[0])
if domainParts[0] == "admin" {
// assign route?
mux.ServeHTTP(w, r)
} else if domainParts[0] == "help" {
// assign route?
mux.ServeHTTP(w, r)
} else if isSubDomainValid(domainParts[0]) {
// assign route for player page?
mux.ServeHTTP(w, r)
} else {
// Handle 404
http.Error(w, "Not found", 404)
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", HomeHandler)
http.ListenAndServe(":8080", mux)
}
func HomeHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("index.html")
if err != nil {
log.Print("template parsing error: ", err)
}
err = t.Execute(w, nil)
if err != nil {
log.Print("template executing error: ", err)
}
}
但我认为我这样做不对。
fmt.Println("Here: " + domainParts[0])
永远不会出现,让我相信 ServeHTTP()
永远不会被调用。
我只是 Go 的初学者,所以我可能会遗漏一些概念
谢谢
你走在正确的轨道上。为每种域创建一个 http.ServeMux 并从 Mux.ServeHTTP 中的 if / else 语句调用这些多路复用器。此外,使用 Mux 作为根处理程序。
type Mux struct {
help, admin, sub, main *http.ServeMux
}
func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if isMainDomain(r.Host) {
mux.main.ServeHTTP(w, r)
return
}
domainParts := strings.Split(r.Host, ".")
if domainParts[0] == "admin" {
mux.admin.ServeHTTP(w, r)
} else if domainParts[0] == "help" {
mux.help.ServeHTTP(w, r)
} else if isSubDomainValid(domainParts[0]) {
mux.sub.ServeHTTP(w, r)
} else {
http.Error(w, "Not found", 404)
}
}
func main() {
mux := &Mux{
help: http.NewServeMux(),
admin: http.NewServeMux(),
sub: http.NewServeMux(),
main: http.NewServeMux(),
}
mux.help.HandleFunc("/", helpHomeHandler)
mux.admin.HandleFunc("/", adminHomeHandler)
mux.sub.HandleFunc("/", defaultSubdomainHomeHandler)
mux.main.HandleFunc("/", mainHomeHandler)
http.ListenAndServe(":8080", mux) // <-- use Mux value as root handler
}