如何在Go服务器中创建自定义域名别名?

huangapple go评论108阅读模式
英文:

How to make custom domain alias in Go server?

问题

我有一个运行在Go之上的Web应用程序。假设这个Web应用程序在domain.com上运行,每个用户都可以创建自己的页面,使用自定义子域名,比如user.domain.com

我的一些用户想要添加自己的自定义域名,例如userdomain.orgpage.anotherdomain.com。我该如何实现这个功能?

我已经在谷歌上搜索过了,他们说应该添加CNAME别名,但是显示了一个DNS解析错误

英文:

i have web app running on top of Go. Let's say this web app running on domain.com, every user can create their on page with custom sub domain like user.domain.com .

Some of my user want to add their own custom domain, ex: userdomain.org or page.anotherdomain.com. How do i accomplish this?

I'm already search on google, they should add CNAME alias, but it shown a DNS Resolution Error

答案1

得分: 1

让用户将他们的域名添加到您的应用程序中,然后在您的 HTTP 处理程序中检查 req.Host 是否与已知用户的页面匹配。

  1. func handleUserPage(w http.ResponseWriter, r *http.Request) {
  2. user, err := db.FindUserByDomain(r.Host)
  3. if err != nil {
  4. w.WriteStatus(404)
  5. return
  6. }
  7. // 根据 r.URL.Path 显示用户的页面
  8. }

请注意,这是一个示例代码片段,用于演示如何在 HTTP 处理程序中检查用户的域名并显示相应的页面。具体实现可能因应用程序的需求而有所不同。

英文:

Have the user add their domain to your app, then in your http handler check if req.Host matches a known user's page.

  1. func handleUserPage(w http.ResponseWriter, r *http.Request) {
  2. user, err := db.FindUserByDomain(r.Host)
  3. if err != nil {
  4. w.WriteStatus(404)
  5. return
  6. }
  7. //display user's page based on r.URL.Path maybe?
  8. }

huangapple
  • 本文由 发表于 2014年10月15日 00:06:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/26365297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定