Trying to serve React SPA that uses react-router - go

Im using Gorilla/Mux for routing and want to serve the React SPA regardless of the URL path.
func main() {
fmt.Println("server running...")
hub := newHub()
go hub.run()
router := mux.NewRouter()
router.HandleFunc("/api/create", Api)
router.HandleFunc("/api/getpoll", Api)
router.HandleFunc("/api/update", Api)
router.HandleFunc("/sockets/{id}", func(w http.ResponseWriter, r
*http.Request) {
Socketme(hub, w, r)
})
// router.HandleFunc("/{rest:.*}", emberHandler)
router.PathPrefix("/").HandlerFunc(serveFile)
log.Fatal(http.ListenAndServe(":5000", router))
}
func serveFile(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir("./public/build")).ServeHTTP(w, r)
}
Dont want Go to give 404s the Spa should be handling these routes.

The Router exports a NotFoundHandler field which you can set to your custom handler.
router := mux.NewRouter()
router.NotFoundHandler = MyCustom404Handler
So you could do something like:
router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./public/build/index.html")
}))
so that it always serves your index page when it would normally return a 404

So I couldnt find any workable solutions to this. So I ended up taking a different approach using mux.Subrouter found here Static file server in Golang using gorilla/mux

Related

How to implement case insensitive URL matching using gorilla mux

I need to implement case insensitive URL matching in gorilla mux as it is done here for built in mux
I tried to achieve the same using middle-ware like this
router := mux.NewRouter()
router.Use(srv.GetCaseMiddleware())
//GetCaseMiddleware middleware to make match URL case insensitive
func (srv *Server) GetCaseMiddleware() (w mux.MiddlewareFunc) {
var middleware mux.MiddlewareFunc = func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.ToLower(r.URL.Path)
next.ServeHTTP(w, r)
})
}
return middleware
}
but still it throws 404 if URL case is changed,is there any way to implement it using gorilla-mux
Unfortunately, as of this writing, middleware functions are invoked after URL matching in gorilla/mux.
Mux supports the addition of middlewares to a Router, which are executed in the order they are added if a match is found, including its subrouters.
I would suggest going with the example in the link you provided.
e.g.
func CaselessMatcher(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = strings.ToLower(r.URL.Path)
next.ServeHTTP(w, r)
})
}
Then, just wrap your multiplexer.
r := mux.NewRouter()
//...
handler := CaselessMatcher(r)
It's actually not bad IMO.

Mux middleware without handler

I'm building an application using the Micro Service Architecture.
On the gateway, I do want to route requests to the correct endpoints.
But, the endpoint are now known at runtime and needs to be configured in the DB.
Below if the code to get the router.
func getRouter() *mux.Router {
r := mux.NewRouter()
r.Use(dynamicRouteMiddleware)
return r
}
The middleware itself is this:
func dynamicRouteMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Error")
})
}
However, the "Error" is never printed.
It's only printed when I put a handler for '/'
How can I create middleware without handlers?
It's called "middleware" because it's supposed to put your Handler in the "middle". It receives the input before your Handler, and receives the output of your Handler.
Inherently, to have your middleware work, you are required to have at least one Handler. Preferably you may just use this functionality that you need in the Handler rather than middleware.
On the middleware, you need call the next handler so all incoming requests will proceed to the destination route.
func dynamicRouteMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Error")
next.ServeHTTP(w, r) // <------- this one
})
}
You can register any routes as you want, but in the very end make sure the r object used as handler of / route.
r.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test"))
})
r.HandleFunc("/test/12", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("test 12"))
})
r.HandleFunc("/about-us", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("about us"))
})
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
When you access /test, /test/12, or /about-us; the Error will still be printed.
Previously it's not printed because you don't proceed to the next handler. The code next.ServeHTTP(w, r) is mandatory in your case.

router in Go - run a function before each http request

I'm using Go with http with Go like this:
mux := http.NewServeMux()
mux.HandleFunc("/API/user", test)
mux.HandleFunc("/authAPI/admin", auth)
and I would like to run a function before every http request
and better yet, run a function on every request that has the /authAPI/ in them.
how can I achieve this in Go?
On top of what #Thomas has proposed, you can wrap the whole mux in your own mux that is called before any handler is invoked, and can just invoke handlers of its own. That's how alternative http routers are implemented in go. Example:
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Handled %s", r.RequestURI)
}
func main(){
// this will do the actual routing, but it's not mandatory,
// we can write a custom router if we want
mux := http.NewServeMux()
mux.HandleFunc("/foo", handler)
mux.HandleFunc("/bar", handler)
// we pass a custom http handler that does preprocessing and calls mux to call the actual handler
http.ListenAndServe(":8081", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request){
fmt.Fprintln(w, "Preprocessing yo")
mux.ServeHTTP(w,r)
}))
}
You could just write a wrapper function:
func wrapHandlerFunc(handler http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
// ...
// do something
// ...
handler(w, req)
}
}
And use it like this:
mux.HandleFunc("/authAPI/admin", wrapHandlerFunc(auth))
Running it automatically for everything under a given URL tree (subrouter, in mux parlance) is, as far as I know, not supported.

Go and Gorilla Mux NotFoundHandler not working

I just can't get this NotFoundHandler to work. I'd like to serve a static file on every get request, given that it exists, otherwise serve index.html. Here's my simplified router at the moment:
func fooHandler() http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Foo"))
}
return http.HandlerFunc(fn)
}
func notFound(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "public/index.html")
}
func main() {
router = mux.NewRouter()
fs := http.FileServer(http.Dir("public"))
router.Handle("/foo", fooHandler())
router.PathPrefix("/").Handler(fs)
router.NotFoundHandler = http.HandlerFunc(notFound)
http.ListenAndServe(":3000", router)
}
/foo works ok
/file-that-exists works ok
/file-that-doesnt-exist doesn't work
- I get 404 page not found instead of index.html
So what am I doing wrong here?
The problem is that router.PathPrefix("/").Handler(fs) will match every route and the NotFoundHandler is never executed.
The NotFoundHandler is only executed when the router can't find a matching route.
When you explicitly define your routes it works as expected.
You could do something like:
router.Handle("/foo", fooHandler())
router.PathPrefix("/assets").Handler(fs)
router.HandleFunc("/", index)
router.HandleFunc("/about", about)
router.HandleFunc("/contact", contact)
router.NotFoundHandler = http.HandlerFunc(notFound)
This has worked for me
r.NotFoundHandler = http.HandlerFunc(NotFound)
Makesure your 'NotFound' function has:
func NotFound(w http.ResponseWriter, r *http.Request) { // a * before http.Request

Nested Gorilla Mux router does not work

Using code below, when I access /test2 it responds with 404 - not found. /test1 works correctly. Why is that? Is nesting not allowed despite the fact that routers implement http.Handler interface?
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
mainRouter := mux.NewRouter()
subRouter := mux.NewRouter()
mainRouter.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test1") })
subRouter.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test2") })
mainRouter.Handle("/", subRouter)
http.ListenAndServe(":9999", mainRouter)
}
EDIT:
My main goal was to add some initial work which would be common for all routes in subRouter, and only for them. To be even more specific, I would like to use Negroni as my middleware orchiestrator.
On the Negroni website there is an example of adding middleware to the group of routes:
router := mux.NewRouter()
adminRoutes := mux.NewRouter()
// add admin routes here
Create a new negroni for the admin middleware
router.Handle("/admin", negroni.New(
Middleware1,
Middleware2,
negroni.Wrap(adminRoutes),
))
Negroni basically executes ServeHTTP methods of every argument, since all of them implement http.Handler. It executes them in order, so router routes will be last.
I'm familiar with the concept of Subrouter in Mux, but AFAIK I can't use it in similar fashion as example above, in particular, I can't inject anything between mainRouter and its Subrouter. This is why nesting looks more flexible.
I know this question is somewhat old, but I have spent some time figuring out how handlers and matching work in go. You can see my experiment code here.
Basically, you can get the effect you want with code like this:
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
mainRouter := mux.NewRouter()
subRouter := mux.NewRouter()
mainRouter.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "test1")
})
subRouter.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "test2")
})
// in mux, you need to register subrouter
// with the same path that the handlers in
// it are matching
mainRouter.Handle("/test2", subRouter)
// if your subrouter has handlers that match
// other sub paths - you also need to do this
mainRouter.Handle("/test2/{_dummy:.*}", subRouter)
http.ListenAndServe(":9999", mainRouter)
}
I hope this helps someone.
None of the previous answers given helped me achieve exactly what I was seeking out to do. I was trying to use negroni.Wrap() around a Subrouter with lots of routes. I believe that is what the original poster wanted.
Additional Context: I am deploying on Google App Engine, hence putting everything in the init() function.
package hello
import (
"fmt"
"net/http"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
)
func init() {
// Create the "root" router, if you will...
r := mux.NewRouter().StrictSlash(true)
// Create your "Subrouter" dedicated to /api which will use the PathPrefix
apiRouter := mux.NewRouter().PathPrefix("/api").Subrouter().StrictSlash(true)
// This step is where we connect our "root" router and our "Subrouter" together.
r.PathPrefix("/api").Handler(negroni.New(
negroni.HandlerFunc(myMiddleware),
negroni.Wrap(apiRouter),
))
// Define "root" routes using r
r.HandleFunc(genHandleFunc("/", "root of site"))
r.HandleFunc(genHandleFunc("/home", "home"))
// Define "Subrouter" routes using apiRouter, prefix is /api
apiRouter.HandleFunc(genHandleFunc("/", "root of API, /api")) // Matches: /api
apiRouter.HandleFunc(genHandleFunc("/v1", "root of API V1, /api/v1")) // Matches: /api/v1
apiRouter.HandleFunc(genHandleFunc("/v1/resourceabc", "API V1 - resourceabc, /api/v1/resourceabc")) // Matches: /api/v1/resourceabc
/* Finally we pass our "root" router to the net/http library. The "root" router will contain all
of the routes for /api also.
*/
http.Handle("/", r)
}
// Silly function to quickly generate a HandleFunc
func genHandleFunc(p string, msg string) (path string, f func(http.ResponseWriter, *http.Request)) {
path = p
f = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%v\n", msg)
}
return
}
func myMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
fmt.Fprintln(w, "Before doing work...")
next(w, r)
fmt.Fprintln(w, "After doing work...")
}
You wouldn't use two routers here anyway.
Gorilla Mux has the concept of a Subrouter, whereby you define the top level domain properties on the main router, then use the Subrouter instance to map the individual paths.
For example:
mainRouter := mux.NewRouter()
subRouter := mainRouter.PathPrefix("/").Subrouter()
subRouter.HandleFunc("/test1", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test1") })
subRouter.HandleFunc("/test2", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test2") })
mainRouter.Handle("/", mainRouter)
You can go even further than that though - for example, you may have another router at /test1 and a subrouter that matches anything further below that (say /test1/othertest).
Use full path in subroutes:
router := mux.NewRouter()
apiRoutes := mux.NewRouter()
apiRoutes.Handle("/api/auth", Auth)
router.PathPrefix("/api").Handler(negroni.New(
Middleware1,
Middleware2,
negroni.Wrap(apiRoutes),
))

Resources