Skip to content
This repository has been archived by the owner on Apr 24, 2018. It is now read-only.

Added a new optional method that allows you to easily handle all the unregistered (404) routes. #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ You can specify custom regular expressions for routes:

mux.Get("/files/:param(.+)", handler)

You can specify a custom 404 not found route:

mux.NotFound(handler)

You can also create routes for static files:

pwd, _ := os.Getwd()
Expand Down
15 changes: 14 additions & 1 deletion routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,22 @@ type route struct {
handler http.HandlerFunc
}

type notFoundHandler func(http.ResponseWriter, *http.Request)

type RouteMux struct {
routes []*route
filters []http.HandlerFunc
notFoundHandler notFoundHandler
}

func New() *RouteMux {
return &RouteMux{}
}

func (m *RouteMux) NotFound(handler notFoundHandler) {
m.notFoundHandler = handler
}

// Get adds a new Route for GET requests.
func (m *RouteMux) Get(pattern string, handler http.HandlerFunc) {
m.AddRoute(GET, pattern, handler)
Expand Down Expand Up @@ -206,7 +213,13 @@ func (m *RouteMux) ServeHTTP(rw http.ResponseWriter, r *http.Request) {

//if no matches to url, throw a not found exception
if w.started == false {
http.NotFound(w, r)

if (m.notFoundHandler != nil) {
m.notFoundHandler(w, r)
} else {
http.NotFound(w, r)
}

}
}

Expand Down