http.FileServer and HTTP StripPrefix

Posted by GremlinP1R on Thu, 03 Mar 2022 21:16:39 +0100

http.FileServer

func FileServer(root FileSystem) Handler The FileServer returns an HTTP processor that uses the file system interface root to provide file access services. To implement using the file system interface of the operating system, use HTTP Dir:
http.Handle("/", http.FileServer(http.Dir("/tmp")))

Use cases of official documents

  • Without http Stripprefix
// Simple static webserver:
log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
  • With HTTP Stripprefix
// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))

http.StripPrefix

The above use case uses http Stripprefix, so why do you need to use it, and what convenience can it bring?
The following is a description of the code in the case:

// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))

FileServer() is told that the root of the static file is "/ tmp". We want the URL to start with "/ tmpfiles /". Therefore, if someone requests to access the "/ tempfiles/example.txt" resource, we hope that the server can process the "/ tempfiles/example.txt" request sent by the client, and then return the file "/ tmp/example.txt" to the client. Then http The function of stripprefix is reflected, http Stripprefix forwards the processing of the request to the object specified as its parameter, but before that, it will modify the request URL by stripping the specified prefix. For example, in the above example, it will strip "/ tmpfiles /".

Service static file

HTTP is introduced in section 2.4.2 of Go Web programming Stripprefix and HTTP Use of fileserver.

package main

import "net/http"

func main() {
	mux := http.NewServeMux()
	files := http.FileServer(http.Dir("web06/public"))
	mux.Handle("/static/", http.StripPrefix("/static/", files))
	server := &http.Server{
		Addr: ":8080",
		Handler: mux,
	}
	server.ListenAndServe()
}

The program uses the FileServer function to create a processor that can serve the static files in the specified directory, and passes the processor to the Handle function of the multiplexer. In addition, the StripPrefix function removes the specified prefix in the request URL:

files := http.FileServer(http.Dir("web06/public"))
mux.Handle("/static/", http.StripPrefix("/static/", files))

When the server receives a URL request starting with / static /, the above two lines of code will remove the / static / string in the URL, and then find the requested file in the web06/public directory. For example, when the server receives a file http://localhost/static/css/bootstrap.min.css It will look for the following files in the web06/public Directory:
/css/bootstrap.min.css
When the server successfully finds the file, it will return it to the client.

Topics: Go Web Development http