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.
question from:
https://stackoverflow.com/questions/14086063/serve-homepage-and-static-content-from-root 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…