|
@@ -1,11 +1,10 @@
|
|
|
# gorilla/mux
|
|
|
|
|
|
[](https://godoc.org/github.com/gorilla/mux)
|
|
|
-[](https://travis-ci.org/gorilla/mux)
|
|
|
[](https://circleci.com/gh/gorilla/mux)
|
|
|
[](https://sourcegraph.com/github.com/gorilla/mux?badge)
|
|
|
|
|
|
-
|
|
|
+
|
|
|
|
|
|
https://www.gorillatoolkit.org/pkg/mux
|
|
|
|
|
@@ -26,6 +25,7 @@ The name mux stands for "HTTP request multiplexer". Like the standard `http.Serv
|
|
|
* [Examples](#examples)
|
|
|
* [Matching Routes](#matching-routes)
|
|
|
* [Static Files](#static-files)
|
|
|
+* [Serving Single Page Applications](#serving-single-page-applications) (e.g. React, Vue, Ember.js, etc.)
|
|
|
* [Registered URLs](#registered-urls)
|
|
|
* [Walking Routes](#walking-routes)
|
|
|
* [Graceful Shutdown](#graceful-shutdown)
|
|
@@ -212,6 +212,93 @@ func main() {
|
|
|
}
|
|
|
```
|
|
|
|
|
|
+### Serving Single Page Applications
|
|
|
+
|
|
|
+Most of the time it makes sense to serve your SPA on a separate web server from your API,
|
|
|
+but sometimes it's desirable to serve them both from one place. It's possible to write a simple
|
|
|
+handler for serving your SPA (for use with React Router's [BrowserRouter](https://reacttraining.com/react-router/web/api/BrowserRouter) for example), and leverage
|
|
|
+mux's powerful routing for your API endpoints.
|
|
|
+
|
|
|
+```go
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "log"
|
|
|
+ "net/http"
|
|
|
+ "os"
|
|
|
+ "path/filepath"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "github.com/gorilla/mux"
|
|
|
+)
|
|
|
+
|
|
|
+// spaHandler implements the http.Handler interface, so we can use it
|
|
|
+// to respond to HTTP requests. The path to the static directory and
|
|
|
+// path to the index file within that static directory are used to
|
|
|
+// serve the SPA in the given static directory.
|
|
|
+type spaHandler struct {
|
|
|
+ staticPath string
|
|
|
+ indexPath string
|
|
|
+}
|
|
|
+
|
|
|
+// ServeHTTP inspects the URL path to locate a file within the static dir
|
|
|
+// on the SPA handler. If a file is found, it will be served. If not, the
|
|
|
+// file located at the index path on the SPA handler will be served. This
|
|
|
+// is suitable behavior for serving an SPA (single page application).
|
|
|
+func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
+ // get the absolute path to prevent directory traversal
|
|
|
+ path, err := filepath.Abs(r.URL.Path)
|
|
|
+ if err != nil {
|
|
|
+ // if we failed to get the absolute path respond with a 400 bad request
|
|
|
+ // and stop
|
|
|
+ http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // prepend the path with the path to the static directory
|
|
|
+ path = filepath.Join(h.staticPath, path)
|
|
|
+
|
|
|
+ // check whether a file exists at the given path
|
|
|
+ _, err = os.Stat(path)
|
|
|
+ if os.IsNotExist(err) {
|
|
|
+ // file does not exist, serve index.html
|
|
|
+ http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
|
|
|
+ return
|
|
|
+ } else if err != nil {
|
|
|
+ // if we got an error (that wasn't that the file doesn't exist) stating the
|
|
|
+ // file, return a 500 internal server error and stop
|
|
|
+ http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // otherwise, use http.FileServer to serve the static dir
|
|
|
+ http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ router := mux.NewRouter()
|
|
|
+
|
|
|
+ router.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
|
|
|
+ // an example API handler
|
|
|
+ json.NewEncoder(w).Encode(map[string]bool{"ok": true})
|
|
|
+ })
|
|
|
+
|
|
|
+ spa := spaHandler{staticPath: "build", indexPath: "index.html"}
|
|
|
+ router.PathPrefix("/").Handler(spa)
|
|
|
+
|
|
|
+ srv := &http.Server{
|
|
|
+ Handler: router,
|
|
|
+ Addr: "127.0.0.1:8000",
|
|
|
+ // Good practice: enforce timeouts for servers you create!
|
|
|
+ WriteTimeout: 15 * time.Second,
|
|
|
+ ReadTimeout: 15 * time.Second,
|
|
|
+ }
|
|
|
+
|
|
|
+ log.Fatal(srv.ListenAndServe())
|
|
|
+}
|
|
|
+```
|
|
|
+
|
|
|
### Registered URLs
|
|
|
|
|
|
Now let's see how to build registered URLs.
|