Skip to content

Instantly share code, notes, and snippets.

@flowchartsman
Last active February 17, 2019 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flowchartsman/50d81a034e6a61743215dfd8af814400 to your computer and use it in GitHub Desktop.
Save flowchartsman/50d81a034e6a61743215dfd8af814400 to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"github.com/gorilla/mux"
)
// example of a basic handler
func fooHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("foo route\n"))
}
// example of a handler using vars
func barHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("bar route got " + mux.Vars(r)["barID"] + "\n"))
}
// Dedicated handler to serve index.html to root route
func rootHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./static/index.html")
}
func main() {
r := mux.NewRouter()
// these two are pretty standard
r.HandleFunc("/foo", fooHandler).Methods("GET")
r.HandleFunc("/bar/{barID}", barHandler).Methods("GET")
// stripPrefix just removes the "static" prefix and passes what's left to http.FileServer. However, there is a special case for the
// user requesting "index.htm[l]" where it will redirect to the root. See: https://golang.org/src/net/http/fs.go#L540
// BUT, since http.StripPrefix has removed the static prefix, the redirect points back to root. But, our next route below serves
// index.html statically from root. Since browsers follow permanent redirects automatically, this is transparent from a browser
// perspective, but requires `curl -L` to properly follow with curl.
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
r.HandleFunc("/", rootHandler).Methods("GET")
http.ListenAndServe(":8080", r)
}
$ curl localhost:8080/foo
foo route
$ curl localhost:8080/bar #will fail like expected
404 page not found
$ curl localhost:8080/bar/argument #fine with argument
bar route got argument
$ curl localhost:8080/static/file #doesn't exist
404 page not found
$ echo "static file" > static/file #create it
$ curl localhost:8080/static/file #again
static file
$ echo "hello index"> static/index.html #create the index
$ curl localhost:8080/ #root serves it just fine
hello index
$ curl localhost:8080/static/index.html #request it statically
$ # What? Nothing?
$ curl -v localhost:8080/static/index.html # Use -v (verbose) to see what's up
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /static/index.html HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Location: ./
< Date: Sun, 17 Feb 2019 18:59:15 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
$ # Aha, it's the redirect from fs.go
$ curl -L localhost:8080/static/index.html # Follow 301s like the browser
hello index
$ #Works!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment