从root用户提供主页和静态内容

Nuc*_*eon 40 webserver go

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

使用以下简单Web服务器作为示例:

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")
}
Run Code Online (Sandbox Code Playgroud)

如果我做

http.Handle("/", http.FileServer(http.Dir("./")))
Run Code Online (Sandbox Code Playgroud)

我收到一个恐慌,说我有两个"/"注册.我在互联网上找到的每个Golang示例都建议从不同的目录中提供静态内容,但这对于sitemap.xml,favicon.ico,robots.txt和其他文件来说无济于事.强制要求永远服务于根.

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

我的一部分说我大概过度复杂的情况让我觉得我错过了什么?任何指导将不胜感激.

Del*_*ace 39

另一种(不使用ServeMux)解决方案是明确地提供位于根目录中的每个文件.背后的想法是保持基于根的文件的数量非常小. sitemap.xml,favicon.ico,robots.txt确实要求在root用户之外提供服务:

package main

import (
    "fmt"
    "net/http"
)

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

func serveSingle(pattern string, filename string) {
    http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
        http.ServeFile(w, r, filename)
    })
}

func main() {
    http.HandleFunc("/", HomeHandler) // homepage

    // Mandatory root-based resources
    serveSingle("/sitemap.xml", "./sitemap.xml")
    serveSingle("/favicon.ico", "./favicon.ico")
    serveSingle("/robots.txt", "./robots.txt")

    // Normal resources
    http.Handle("/static", http.FileServer(http.Dir("./static/")))

    http.ListenAndServe(":8080", nil)
}
Run Code Online (Sandbox Code Playgroud)

请将所有其他资源(CSS,JS等)移动到适当的子目录,例如/static/.

  • 为了提供文件夹中的静态资源,我必须使用`http.Handle("/ css /",http.StripPrefix("/ css /",http.FileServer(http.Dir("./ css /")))) ` (17认同)

mas*_*ase 24

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

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")
    }   
} 
Run Code Online (Sandbox Code Playgroud)


fat*_*nse 20

使用Gorilla mux包:

r := mux.NewRouter()

//put your regular handlers here

//then comes root handler
r.HandleFunc("/", homePageHandler)
//if a path not found until now, e.g. "/image/tiny.png" 
//this will look at "./public/image/tiny.png" at filesystem
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/")))

http.Handle("/", r)
http.ListenAndServe(":8080", nil)
Run Code Online (Sandbox Code Playgroud)