Multiple domains and subdomains
andreinistor opened this issue · comments
I am sorry if this was answered before, but I can't seems to find a way to use multiple domains ..
I have multiple domains on the same server and I am trying to set a domain to a specific route.
I have tried this, but I get 404 Not found:
router.NotFound((*Context).NotFound)
router.Middleware(web.StaticMiddleware("static", web.StaticOption{Prefix: "/", IndexFile: "index.html"}))
router.Get("domain.com/something/", (*Context).SomethingRoot)
router.Get("/:link", (*Context).Root)
fmt.Println("Server started on port " + strconv.Itoa(port) + "..")
http.ListenAndServe(":"+strconv.Itoa(port), router)
Thank you in advance!
I don't think this is possible using gocraft/web since the host + path handling in http.ServeMux has already occurred. All that gocraft/web receives is the context path.
However, you can create multiple routers and register them with http.Handle and pass a nil handler to ListenAndServe - see example below.
Note that you will not need the :port
part in the hostname mapping if the hostname header that your code sees does not contain a port. If your golang application is sitting behind a reverse proxy this may be the case.
Hope this helps.
package main
import (
"github.com/gocraft/web"
"fmt"
"net/http"
)
type Context struct{}
func (c *Context) DefaultHost(rw web.ResponseWriter, req *web.Request) {
fmt.Fprintf(rw, "DefaultHost host=%q", req.Host)
}
func (c *Context) OtherHost(rw web.ResponseWriter, req *web.Request) {
fmt.Fprintf(rw, "OtherHost host=%q", req.Host)
}
func main() {
otherRouter := web.New(Context{})
otherRouter.Get("/", (*Context).OtherHost)
// my /etc/hosts contains otherhost->127.0.0.1
http.Handle("otherhost:3000/", otherRouter)
router := web.New(Context{})
router.Get("/", (*Context).DefaultHost)
http.Handle("/", router)
http.ListenAndServe(":3000", nil)
}
@mlctrez Thank you for the info! This suites my needs!