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

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

Golang: How to handle and serve subdomains?

问题

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

package main

import (
	"html/template"
	"log"
	"net/http"
)

var tpl *template.Template

const (
	domain      = "localhost"
	blogDomain  = "blog." + domain
	adminDomain = "admin." + domain
	designDomain = "design." + domain
)

func init() {
	tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}

func main() {
	// 默认处理程序
	http.HandleFunc("/", index)

	// 博客处理程序
	http.HandleFunc(blogDomain+"/", blogIndex)

	// 管理员处理程序
	http.HandleFunc(adminDomain+"/", adminIndex)

	// 设计处理程序
	http.HandleFunc(designDomain+"/", designIndex)

	http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static"))))
	http.ListenAndServe(":8080", nil)
}

func index(res http.ResponseWriter, req *http.Request) {
	err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
	if err != nil {
		log.Fatalln("模板未执行:", err)
	}
}

// 博客处理程序
func blogIndex(res http.ResponseWriter, req *http.Request) {
	err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
	if err != nil {
		log.Fatalln("模板未执行:", err)
	}
}

// 管理员处理程序
func adminIndex(res http.ResponseWriter, req *http.Request) {
	err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
	if err != nil {
		log.Fatalln("模板未执行:", err)
	}
}

// 设计处理程序
func designIndex(res http.ResponseWriter, req *http.Request) {
	err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
	if err != nil {
		log.Fatalln("模板未执行:", err)
	}
}

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

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

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

127.0.0.1 blog.localhost.com
127.0.0.1 admin.localhost.com
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.

package main
import (
"html/template"
"log"
"net/http"
)
var tpl *template.Template
const (
domain = "localhost"
blogDomain = "blog." + domain
adminDomain = "admin." + domain
designDomain = "design." + domain
)
func init() {
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}
func main() {
// Default Handlers
http.HandleFunc("/", index)
// Blog Handlers
http.HandleFunc(blogDomain+"/", blogIndex)
// Admin Handlers
http.HandleFunc(adminDomain+"/", adminIndex)
// Design Handlers
http.HandleFunc(designDomain+"/", designIndex)
http.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir("static"))))
http.ListenAndServe(":8080", nil) 
}
func index(res http.ResponseWriter, req *http.Request) {
err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
if err != nil {
log.Fatalln("template didn't execute: ", err)
}
}
// Blog Handlers
func blogIndex(res http.ResponseWriter, req *http.Request) {
err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
if err != nil {
log.Fatalln("template didn't execute: ", err)
}
}
// Admin Handlers
func adminIndex(res http.ResponseWriter, req *http.Request) {
err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
if err != nil {
log.Fatalln("template didn't execute: ", err)
}
}
// Design Handlers
func designIndex(res http.ResponseWriter, req *http.Request) {
err := tpl.ExecuteTemplate(res, "index.gohtml", nil)
if err != nil {
log.Fatalln("template didn't execute: ", err)
}
}

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:

127.0.0.1 blog.localhost.com
127.0.0.1 admin.localhost.com
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:

确定