Created
July 7, 2018 20:00
-
-
Save gufranmirza/b69120c0f8bece2fb7f67a45323868a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func newRouter() *mux.Router { | |
r := mux.NewRouter() | |
r.HandleFunc("/hello", handler).Methods("GET") | |
// Declare the static file directory and point it to the | |
// directory we just made | |
staticFileDirectory := http.Dir("./assets/") | |
// Declare the handler, that routes requests to their respective filename. | |
// The fileserver is wrapped in the `stripPrefix` method, because we want to | |
// remove the "/assets/" prefix when looking for files. | |
// For example, if we type "/assets/index.html" in our browser, the file server | |
// will look for only "index.html" inside the directory declared above. | |
// If we did not strip the prefix, the file server would look for | |
// "./assets/assets/index.html", and yield an error | |
staticFileHandler := http.StripPrefix("/assets/", http.FileServer(staticFileDirectory)) | |
// The "PathPrefix" method acts as a matcher, and matches all routes starting | |
// with "/assets/", instead of the absolute route itself | |
r.PathPrefix("/assets/").Handler(staticFileHandler).Methods("GET") | |
return r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment