Hello
I am working on a project where I need to setup the multiple sub-domains with the routes. For routing I am using gin package. I tried code with two sub-domains, but in my case it would be 100 sub-domains. I tried the following code for this:
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
type Subdomains map[string]http.Handler
func (subdomains Subdomains) ServeHTTP(w http.ResponseWriter, r *http.Request) {
domainParts := strings.Split(r.Host, ".")
if mux := subdomains[domainParts[0]]; mux != nil {
mux.ServeHTTP(w, r)
} else {
http.Error(w, "Not found", 404)
}
}
func main() {
r := gin.Default()
r2 := gin.Default()
hs := make(Subdomains)
hs["admin"] = r
hs["analytics"] = r2
r.GET("/ping", adminHandlerOne)
r2.GET("/ping", adminHandlerOne)
http.ListenAndServe(":9090", hs)
}
func adminHandlerOne(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}
But I think that this is not good. Is anybody know the proper way to do this?
I tried to do this with loop also, but it did't work for me.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.