I'm writing a web server and I've got Not Found Handler function.
All the Handle functions like login, registration, view page are working correctly.
I also have such line: router.NotFoundHandler = http.HandlerFunc(Handlers.NotFoundHandler)
Handlers is my package including next code:
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
//title has to be 404.html
title := config.Page404
title = config.WebAppex + title
fmt.Println("title: " + title)
/*HERE IS QUESTION LINE*/
r = http.NewRequest("GET", config.Page404, nil)
fmt.Println(r.URL.Path)
logger.Info("Sending... ", title)
//load page
p, err := loadPage(title)
if err != nil {
logger.Error("ERROR : ", err.Error())
p, _ := loadPage(config.Page404)
fmt.Fprintf(w, "%s", p.body) //wired way to send HTML page
return //just return because of printing dry p.body
}
//serve it
http.ServeFile(w, r, p.dir)
logger.Info(p.dir, " has been sent")
}
So the problem is: If I try going to localhost:8080/nothing, then I get my nice Page404 But if I go to localhost:8080/nothing/nothing or localhost:8080/nothing/nothing/nothing and etc, I get dry 404.html without any CSS. So I thought that problem is request, so I create new with the way of "/404.html" (it's config.Page404), but nothing changed. I read about gorilla mux Subrouter, bit It may help to exclude only localhost:8080/nothing, but I need all the cases. So is there any ways to exclude all the not existing pages?
UPD: My loadPage function:
func loadPage(title string) (*page, error) {
logger.Info("Trying to load", title)
//writing from title to body
body, err := ioutil.ReadFile(title)
if err != nil {
return nil, err
}
return &page{dir: config.HOMEDIR + title, body: body}, nil
}
type page struct {
dir string
body []byte
}
Thank you!