This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
80 lines (72 loc) · 2.27 KB
/
main.go
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 🚀 Fiber is an Express inspired web framework written in Go with 💖
// 📌 API Documentation: https://fiber.wiki
// 📝 Github Repository: https://github.com/gofiber/fiber
package adaptor
import (
"io/ioutil"
"net"
"net/http"
"github.com/gofiber/fiber"
"github.com/gofiber/utils"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpadaptor"
)
// HTTPHandlerFunc wraps net/http handler func to fiber handler
func HTTPHandlerFunc(h http.HandlerFunc) func(*fiber.Ctx) {
return HTTPHandler(h)
}
// HTTPHandler wraps net/http handler to fiber handler
func HTTPHandler(h http.Handler) func(*fiber.Ctx) {
return func(c *fiber.Ctx) {
handler := fasthttpadaptor.NewFastHTTPHandler(h)
handler(c.Fasthttp)
}
}
// FiberHandler wraps fiber handler to net/http handler
func FiberHandler(h func(*fiber.Ctx)) http.Handler {
return FiberHandlerFunc(h)
}
// FiberHandlerFunc wraps fiber handler to net/http handler func
func FiberHandlerFunc(h func(*fiber.Ctx)) http.HandlerFunc {
app := fiber.New()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// New fasthttp request
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
// Convert net/http -> fasthttp request
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
return
}
req.Header.SetMethod(r.Method)
req.SetRequestURI(r.RequestURI)
req.Header.SetContentLength(len(body))
req.SetHost(r.Host)
for key, val := range r.Header {
for _, v := range val {
req.Header.Add(key, v)
}
}
_, _ = req.BodyWriter().Write(body)
remoteAddr, err := net.ResolveTCPAddr("tcp", r.RemoteAddr)
if err != nil {
http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
return
}
// New fasthttp Ctx
var fctx fasthttp.RequestCtx
fctx.Init(req, remoteAddr, nil)
// New fiber Ctx
ctx := app.AcquireCtx(&fctx)
defer app.ReleaseCtx(ctx)
// Execute fiber Ctx
h(ctx)
// Convert fasthttp Ctx > net/http
ctx.Fasthttp.Response.Header.VisitAll(func(k, v []byte) {
w.Header().Set(string(k), string(v))
})
w.WriteHeader(ctx.Fasthttp.Response.StatusCode())
_, _ = w.Write(ctx.Fasthttp.Response.Body())
})
}