Skip to content

Commit

Permalink
Add router prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan committed Oct 3, 2015
1 parent dfd14c0 commit c69841c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Bone is a lightweight and lightning fast HTTP Multiplexer for Golang. It support
- URL Parameters
- REGEX Parameters
- Wildcard routes
- Router Prefix
- Sub Router, `mux.SubRoute()`, support most standard router (bone, gorilla/mux, httpRouter etc...)
- Http method declaration
- Support for `http.Handler` and `http.HandlerFunc`
Expand Down Expand Up @@ -143,4 +144,7 @@ MIT

## Links
- Blog post talking about bone : http://www.peterbe.com/plog/my-favorite-go-multiplexer
- Middleware Chaining module : [Claw](https://github.com/go-zoo/claw)

## Libs
- Errors dump for Go : [Trash](https://github.com/go-zoo/trash)
- Middleware Chaining module : [Claw](https://github.com/go-zoo/claw)
13 changes: 10 additions & 3 deletions bone.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import "net/http"
// notFound: 404 handler, default http.NotFound if not provided
type Mux struct {
Routes map[string][]*Route
prefix string
notFound http.Handler
}

Expand All @@ -36,12 +37,18 @@ func New() *Mux {
return &Mux{Routes: make(map[string][]*Route)}
}

// Prefix set a default prefix for all routes registred on the router
func (m *Mux) Prefix(p string) *Mux {
m.prefix = p
return m
}

// Serve http request
func (m *Mux) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Check if a route match
if !m.parse(rw, req) {
// Check if it's a static ressource
if !m.StaticRoute(rw, req) {
if !m.staticRoute(rw, req) {
// Check if the request path doesn't end with /
if !m.validate(rw, req) {
m.HandleNotFound(rw, req)
Expand Down Expand Up @@ -104,7 +111,7 @@ func (m *Mux) NotFound(handler http.Handler) {

// Register the new route in the router with the provided method and handler
func (m *Mux) register(method string, path string, handler http.Handler) *Route {
r := NewRoute(path, handler)
r := NewRoute(m.prefix+path, handler)
if valid(path) {
m.Routes[method] = append(m.Routes[method], r)
return r
Expand All @@ -115,7 +122,7 @@ func (m *Mux) register(method string, path string, handler http.Handler) *Route

// SubRoute register a router as a SubRouter of bone
func (m *Mux) SubRoute(path string, router Router) *Route {
r := NewRoute(path, router)
r := NewRoute(m.prefix+path, router)
if valid(path) {
r.Atts += SUB
for _, mt := range method {
Expand Down
2 changes: 1 addition & 1 deletion example/003/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
rw.Write([]byte("Hello from httprouter mux"))
})

muxx := bone.New()
muxx := bone.New().Prefix("/api")

muxx.SubRoute("/bone", boneSub)
muxx.SubRoute("/gorilla", gorrilaSub)
Expand Down
3 changes: 1 addition & 2 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import "net/http"
func (m *Mux) parse(rw http.ResponseWriter, req *http.Request) bool {
for _, r := range m.Routes[req.Method] {
// If the route is equal to the request path.

if req.URL.Path == r.Path {
r.Handler.ServeHTTP(rw, req)
return true
Expand Down Expand Up @@ -48,7 +47,7 @@ func (m *Mux) HandleNotFound(rw http.ResponseWriter, req *http.Request) {
}

// StaticRoute check if the request path is for Static route
func (m *Mux) StaticRoute(rw http.ResponseWriter, req *http.Request) bool {
func (m *Mux) staticRoute(rw http.ResponseWriter, req *http.Request) bool {
for _, s := range m.Routes[static] {
if len(req.URL.Path) >= s.Size {
if req.URL.Path[:s.Size] == s.Path {
Expand Down

0 comments on commit c69841c

Please sign in to comment.