0

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.

zuzuleinen
  • 2,418
  • 4
  • 36
  • 45

2 Answers2

3

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

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

VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
0

Instead of using r.Host("sftablet.dev"), move the domain host into the http.ListenAndServe method:

func main() {
    r := mux.NewRouter()
    // r.Host("sftablet.dev")
    r.HandleFunc("/", HomeHandler)
    r.HandleFunc("/products", ProductsHandler)
    http.ListenAndServe("sftablet.dev:8080", r)
}
Xavier Egea
  • 4,592
  • 3
  • 24
  • 39