从根目录提供主页和静态内容

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

Serve homepage and static content from root

问题

在Golang中,我如何在根目录下提供静态内容,同时还有一个根目录处理程序用于提供主页。

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

package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/", HomeHandler) // 主页
	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和其他始终需要从根目录提供的文件来说,这并没有太大帮助。

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

我有一部分人认为我过于复杂化了情况,这让我觉得我可能遗漏了什么?任何指导都将不胜感激。

英文:

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")
}

If I do

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

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.

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.

答案1

得分: 40

一个替代方案(不使用ServeMux)是显式地为根目录中的每个文件提供服务。其背后的想法是保持基于根目录的文件数量非常小。
确实需要从根目录中提供sitemap.xmlfavicon.icorobots.txt

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) // 主页

	// 必需的基于根目录的资源
	serveSingle("/sitemap.xml", "./sitemap.xml")
	serveSingle("/favicon.ico", "./favicon.ico")
	serveSingle("/robots.txt", "./robots.txt")

	// 正常资源
	http.Handle("/static", http.FileServer(http.Dir("./static/")))

	http.ListenAndServe(":8080", nil)
}

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

英文:

An alternative (not using ServeMux) solution is to serve explicitly each file located in the root directory. The idea behind is to keep the number of root-based files very small.
sitemap.xml, favicon.ico, robots.txt are indeed mandated to be served out of the 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)
}

Please move all other resources (CSS, JS, etc.) to a proper subdirectory, e.g. /static/ .

答案2

得分: 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) // 主页
    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")
    }   
}
英文:

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")
    }   
} 

答案3

得分: 20

使用Gorilla mux包

r := mux.NewRouter()

//在这里放置你的常规处理程序

//然后是根处理程序
r.HandleFunc("/", homePageHandler)
//如果到目前为止找不到路径,例如"/image/tiny.png"
//它将在文件系统中查找"./public/image/tiny.png"
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./public/")))

http.Handle("/", r)
http.ListenAndServe(":8080", nil)
英文:

Using Gorilla mux package :

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)

huangapple
  • 本文由 发表于 2012年12月30日 05:24:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/14086063.html
匿名

发表评论

匿名网友

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

确定