title: How to Serve Dynamically Created URL Paths with Go?
body: <p>I am using react-router and <a href="https://developer.mozilla.org/en-US/docs/Web/API/History_API" rel="nofollow noreferrer">browserHistory</a>'s pushState in a reactjs project. This project lets a user create a note which creates a new path. To serve this type of site I need to serve the same HTML file to every path besides the static content. So my nodejs code looks like this.</p>
  
  <pre><code>// Serve the static content
  app.use('/static/css/', express.static(path.join(__dirname, '../../react-ui/build/static/css')));
  app.use('/static/js/', express.static(path.join(__dirname, '../../react-ui/build/static/js')));
  app.use('/static/media/', express.static(path.join(__dirname, '../../react-ui/build/static/media')));
  app.use('/static/img/', express.static(path.join(__dirname, '../../react-ui/build/static/img')));
  app.use('/img/', express.static(path.join(__dirname, '../../react-ui/build/img')));
  
  // Serve the same HTML file to everything else
  app.use('*', express.static(path.join(__dirname, '../../react-ui/build'))); 
  </code></pre>
  
  <p>I don't see any wildcard support for the Go FileServer. Currently I have all the static pages served using Go code similar to this.</p>
  
  <pre><code>package main
  
  import (
      "net/http"
  )
  
  func init(){
      fs := http.FileServer(http.Dir("web"))
      http.Handle("/", fs)
      http.Handle("/static-page-1/", http.StripPrefix("/static-page-1/", fs))
      http.Handle("/static-page-2/", http.StripPrefix("/static-page-2/", fs))
      http.Handle("/static-page-3/", http.StripPrefix("/static-page-3/", fs))
  }
  </code></pre>
  
  <p>Is it possible to serve content to dynamically generated URL paths with a Go server?</p>
  
  <p>If the Handle method supported variables then I'd write the code like this</p>
  
  <pre><code>fs := http.FileServer(http.Dir("web"))
  http.Handle("/static/", fs)
  http.Handle("/{unknownUserPath}", http.StripPrefix("/{unknownUserPath}", fs))
  </code></pre>
  
  <p>{unknownUserPath} would be any path that a user types in that is not under /static/ path.</p>
  
  <p>Here's the go project structure</p>
  
  <p><a href="https://i.stack.imgur.com/xN4uC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xN4uC.png" alt="enter image description here"></a></p>
  
  <p>Here's the server based on @putu answer</p>
  
  <pre><code>package main
  
  import (
      "net/http"
      "strings"
  )
  
  func adaptFileServer(fs http.Handler) http.Handler {
      fn := func(w http.ResponseWriter, req *http.Request) {
          staticIndex := strings.Index(req.URL.Path, "/static/");
          imgIndex := strings.Index(req.URL.Path, "/img/");
  
          if staticIndex == -1 &amp;&amp; imgIndex == -1 {
              fsHandler := http.StripPrefix(req.URL.Path, fs)
              fsHandler.ServeHTTP(w, req)
          } else {
              fs.ServeHTTP(w, req)
          }
      }
      return http.HandlerFunc(fn)
  }
  
  func init() {
      fs := http.FileServer(http.Dir("web"))
      http.Handle("/", adaptFileServer(fs))
  }
  </code></pre>