Golang gorilla mux未找到处理程序无法正常工作。

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

Golang gorilla mux not found handler doesn't work correct

问题

我正在写一个网络服务器,我有一个Not Found Handler函数。
所有的Handle函数,如登录、注册、查看页面都正常工作。
我还有这样一行代码:router.NotFoundHandler = http.HandlerFunc(Handlers.NotFoundHandler)
Handlers是我的包,包含以下代码:

func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
    //title必须是404.html
    title := config.Page404
    title = config.WebAppex + title

    fmt.Println("title: " + title)

    /*这是问题所在的代码行*/
    r = http.NewRequest("GET", config.Page404, nil)

    fmt.Println(r.URL.Path)

    logger.Info("Sending... ", title)

    //加载页面
    p, err := loadPage(title)
    if err != nil {
        logger.Error("ERROR : ", err.Error())
        p, _ := loadPage(config.Page404)
        fmt.Fprintf(w, "%s", p.body) //以奇怪的方式发送HTML页面
        return                       //只是返回,因为要打印dry p.body
    }

    //提供服务
    http.ServeFile(w, r, p.dir)
    logger.Info(p.dir, "已发送")
}

所以问题是:如果我尝试访问localhost:8080/nothing,我会得到一个漂亮的Page404页面。
但是如果我访问localhost:8080/nothing/nothing或localhost:8080/nothing/nothing/nothing等等,我会得到一个没有任何CSS的干燥的404.html页面。所以我认为问题是请求,所以我用"/404.html"的方式创建了一个新的请求(它是config.Page404),但是没有任何变化。我读过关于gorilla mux子路由的文章,但它只能帮助排除localhost:8080/nothing,但我需要处理所有情况。所以有没有办法排除所有不存在的页面?

更新:我的loadPage函数:

func loadPage(title string) (*page, error) {
    logger.Info("尝试加载", title)
    //从title写入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
}

谢谢!

英文:

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!

答案1

得分: 0

你可能是在使用相对路径的方式来提供404页面的CSS样式表路径,当从嵌套页面(如/nothing/nothing/nothing)提供时,这显然会失败。

要么在每个地方使用<base>标签获取绝对链接,要么将你的请求重定向到所需的页面。

英文:

You're probably serving your 404 page with a relative CSS stylesheet path, which will obviously fail when serving it from a nested page like /nothing/nothing/nothing.

Either use the &lt;base&gt; tag to get absolute links everywhere, or redirect your request to the wanted page.

huangapple
  • 本文由 发表于 2016年1月29日 03:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/35069800.html
匿名

发表评论

匿名网友

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

确定