Commit d97157d3 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: better stand-alone FileServer doc example

Motivated by a deletion in the wiki, which had a better
example.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/8288045
parent bcc30aa9
...@@ -51,11 +51,20 @@ func ExampleGet() { ...@@ -51,11 +51,20 @@ func ExampleGet() {
} }
func ExampleFileServer() { func ExampleFileServer() {
// we use StripPrefix so that /tmpfiles/somefile will access /tmp/somefile // Simple static webserver:
log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
}
func ExampleFileServer_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.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
} }
func ExampleStripPrefix() { func ExampleStripPrefix() {
// we use StripPrefix so that /tmpfiles/somefile will access /tmp/somefile // 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.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment