Serving a single page application with go net/http mux
<p>I'm building an api that also serves my react front end app, but am having an issue serving my index.html </p>

<p>Given that it's not really a go template I'm not using html/template. </p>

<p>I'm not seeing a strait forward way to serve the static html root of my app on all pages that do not start /api in the route.</p>

<p>I'm purposely trying not to use any go frameworks beyond gorilla's mux</p>

<p>My handler.go:</p>

<pre><code>func Index(w http.ResponseWriter, r *http.Request) {
    http.FileServer(http.Dir("./views"))
}
</code></pre>

<p>Routes.go:</p>

<pre><code>type Route struct {
    Name        string
    Method      string
    Pattern     string
    HandlerFunc http.HandlerFunc
}

type Routes []Route

var routes = Routes{
    Route{
        "Index",
        "GET",
        "/",
        Index,
    },
}
</code></pre>

<p>router.go</p>

<pre><code>import (
    "net/http"

    "github.com/gorilla/mux"
)

func NewRouter() *mux.Router {

    router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
        var handler http.Handler

        handler = route.HandlerFunc
        handler = Logger(handler, route.Name)

        router.
            Methods(route.Method).
            Path(route.Pattern).
            Name(route.Name).
            Handler(handler)

    }

    return router
}
</code></pre>

<p>main: </p>

<pre><code>package main

import (
    "./server"
    "log"
    "net/http"
)

func main() {

    router := server.NewRouter()

    log.Fatal(http.ListenAndServe(":8080", router))
}
</code></pre>

<p>Currently a blank page shows up, and thats it.  My index.html is located in <code>/views/index.html</code> in relation to the executable (but I've tried it in relation to the handler as well)</p>

<p><strong>Update</strong></p>

<p>I was able to serve the html file using the method shown in this question: <a href="https://stackoverflow.com/questions/26559557/how-do-you-serve-a-static-html-file-using-a-go-web-server">How do you serve a static html file using a go web server?</a>  However using mux and the more modularized file structure still yields a nice pretty, clean blank page.</p>