How to use multiple domains with one port ?
Comment From: ghost
Such as : type AdminController struct // portal.domain.com
type BlogController struct // blog.domain.com
Comment From: thinkerou
@getkokomi please check #347
Comment From: wahello
Maybe, the question meaning is: How to use dynamic/wildcard subdomains?
Comment From: zelenko
Use HostSwitch
to host multiple domains.
import (
"fmt"
"log"
"net/http"
)
type HostSwitch map[string]http.Handler
// Implement the ServerHTTP method
func (hs HostSwitch) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if handler, ok := hs[r.Host]; ok && handler != nil {
handler.ServeHTTP(w, r)
} else {
http.Error(w, "Forbidden", http.StatusForbidden)
}
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Welcome to the first home page!")
})
muxTwo := http.NewServeMux()
muxTwo.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "Welcome to the second home page!")
})
hs := make(HostSwitch)
hs["example-one.local:8080"] = mux
hs["example-two.local:8080"] = muxTwo
log.Fatal(http.ListenAndServe(":8080", hs))
}