use short name httprouter

This commit is contained in:
Martin Riedl 2021-03-21 17:12:32 +01:00
parent 8ecd10eff8
commit f3555a603d
Signed by: martinr92
GPG key ID: FB68DA65516A804C

View file

@ -11,10 +11,11 @@ goHTTPRouter is a framework used for HTTP request routing.
## Simple Routing ## Simple Routing
Just replace the standard router of golang with this one: Just replace the standard router of golang with this one:
```golang ```golang
import "gitlab.com/martinr92/gohttprouter/v2" import httprouter "gitlab.com/martinr92/gohttprouter/v2"
```
router := gohttprouter.New() ```golang
router.HandleFunc(http.MethodGet, "/home", func(response http.ResponseWriter, request *http.Request, info gohttprouter.RoutingInfo) { router := httprouter.New()
router.HandleFunc(http.MethodGet, "/home", func(response http.ResponseWriter, request *http.Request, info httprouter.RoutingInfo) {
response.Write([]byte("Home")) response.Write([]byte("Home"))
}) })
err := http.ListenAndServe("localhost:8080", router) err := http.ListenAndServe("localhost:8080", router)
@ -23,12 +24,13 @@ err := http.ListenAndServe("localhost:8080", router)
## Routing with placeholder ## 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. 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 ```golang
import "gitlab.com/martinr92/gohttprouter/v2" import httprouter "gitlab.com/martinr92/gohttprouter/v2"
```
router := gohttprouter.New() ```golang
router := httprouter.New()
router.HandleFunc(http.MethodGet, "/user/:id", handleUserPages) router.HandleFunc(http.MethodGet, "/user/:id", handleUserPages)
router.HandleFunc(http.MethodGet, "/user/:id/settings", handleUserPages) router.HandleFunc(http.MethodGet, "/user/:id/settings", handleUserPages)
func handleUserPages(response http.ResponseWriter, request *http.Request, info gohttprouter.RoutingInfo) { func handleUserPages(response http.ResponseWriter, request *http.Request, info httprouter.RoutingInfo) {
id := info.Parameters["id"] id := info.Parameters["id"]
response.Write([]byte(id)) response.Write([]byte(id))
} }
@ -38,22 +40,23 @@ err := http.ListenAndServe("localhost:8080", router)
## Serve Static Content ## 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. 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 ```golang
import "gitlab.com/martinr92/gohttprouter/v2" import httprouter "gitlab.com/martinr92/gohttprouter/v2"
//go:embed files/statc/* //go:embed files/statc/*
var staticFiles embed.FS var staticFiles embed.FS
``` ```
```golang ```golang
staticFS := gohttprouter.NewFS(&staticFiles) staticFS := httprouter.NewFS(&staticFiles)
router := gohttprouter.New() router := httprouter.New()
router.Handle(http.MethodGet, "/static/*", staticFS) 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. 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 ```golang
import "gitlab.com/martinr92/gohttprouter/v2" import httprouter "gitlab.com/martinr92/gohttprouter/v2"
```
staticFS := gohttprouter.NewFS(&staticFiles) ```golang
staticFS := httprouter.NewFS(&staticFiles)
staticFS.UseLocalFolder = true staticFS.UseLocalFolder = true
staticFS.LocalFolderPrefix = "some/folder" // optional staticFS.LocalFolderPrefix = "some/folder" // optional
``` ```