Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve homepage and static content from root

Tags:

webserver

go

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.

like image 509
Nucleon Avatar asked Dec 29 '12 21:12

Nucleon


People also ask

How do you serve static content?

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express. The root argument specifies the root directory from which to serve static assets. For more information on the options argument, see express.static.

How do I serve a static file in node?

In your node application, you can use node-static module to serve static resources. The node-static module is an HTTP static-file server module with built-in caching. First of all, install node-static module using NPM as below. After installing node-static module, you can create static file server in Node.

What does serving static files mean?

Definition. Static content is any content that can be delivered to an end user without having to be generated, modified, or processed. The server delivers the same file to each user, making static content one of the simplest and most efficient content types to transmit over the Internet.


2 Answers

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/ .

like image 195
Deleplace Avatar answered Sep 20 '22 08:09

Deleplace


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")     }    }  
like image 30
masebase Avatar answered Sep 21 '22 08:09

masebase