Add cursory readme and MIT license.

This commit is contained in:
2020-12-07 20:52:23 -08:00
parent 1805ac77fe
commit 08228b2aa7
3 changed files with 33 additions and 1 deletions

25
README.md Normal file
View File

@ -0,0 +1,25 @@
# Go Router
```go
type Router struct {
NotFoundHandler http.Handler
}
```
## Setting path params
Register routes with Router.AddRoute. Parts of the URI which start with a ":" are parameters. For example, if you registered `/user/:userID`, it would match `example.com/user/3`. You would then get a param called `userID` which would equal `"3"`.
Inside the handler function you provide, access path parameters with the `PathParams` function.
```go
router.AddRoute(http.MethodGet, "user/:userID", func(w http.ResponseWriter, r *http.Request) {
params, _ := router.PathParams(requestMethod, requestPath)
userID := params["userID"]
})
```
## Not Found Handler
If Router.NotFoundHandler is not a set, a default handler will be called when a route is not found. If you want to set your own handler, set Router.NotFoundHandler with the http.Handler you would prefer.