从 root 提供主页和静态内容 [英] Serve homepage and static content from root

查看:36
本文介绍了从 root 提供主页和静态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Golang 中,如何在根目录之外提供静态内容,同时仍然具有用于提供主页的根目录处理程序.

In Golang, how do I serve static content out of the root directory while still having a root directory handler for serving the homepage.

以下面的简单网络服务器为例:

Use the following simple web server as an example:

package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", HomeHandler) // homepage
    http.ListenAndServe(":8080", nil)
}

func HomeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "HomeHandler")
}

如果我这样做

http.Handle("/", http.FileServer(http.Dir("./")))

我收到一条恐慌消息,说我有两个/"注册.我在互联网上找到的每个 Golang 示例都建议从不同的目录中提供静态内容,但这对于诸如 sitemap.xml、favicon.ico、robots.txt 和其他实践或必须始终在根目录外提供服务.

I receive a panic saying that I have two registrations for "/". Every Golang example I've found on the internet suggests serving their static content out of different directories, but that doesn't help much for things like sitemap.xml, favicon.ico, robots.txt and other files which are by-practice or mandated to always be served out of the root.

我寻求的行为是在大多数 Web 服务器(例如 Apache、Nginx 或 IIS)中发现的行为,它首先遍历您的规则,如果没有找到规则,则查找实际文件,如果没有找到文件发现它是404s.我的猜测是,不是编写 http.HandlerFunc,我需要编写一个 http.Handler 来检查我是否引用了带有扩展名的文件,如果是,则检查文件存在并提供文件,否则404s或提供主页是请求是/".不幸的是,我什至不确定如何开始这样的任务.

The behavior I seek is the behavior which is found in most web servers such as Apache, Nginx, or IIS, where it first traverses your rules, and if no rule is found it looks for an actual file, and if no file is found it 404s. My guess is that instead of writing a http.HandlerFunc, I need to write a http.Handler which checks if I am referencing a file with an extension, and if so checks for file existence and serves the file, otherwise it 404s or serves the homepage is the request was for "/". Unfortunately I'm not certain how to even begin such a task.

我的一部分人说我把情况过于复杂了,这让我觉得我错过了什么?任何指导将不胜感激.

Part of me says I'm massively over-complicating the situation which makes me think that I am missing something? Any guidance would be appreciated.

推荐答案

我认为可能对您有帮助的一件事是您可以创建自己的 ServeMux.我添加到您的示例中,以便 chttp 是您可以提供静态文件的 ServeMux.HomeHandler 然后检查它是否应该提供文件.我只是检查一个."但你可以做很多事情.只是一个想法,可能不是您要找的.

One thing I thought of that might help you is that you can create your own ServeMux. I added to your example so that chttp is a ServeMux that you can have serve static files. The HomeHandler then checks to see if it should serve a file or not. I just check for a "." but you could do a lot of things. Just an idea, might not be what you are looking for.

package main

import (
    "fmt"
    "net/http"
    "strings"
)   

var chttp = http.NewServeMux()

func main() {

    chttp.Handle("/", http.FileServer(http.Dir("./")))

    http.HandleFunc("/", HomeHandler) // homepage
    http.ListenAndServe(":8080", nil)
}   

func HomeHandler(w http.ResponseWriter, r *http.Request) {

    if (strings.Contains(r.URL.Path, ".")) {
        chttp.ServeHTTP(w, r)
    } else {
        fmt.Fprintf(w, "HomeHandler")
    }   
} 

这篇关于从 root 提供主页和静态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆