From cc0960181067014f3c30d18b0a3cad95b91f66ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Lorenz?= Date: Tue, 2 Jan 2024 09:45:40 +1100 Subject: [PATCH] Fix paths --- doc.go | 2 +- example_test.go | 3 +- fasthttp.go | 6 +-- fasthttp_test.go | 2 +- go.mod | 2 +- mux/node.go | 6 +-- mux/tree.go | 6 +-- nethttp.go | 8 ++-- nethttp_test.go | 2 +- route_test.go | 4 +- tree.go | 2 +- website/docs/apphandler.md | 78 ++++++++++++++++++++++++++++++++++++++ website/src/sidebars.json | 3 +- 13 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 website/docs/apphandler.md diff --git a/doc.go b/doc.go index 1188c83..c9ded63 100644 --- a/doc.go +++ b/doc.go @@ -35,7 +35,7 @@ and can contain named wildcard placeholders *(e.g. {placeholder})* to match dyna Take the following example: - import "github.com/ceriath/gorouter/v4/context" + import "github.com/vardius/gorouter/v4/context" router.GET("/hello/{name:r([a-z]+)go}", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { params, _ := context.Parameters(r.Context()) diff --git a/example_test.go b/example_test.go index fa78550..344e81b 100644 --- a/example_test.go +++ b/example_test.go @@ -7,8 +7,7 @@ import ( "github.com/valyala/fasthttp" - "github.com/ceriath/gorouter/v4" - "github.com/ceriath/gorouter/v4/context" + "github.com/vardius/gorouter/v4/context" ) func handleNetHTTPRequest(method, path string, handler http.Handler) { diff --git a/fasthttp.go b/fasthttp.go index ad2c1ef..4f1332c 100644 --- a/fasthttp.go +++ b/fasthttp.go @@ -3,12 +3,12 @@ package gorouter import ( "strings" - pathutils "github.com/ceriath/gorouter/v4/path" + pathutils "github.com/vardius/gorouter/v4/path" "github.com/valyala/fasthttp" - "github.com/ceriath/gorouter/v4/middleware" - "github.com/ceriath/gorouter/v4/mux" + "github.com/vardius/gorouter/v4/middleware" + "github.com/vardius/gorouter/v4/mux" ) var allFasthttpMethods = []string{ diff --git a/fasthttp_test.go b/fasthttp_test.go index a660d24..d8cde2d 100644 --- a/fasthttp_test.go +++ b/fasthttp_test.go @@ -9,7 +9,7 @@ import ( "github.com/valyala/fasthttp" - "github.com/ceriath/gorouter/v4/context" + "github.com/vardius/gorouter/v4/context" ) func buildFastHTTPRequestContext(method, path string) *fasthttp.RequestCtx { diff --git a/go.mod b/go.mod index 4b03789..53b1fd6 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/ceriath/gorouter/v4 +module github.com/vardius/gorouter/v4 go 1.20 diff --git a/mux/node.go b/mux/node.go index 7fd48b1..2e9e81b 100644 --- a/mux/node.go +++ b/mux/node.go @@ -3,9 +3,9 @@ package mux import ( "regexp" - "github.com/ceriath/gorouter/v4/context" - "github.com/ceriath/gorouter/v4/middleware" - pathutils "github.com/ceriath/gorouter/v4/path" + "github.com/vardius/gorouter/v4/context" + "github.com/vardius/gorouter/v4/middleware" + pathutils "github.com/vardius/gorouter/v4/path" ) // NewNode provides new mux Node diff --git a/mux/tree.go b/mux/tree.go index ba4b0c6..8f77c96 100644 --- a/mux/tree.go +++ b/mux/tree.go @@ -6,9 +6,9 @@ import ( "sort" "strings" - "github.com/ceriath/gorouter/v4/context" - "github.com/ceriath/gorouter/v4/middleware" - pathutils "github.com/ceriath/gorouter/v4/path" + "github.com/vardius/gorouter/v4/context" + "github.com/vardius/gorouter/v4/middleware" + pathutils "github.com/vardius/gorouter/v4/path" ) // NewTree provides new empty Tree diff --git a/nethttp.go b/nethttp.go index 2883fa6..5e84c96 100644 --- a/nethttp.go +++ b/nethttp.go @@ -5,10 +5,10 @@ import ( "net/url" "strings" - "github.com/ceriath/gorouter/v4/context" - "github.com/ceriath/gorouter/v4/middleware" - "github.com/ceriath/gorouter/v4/mux" - pathutils "github.com/ceriath/gorouter/v4/path" + "github.com/vardius/gorouter/v4/context" + "github.com/vardius/gorouter/v4/middleware" + "github.com/vardius/gorouter/v4/mux" + pathutils "github.com/vardius/gorouter/v4/path" ) var allNethttpMethods = []string{ diff --git a/nethttp_test.go b/nethttp_test.go index 90caf8a..9abba03 100644 --- a/nethttp_test.go +++ b/nethttp_test.go @@ -8,7 +8,7 @@ import ( "strings" "testing" - "github.com/ceriath/gorouter/v4/context" + "github.com/vardius/gorouter/v4/context" ) func TestInterface(t *testing.T) { diff --git a/route_test.go b/route_test.go index be95dd6..978d5de 100644 --- a/route_test.go +++ b/route_test.go @@ -5,8 +5,8 @@ import ( "net/http/httptest" "testing" - "github.com/ceriath/gorouter/v4/context" - "github.com/ceriath/gorouter/v4/middleware" + "github.com/vardius/gorouter/v4/context" + "github.com/vardius/gorouter/v4/middleware" ) func TestRouter(t *testing.T) { diff --git a/tree.go b/tree.go index 5d519ae..a193dea 100644 --- a/tree.go +++ b/tree.go @@ -3,7 +3,7 @@ package gorouter import ( "net/http" - "github.com/ceriath/gorouter/v4/mux" + "github.com/vardius/gorouter/v4/mux" ) func allowed(t mux.Tree, method, path string) (allow string) { diff --git a/website/docs/apphandler.md b/website/docs/apphandler.md new file mode 100644 index 0000000..dc6fb34 --- /dev/null +++ b/website/docs/apphandler.md @@ -0,0 +1,78 @@ +--- +id: apphandler +title: App Handler +sidebar_label: App Handler +--- + +## Use custom handler type in your application + + + + +```go +package main + +import ( + "errors" + "fmt" + "log" + "net/http" + + "github.com/vardius/gorouter/v4" +) + +type AppHandlerFunc func(http.ResponseWriter, *http.Request) error + +// ServeHTTP calls f(w, r) and handles error +func (f AppHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if err := f(w, r); err != nil { + w.WriteHeader(http.StatusInternalServerError) + _, _ = fmt.Fprint(w, err.Error()) + } +} + +func Index(w http.ResponseWriter, r *http.Request) error { + return errors.New("I am app handler which can return error") +} + +func main() { + router := gorouter.New() + router.GET("/", AppHandlerFunc(Index)) + + log.Fatal(http.ListenAndServe(":8080", router)) +} +``` + +```go +package main + +import ( + "errors" + "log" + + "github.com/valyala/fasthttp" + "github.com/vardius/gorouter/v4" +) + +type AppHandlerFunc func(ctx *fasthttp.RequestCtx) error + +// HandleFastHTTP calls f(ctx) and handles error +func (f AppHandlerFunc) HandleFastHTTP(ctx *fasthttp.RequestCtx) { + if err := f(ctx); err != nil { + ctx.SetBody([]byte(err.Error())) + ctx.SetStatusCode(fasthttp.StatusInternalServerError) + } +} + +func index(_ *fasthttp.RequestCtx) error { + return errors.New("I am app handler which can return error") +} + +func main() { + router := gorouter.NewFastHTTPRouter() + router.GET("/", AppHandlerFunc(index)) + + log.Fatal(fasthttp.ListenAndServe(":8080", router.HandleFastHTTP)) +} +``` + diff --git a/website/src/sidebars.json b/website/src/sidebars.json index 4902490..bf69d9f 100644 --- a/website/src/sidebars.json +++ b/website/src/sidebars.json @@ -18,7 +18,8 @@ "https", "http2", "multidomain", - "panic" + "panic", + "apphandler" ], "Benchmark": ["benchmark"] }