github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/adaptor.md (about) 1 --- 2 id: adaptor 3 title: Adaptor 4 --- 5 6 Converter for net/http handlers to/from Fiber request handlers, special thanks to [@arsmn](https://github.com/arsmn)! 7 8 ## Signatures 9 | Name | Signature | Description 10 | :--- | :--- | :--- 11 | HTTPHandler | `HTTPHandler(h http.Handler) fiber.Handler` | http.Handler -> fiber.Handler 12 | HTTPHandlerFunc | `HTTPHandlerFunc(h http.HandlerFunc) fiber.Handler` | http.HandlerFunc -> fiber.Handler 13 | HTTPMiddleware | `HTTPHandlerFunc(mw func(http.Handler) http.Handler) fiber.Handler` | func(http.Handler) http.Handler -> fiber.Handler 14 | FiberHandler | `FiberHandler(h fiber.Handler) http.Handler` | fiber.Handler -> http.Handler 15 | FiberHandlerFunc | `FiberHandlerFunc(h fiber.Handler) http.HandlerFunc` | fiber.Handler -> http.HandlerFunc 16 | FiberApp | `FiberApp(app *fiber.App) http.HandlerFunc` | Fiber app -> http.HandlerFunc 17 | ConvertRequest | `ConvertRequest(c *fiber.Ctx, forServer bool) (*http.Request, error)` | fiber.Ctx -> http.Request 18 | CopyContextToFiberContext | `CopyContextToFiberContext(context interface{}, requestContext *fasthttp.RequestCtx)` | context.Context -> fasthttp.RequestCtx 19 20 ## Examples 21 22 ### net/http to Fiber 23 ```go 24 package main 25 26 import ( 27 "fmt" 28 "net/http" 29 30 "github.com/gofiber/fiber/v2" 31 "github.com/gofiber/fiber/v2/middleware/adaptor" 32 ) 33 34 func main() { 35 // New fiber app 36 app := fiber.New() 37 38 // http.Handler -> fiber.Handler 39 app.Get("/", adaptor.HTTPHandler(handler(greet))) 40 41 // http.HandlerFunc -> fiber.Handler 42 app.Get("/func", adaptor.HTTPHandlerFunc(greet)) 43 44 // Listen on port 3000 45 app.Listen(":3000") 46 } 47 48 func handler(f http.HandlerFunc) http.Handler { 49 return http.HandlerFunc(f) 50 } 51 52 func greet(w http.ResponseWriter, r *http.Request) { 53 fmt.Fprint(w, "Hello World!") 54 } 55 ``` 56 57 ### net/http middleware to Fiber 58 ```go 59 package main 60 61 import ( 62 "log" 63 "net/http" 64 65 "github.com/gofiber/fiber/v2" 66 "github.com/gofiber/fiber/v2/middleware/adaptor" 67 ) 68 69 func main() { 70 // New fiber app 71 app := fiber.New() 72 73 // http middleware -> fiber.Handler 74 app.Use(adaptor.HTTPMiddleware(logMiddleware)) 75 76 // Listen on port 3000 77 app.Listen(":3000") 78 } 79 80 func logMiddleware(next http.Handler) http.Handler { 81 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 82 log.Println("log middleware") 83 next.ServeHTTP(w, r) 84 }) 85 } 86 ``` 87 88 ### Fiber Handler to net/http 89 ```go 90 package main 91 92 import ( 93 "net/http" 94 95 "github.com/gofiber/fiber/v2" 96 "github.com/gofiber/fiber/v2/middleware/adaptor" 97 ) 98 99 func main() { 100 // fiber.Handler -> http.Handler 101 http.Handle("/", adaptor.FiberHandler(greet)) 102 103 // fiber.Handler -> http.HandlerFunc 104 http.HandleFunc("/func", adaptor.FiberHandlerFunc(greet)) 105 106 // Listen on port 3000 107 http.ListenAndServe(":3000", nil) 108 } 109 110 func greet(c *fiber.Ctx) error { 111 return c.SendString("Hello World!") 112 } 113 ``` 114 115 ### Fiber App to net/http 116 ```go 117 package main 118 119 import ( 120 "net/http" 121 122 "github.com/gofiber/fiber/v2" 123 "github.com/gofiber/fiber/v2/middleware/adaptor" 124 ) 125 126 func main() { 127 app := fiber.New() 128 129 app.Get("/greet", greet) 130 131 // Listen on port 3000 132 http.ListenAndServe(":3000", adaptor.FiberApp(app)) 133 } 134 135 func greet(c *fiber.Ctx) error { 136 return c.SendString("Hello World!") 137 } 138 ``` 139 140 ### Fiber Context to (net/http).Request 141 ```go 142 package main 143 144 import ( 145 "net/http" 146 147 "github.com/gofiber/fiber/v2" 148 "github.com/gofiber/fiber/v2/middleware/adaptor" 149 ) 150 151 func main() { 152 app := fiber.New() 153 154 app.Get("/greet", greetWithHTTPReq) 155 156 // Listen on port 3000 157 http.ListenAndServe(":3000", adaptor.FiberApp(app)) 158 } 159 160 func greetWithHTTPReq(c *fiber.Ctx) error { 161 httpReq, err := adaptor.ConvertRequest(c, false) 162 if err != nil { 163 return err 164 } 165 166 return c.SendString("Request URL: " + httpReq.URL.String()) 167 } 168 ```