将域名指向使用Gorilla Mux的Go服务器。

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

Point domain to go server with gorilla mux

问题

我有一个小型服务器,我希望该服务器使用gorilla/mux包监听我的自定义域名sftablet.dev。

以下是代码:

package main

import (
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()
	r.Host("sftablet.dev")
	r.HandleFunc("/", HomeHandler)
	r.HandleFunc("/products", ProductsHandler)
	http.ListenAndServe(":8080", r)
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hey, this is homepage")
}

func ProductsHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hey, this is products")
}

我还在hosts文件中添加了以下内容:

127.0.0.1       sftablet.dev

但出于某种原因它不起作用。如果我访问127.0.0.1:8080,它可以正常工作,但是当我访问http://sftablet.dev/时却不行。我也清除了DNS缓存。

英文:

I have a small server and I want that server to listen to my custom domain sftablet.dev using gorilla/mux package.

Here is the code:

package main

import (
	"fmt"
	"net/http"

	"github.com/gorilla/mux"
)

func main() {
	r := mux.NewRouter()
	r.Host("sftablet.dev")
	r.HandleFunc("/", HomeHandler)
	r.HandleFunc("/products", ProductsHandler)
	http.ListenAndServe(":8080", r)
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hey, this is homepage")
}

func ProductsHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "Hey, this is products")
}

I also added this in the hosts file:

127.0.0.1       sftablet.dev

But for some reason it doesn't work. It does work if I go to 127.0.0.1:8080, but not when I access http://sftablet.dev/. Also cleared the DNS cache.

答案1

得分: 3

http://sftablet.dev/ 默认会查询端口80。

您的服务器只监听端口8080。http://sftablet.dev:8080/ 应该可以正常工作。

英文:

http://sftablet.dev/ would by default query the port 80

Your server only listen to port 8080. http://sftablet.dev:8080/ should work.

huangapple
  • 本文由 发表于 2015年9月5日 16:54:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/32411219.html
匿名

发表评论

匿名网友

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

确定