From 08228b2aa7ae7c608508c35cab04bc516fe2d452 Mon Sep 17 00:00:00 2001 From: nolwn Date: Mon, 7 Dec 2020 20:52:23 -0800 Subject: [PATCH] Add cursory readme and MIT license. --- README.md | 25 +++++++++++++++++++++++++ go.mod | 2 +- license | 7 +++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 README.md create mode 100644 license diff --git a/README.md b/README.md new file mode 100644 index 0000000..67d2761 --- /dev/null +++ b/README.md @@ -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. diff --git a/go.mod b/go.mod index 4f76c4a..b5906bd 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/jr-dev-league/go-router -go 1.14 \ No newline at end of file +go 1.15 \ No newline at end of file diff --git a/license b/license new file mode 100644 index 0000000..525e963 --- /dev/null +++ b/license @@ -0,0 +1,7 @@ +Copyright 2020 Nolan Hellyer + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file