updated readme for v2 release

This commit is contained in:
Martin Riedl 2021-03-18 17:59:28 +01:00
parent d8985d5a74
commit 50d3c1be28
Signed by: martinr92
GPG key ID: FB68DA65516A804C

View file

@ -11,6 +11,8 @@ goHTTPRouter is a framework used for HTTP request routing.
## Simple Routing
Just replace the standard router of golang with this one:
```golang
import "gitlab.com/martinr92/gohttprouter/v2"
router := gohttprouter.New()
router.HandleFunc(http.MethodGet, "/home", func(response http.ResponseWriter, request *http.Request, info gohttprouter.RoutingInfo) {
response.Write([]byte("Home"))
@ -21,6 +23,8 @@ err := http.ListenAndServe("localhost:8080", router)
## Routing with placeholder
A path can also contain placeholder (like :id). If then a request is sent to the url "/user/123" the method gets executed and the URL part (in this case "123") is passed as parameter into your handler function.
```golang
import "gitlab.com/martinr92/gohttprouter/v2"
router := gohttprouter.New()
router.HandleFunc(http.MethodGet, "/user/:id", handleUserPages)
router.HandleFunc(http.MethodGet, "/user/:id/settings", handleUserPages)
@ -34,6 +38,8 @@ err := http.ListenAndServe("localhost:8080", router)
## Serve Static Content
Static files (like JavaScript or CSS) can be served automatically (including caching header and MIME type). It uses the `embed.FS` (since go 1.16) to serve static content.
```golang
import "gitlab.com/martinr92/gohttprouter/v2"
//go:embed files/statc/*
var staticFiles embed.FS
```
@ -45,6 +51,8 @@ router.Handle(http.MethodGet, "/static/*", staticFS)
For development purpose you can enable the local file serving additionally. The framework checks first, if the file exists locally and serves it directly. If not, the file is served from the `embed.FS`. This helps you during local development so you can modify a CSS file without recompiling everything.
```golang
import "gitlab.com/martinr92/gohttprouter/v2"
staticFS := gohttprouter.NewFS(&staticFS)
staticFS.UseLocalFolder = true
staticFS.LocalFolderPrefix = "some/folder" // optional