mirko sailorm
mirko sailorm

Reputation: 147

Why every page show the index page?

i'm creating a small website from scratch, it's been some times since my last website in go and I remeber using the same code i'm using now and it worked without issues. I don't get why, now , it doesn't work, already tried clearing cookies. Every page return the index template. That's the code:

EDIT: Removed "http.Handle("/", r)" nothing changes

var tpl *template.Template


func MainHandling() {
r := http.NewServeMux()

r.HandleFunc("/", Index)
r.HandleFunc("/login", Login)
r.HandleFunc("/register", Register)


fs := http.FileServer(http.Dir("./1Forum/static/")) // funziona solo con newservemux
r.Handle("/static/", http.StripPrefix("/static", fs))

log.Fatal(http.ListenAndServe(":80", context.ClearHandler(r)))
}

func Index(w http.ResponseWriter, r *http.Request) {
    tpl.ExecuteTemplate(w, "index.html", nil)
}

func Login(w http.ResponseWriter, r *http.Request) {
    tpl.ExecuteTemplate(w, "login.html", nil)
}

func Register(w http.ResponseWriter, r *http.Request) {
    tpl.ExecuteTemplate(w, "register.html", nil)
}

Upvotes: 1

Views: 131

Answers (1)

Amin Rashidbeigi
Amin Rashidbeigi

Reputation: 684

The "/" pattern matches everything, so you need to check that you are at the root:

func Index(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path != "/" {
        return
    }
    tpl.ExecuteTemplate(w, "index.html", nil)
}

Check reference for more information.

Upvotes: 0

Related Questions