-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
101 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module github.com/ceriath/gorouter/v4 | ||
module github.com/vardius/gorouter/v4 | ||
|
||
go 1.20 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
id: apphandler | ||
title: App Handler | ||
sidebar_label: App Handler | ||
--- | ||
|
||
## Use custom handler type in your application | ||
|
||
<!--DOCUSAURUS_CODE_TABS--> | ||
<!--net/http--> | ||
|
||
```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)) | ||
} | ||
``` | ||
<!--valyala/fasthttp--> | ||
```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)) | ||
} | ||
``` | ||
<!--END_DOCUSAURUS_CODE_TABS--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ | |
"https", | ||
"http2", | ||
"multidomain", | ||
"panic" | ||
"panic", | ||
"apphandler" | ||
], | ||
"Benchmark": ["benchmark"] | ||
} | ||
|