use short name httprouter
This commit is contained in:
parent
8ecd10eff8
commit
86f644be76
1 changed files with 17 additions and 14 deletions
31
README.md
31
README.md
|
@ -11,10 +11,11 @@ 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) {
|
||||
import httprouter "gitlab.com/martinr92/gohttprouter/v2"
|
||||
```
|
||||
```golang
|
||||
router := httprouter.New()
|
||||
router.HandleFunc(http.MethodGet, "/home", func(response http.ResponseWriter, request *http.Request, info httprouter.RoutingInfo) {
|
||||
response.Write([]byte("Home"))
|
||||
})
|
||||
err := http.ListenAndServe("localhost:8080", router)
|
||||
|
@ -23,12 +24,13 @@ 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()
|
||||
import httprouter "gitlab.com/martinr92/gohttprouter/v2"
|
||||
```
|
||||
```golang
|
||||
router := httprouter.New()
|
||||
router.HandleFunc(http.MethodGet, "/user/:id", 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"]
|
||||
response.Write([]byte(id))
|
||||
}
|
||||
|
@ -38,22 +40,23 @@ 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"
|
||||
import httprouter "gitlab.com/martinr92/gohttprouter/v2"
|
||||
|
||||
//go:embed files/statc/*
|
||||
var staticFiles embed.FS
|
||||
```
|
||||
```golang
|
||||
staticFS := gohttprouter.NewFS(&staticFiles)
|
||||
router := gohttprouter.New()
|
||||
staticFS := httprouter.NewFS(&staticFiles)
|
||||
router := httprouter.New()
|
||||
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(&staticFiles)
|
||||
import httprouter "gitlab.com/martinr92/gohttprouter/v2"
|
||||
```
|
||||
```golang
|
||||
staticFS := httprouter.NewFS(&staticFiles)
|
||||
staticFS.UseLocalFolder = true
|
||||
staticFS.LocalFolderPrefix = "some/folder" // optional
|
||||
```
|
||||
|
|
Loading…
Add table
Reference in a new issue