Adaptor

原文: https://docs.gofiber.io/api/middleware/adaptor

Adaptor 适配器

Converter for net/http handlers to/from Fiber request handlers, special thanks to @arsmn!

​ net/http 处理程序到/从 Fiber 请求处理程序的转换器,特别感谢 @arsmn!

Signatures 签名

Name 名称Signature 签名Description 说明
HTTPHandlerHTTPHandler(h http.Handler) fiber.Handlerhttp.Handler -> fiber.Handler
HTTPHandlerFuncHTTPHandlerFunc(h http.HandlerFunc) fiber.Handlerhttp.HandlerFunc -> fiber.Handler
HTTPMiddlewareHTTPHandlerFunc(mw func(http.Handler) http.Handler) fiber.Handlerfunc(http.Handler) http.Handler -> fiber.Handler
FiberHandlerFiberHandler(h fiber.Handler) http.Handlerfiber.Handler -> http.Handler
FiberHandlerFuncFiberHandlerFunc(h fiber.Handler) http.HandlerFuncfiber.Handler -> http.HandlerFunc
FiberAppFiberApp(app *fiber.App) http.HandlerFuncFiber app -> http.HandlerFunc
ConvertRequestConvertRequest(c *fiber.Ctx, forServer bool) (*http.Request, error)fiber.Ctx -> http.Request
CopyContextToFiberContextCopyContextToFiberContext(context interface{}, requestContext *fasthttp.RequestCtx)context.Context -> fasthttp.RequestCtx

Examples 示例

net/http to Fiber net/http 到 Fiber

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
    "fmt"
    "net/http"

    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/adaptor"
)

func main() {
    // New fiber app
    app := fiber.New()

    // http.Handler -> fiber.Handler
    app.Get("/", adaptor.HTTPHandler(handler(greet)))

    // http.HandlerFunc -> fiber.Handler
    app.Get("/func", adaptor.HTTPHandlerFunc(greet))

    // Listen on port 3000
    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!")
}

net/http middleware to Fiber net/http 中间件到 Fiber Handler Fiber Handler 到 net/http Fiber App 到 net/http Fiber 到 (net/http).Request BasicAuth

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package main

import (
    "log"
    "net/http"

    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/adaptor"
)

func main() {
    // New fiber app
    app := fiber.New()

    // http middleware -> fiber.Handler
    app.Use(adaptor.HTTPMiddleware(logMiddleware))

    // Listen on port 3000
    app.Listen(":3000")
}

func logMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Println("log middleware")
        next.ServeHTTP(w, r)
    })
}

Fiber Handler to net/http

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import (
    "net/http"

    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/adaptor"
)

func main() {
    // fiber.Handler -> http.Handler
    http.Handle("/", adaptor.FiberHandler(greet))

    // fiber.Handler -> http.HandlerFunc
    http.HandleFunc("/func", adaptor.FiberHandlerFunc(greet))

    // Listen on port 3000
    http.ListenAndServe(":3000", nil)
}

func greet(c *fiber.Ctx) error {
    return c.SendString("Hello World!")
}

Fiber App to net/http

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import (
    "net/http"

    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/adaptor"
)

func main() {
    app := fiber.New()

    app.Get("/greet", greet)

    // Listen on port 3000
    http.ListenAndServe(":3000", adaptor.FiberApp(app))
}

func greet(c *fiber.Ctx) error {
    return c.SendString("Hello World!")
}

Fiber Context to (net/http).Request

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main

import (
    "net/http"

    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/adaptor"
)

func main() {
    app := fiber.New()

    app.Get("/greet", greetWithHTTPReq)

    // Listen on port 3000
    http.ListenAndServe(":3000", adaptor.FiberApp(app))
}

func greetWithHTTPReq(c *fiber.Ctx) error {
    httpReq, err := adaptor.ConvertRequest(c, false)
    if err != nil {
        return err
    }

    return c.SendString("Request URL: " + httpReq.URL.String())
}
最后修改 October 10, 2024: 更新 (a4b8f85)