Mounting Sub Router

Rafał Lorenz edited this page Feb 26, 2018 · 2 revisions

When having multiple instance of a router you might want to mount one as a sub router of another under some route path, still keeping all middleware.

It doesn't have to be gorouter. You can mount other routers as well as long they implement http.Handler interface.

Mount

package main

import (
   "log"
   "net/http"

   "github.com/vardius/gorouter"
)

func main() {
    router := gorouter.New()
    subrouter := gorouter.New()

    router.Mount("/{param}", subrouter)

    log.Fatal(http.ListenAndServe(":8080", router))
}

Given example will result in all routes of a subrouter being available under paths prefixed with a mount path.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.