大家好,
我在一个项目上工作,我需要设置多个子域的路线。我尝试了两个子域的代码,但在我的情况下,它将是100个子域。我为此尝试了以下代码:
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",
})
}
字符串
但我觉得这样不好。有人知道正确的方法吗?
3条答案
按热度按时间ws51t4hk1#
你可以使用
*httputil.ReverseProxy
来实现。下面是我如何在没有任何Gin中间件的情况下基于主机名重路由到子域。字符串
new9mtju2#
字符串
pw136qt23#
你有几个选择(写一个路由器,写 Package 器,试图弯曲杜松子酒你的意志),但作为默认的ServeMux支持这一点,你可能真的不需要杜松子酒,特别是,我会去与标准路由器。首先看一下DefaultServeMux的源代码,了解路由器的本质有多简单--路由器只是到处理程序的路径Map。
默认的ServeMux实际上做了你想要的(允许主机和路径匹配),所以我建议先尝试一下。注册您的图案:
字符串
编写一些处理程序(显然你可能会用编码器而不是直接编写json,这只是一个例子):
型
如果你在localhost上测试这个,你可能需要编辑/etc/hosts文件,以确保你使用正确的主机名访问服务器。
把这些放在一起,你可以用这样的东西来测试:
https://play.golang.org/p/ut-GT_s3Gf
注意,如果你愿意,这些子域可以是动态的(提到100多个让我觉得它们可能是动态的),你可以在运行时在处理程序中自定义行为,而不是使用单独的处理程序,这取决于域的行为有多不同。