📦 ship adaptor

pull/836/head
Fenny 2020-09-27 14:38:30 +02:00
parent 6395e1933c
commit a24e33be80
3 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,45 @@
# Adaptor
Adaptor for [Fiber](https://github.com/gofiber/fiber) Converter for net/http handlers to Fiber handlers, special thanks to [@arsmn](https://github.com/arsmn)!.
- [Signatures](#signatures)
- [Examples](#examples)
### Signatures
```go
func HTTPHandlerFunc(h http.HandlerFunc) fiber.Handler
func HTTPHandler(h http.Handler) fiber.Handler
```
### Example
Import the adaptor package that is part of the Fiber web framework
```go
import (
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/adaptor"
)
```
After you initiate your Fiber app, you can use the following possibilities:
```go
func main() {
app := fiber.New()
// http.Handler -> fiber.Handler
app.Get("/", adaptor.HTTPHandler(handler(greet)))
// http.HandlerFunc -> fiber.Handler
app.Get("/func", adaptor.HTTPHandlerFunc(greet))
log.Fatal(app.Listen(":3000"))
}
func handler(f http.HandlerFunc) http.Handler {
return http.HandlerFunc(f)
}
func greet(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello World!")
}
```

View File

@ -0,0 +1,22 @@
package adaptor
import (
"net/http"
"github.com/gofiber/fiber/v2"
"github.com/valyala/fasthttp/fasthttpadaptor"
)
// HTTPHandlerFunc wraps net/http handler func to fiber handler
func HTTPHandlerFunc(h http.HandlerFunc) fiber.Handler {
return HTTPHandler(h)
}
// HTTPHandler wraps net/http handler to fiber handler
func HTTPHandler(h http.Handler) fiber.Handler {
return func(c *fiber.Ctx) error {
handler := fasthttpadaptor.NewFastHTTPHandler(h)
handler(c.Context())
return nil
}
}

View File

@ -0,0 +1 @@
package adaptor