Golang:如何处理和提供子域名?

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

Golang: How to handle and serve subdomains?

问题

问题是如何同时为域名和子域名x、y、z(或在此示例中为blog、admin和design)提供服务。当运行以下代码并请求blog.localhost:8080/时,Firefox无法找到服务器www.blog.localhost:8080。

  1. package main
  2. import (
  3. "html/template"
  4. "log"
  5. "net/http"
  6. )
  7. var tpl *template.Template
  8. const (
  9. domain = "localhost"
  10. blogDomain = "blog." + domain
  11. adminDomain = "admin." + domain
  12. designDomain = "design." + domain
  13. )
  14. func init() {
  15. tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  16. }
  17. func main() {
  18. // 默认处理程序
  19. http.HandleFunc("/", index)
  20. // 博客处理程序
  21. http.HandleFunc(blogDomain+"/", blogIndex)
  22. // 管理员处理程序
  23. http.HandleFunc(adminDomain+"/", adminIndex)
  24. // 设计处理程序
  25. http.HandleFunc(designDomain+"/", designIndex)
  26. http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static"))))
  27. http.ListenAndServe(":8080", nil)
  28. }
  29. func index(res http.ResponseWriter, req *http.Request) {
  30. err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
  31. if err != nil {
  32. log.Fatalln("模板未执行:", err)
  33. }
  34. }
  35. // 博客处理程序
  36. func blogIndex(res http.ResponseWriter, req *http.Request) {
  37. err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
  38. if err != nil {
  39. log.Fatalln("模板未执行:", err)
  40. }
  41. }
  42. // 管理员处理程序
  43. func adminIndex(res http.ResponseWriter, req *http.Request) {
  44. err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
  45. if err != nil {
  46. log.Fatalln("模板未执行:", err)
  47. }
  48. }
  49. // 设计处理程序
  50. func designIndex(res http.ResponseWriter, req *http.Request) {
  51. err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
  52. if err != nil {
  53. log.Fatalln("模板未执行:", err)
  54. }
  55. }

是否可以使用标准库来提供子域名?如果可以,应该如何实现?

编辑: 请求localhost:8080/可以正常工作。

编辑2: 我编辑了/etc/hosts文件以包含子域名:

  1. 127.0.0.1 blog.localhost.com
  2. 127.0.0.1 admin.localhost.com
  3. 127.0.0.1 design.localhost.com

对它们进行ping测试可以正常工作,但是Firefox无法访问它们。

英文:

The problem is serving the domain as well as subdomains x,y,z (or in this example blog, admin, and design). When running the following and requesting blog.localhost:8080/ firefox cant find the server www.blog.localhost:8080.

  1. package main
  2. import (
  3. "html/template"
  4. "log"
  5. "net/http"
  6. )
  7. var tpl *template.Template
  8. const (
  9. domain = "localhost"
  10. blogDomain = "blog." + domain
  11. adminDomain = "admin." + domain
  12. designDomain = "design." + domain
  13. )
  14. func init() {
  15. tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
  16. }
  17. func main() {
  18. // Default Handlers
  19. http.HandleFunc("/", index)
  20. // Blog Handlers
  21. http.HandleFunc(blogDomain+"/", blogIndex)
  22. // Admin Handlers
  23. http.HandleFunc(adminDomain+"/", adminIndex)
  24. // Design Handlers
  25. http.HandleFunc(designDomain+"/", designIndex)
  26. http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static"))))
  27. http.ListenAndServe(":8080", nil)
  28. }
  29. func index(res http.ResponseWriter, req *http.Request) {
  30. err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
  31. if err != nil {
  32. log.Fatalln("template didn't execute: ", err)
  33. }
  34. }
  35. // Blog Handlers
  36. func blogIndex(res http.ResponseWriter, req *http.Request) {
  37. err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
  38. if err != nil {
  39. log.Fatalln("template didn't execute: ", err)
  40. }
  41. }
  42. // Admin Handlers
  43. func adminIndex(res http.ResponseWriter, req *http.Request) {
  44. err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
  45. if err != nil {
  46. log.Fatalln("template didn't execute: ", err)
  47. }
  48. }
  49. // Design Handlers
  50. func designIndex(res http.ResponseWriter, req *http.Request) {
  51. err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
  52. if err != nil {
  53. log.Fatalln("template didn't execute: ", err)
  54. }
  55. }

Is it possible to serve subdomains using the standard library? If so how?

EDIT: Requesting localhost:8080/ works fine

EDIT2: I edited /etc/hosts to include the subdomains:

  1. 127.0.0.1 blog.localhost.com
  2. 127.0.0.1 admin.localhost.com
  3. 127.0.0.1 design.localhost.com

Pinging them works correctly but firefox cannot reach them.

答案1

得分: 4

根据你的第二次编辑中的hosts文件,你可以将Firefox指向blog.localhost.com:8080,但你还需要处理该域名模式,即http.HandleFunc(blogDomain+":8080/", blogIndex)

如果这不是你想要的,你可以监听端口80,即http.ListenAndServe(":80", nil),你可能需要以sudo方式运行你的应用程序,以便它有权限使用该端口,然后你的blog.localhost.com应该可以正常工作。

英文:

Given the hosts file in your second edit, you can point firefox to, for example, blog.localhost.com:8080 but then you also have to handle that domain pattern, i.e. http.HandleFunc(blogDomain+":8080/", blogIndex).

If that's not what you want you can instead listen on port 80 i.e. http.ListenAndServe(":80", nil), you might need to run your app in sudo so that it has permission to use that port, then your blog.localhost.com should work.

huangapple
  • 本文由 发表于 2017年3月15日 03:50:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/42795286.html
匿名

发表评论

匿名网友

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

确定