Skip to content

Commit 88ef11a

Browse files
authored
Merge pull request #3003 from kemalturk/firebase-functions-improvement
Firebase functions improvement
2 parents b3b7d80 + 04aa5b7 commit 88ef11a

File tree

4 files changed

+21
-134
lines changed

4 files changed

+21
-134
lines changed

firebase-functions/Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
PROJECT_ID=<ProjectID>
2+
FUNCTION_NAME=<FunctionName>
3+
RUNTIME=go121
4+
ENTRY_POINT=ServerFunction
5+
16
deploy:
2-
gcloud config set project <ProjectID>
3-
gcloud functions deploy ServerFunction --runtime go120 --trigger-http
7+
gcloud config set project $(PROJECT_ID)
8+
gcloud functions deploy $(FUNCTION_NAME) \
9+
--runtime $(RUNTIME) \
10+
--entry-point $(ENTRY_POINT) \
11+
--trigger-http \
12+
--no-gen2

firebase-functions/README.md

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -205,74 +205,18 @@ In `functions.go`, convert Google Cloud Function requests to Fiber and route the
205205
package app
206206

207207
import (
208-
"io"
209-
"net"
210208
"net/http"
211209

212210
"github.com/gofiber/fiber/v2"
213-
"github.com/gofiber/fiber/v2/utils"
214-
"github.com/valyala/fasthttp"
211+
adaptor "github.com/gofiber/fiber/v2/middleware/adaptor"
215212
)
216213

217214
// CloudFunctionRouteToFiber route cloud function http.Handler to *fiber.App
218215
// Internally, google calls the function with the /execute base URL
219-
func CloudFunctionRouteToFiber(fiberApp *fiber.App, w http.ResponseWriter, r *http.Request) error {
220-
221-
// Convert net/http -> fasthttp Ctx
222-
ctx := ConvertNetHTTPRequestToFastHTTPCtx(r, w)
223-
224-
// Run Fiber
225-
fiberApp.Handler()(ctx)
226-
227-
// Convert fasthttp Ctx -> net/http
228-
ctx.Response.Header.VisitAll(func(k, v []byte) {
229-
w.Header().Add(string(k), string(v))
230-
})
231-
w.WriteHeader(ctx.Response.StatusCode())
232-
_, err := w.Write(ctx.Response.Body())
233-
234-
return err
216+
func CloudFunctionRouteToFiber(fiberApp *fiber.App, w http.ResponseWriter, r *http.Request) {
217+
adaptor.FiberApp(fiberApp)(w, r)
235218
}
236219

237-
// ConvertNetHTTPRequestToFastHTTPCtx converts a net/http.Request to fasthttp.RequestCtx
238-
func ConvertNetHTTPRequestToFastHTTPCtx(r *http.Request, w http.ResponseWriter) *fasthttp.RequestCtx {
239-
// New fasthttp request
240-
req := fasthttp.AcquireRequest()
241-
defer fasthttp.ReleaseRequest(req)
242-
// Convert net/http -> fasthttp request
243-
if r.Body != nil {
244-
n, err := io.Copy(req.BodyWriter(), r.Body)
245-
req.Header.SetContentLength(int(n))
246-
247-
if err != nil {
248-
http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
249-
return nil
250-
}
251-
}
252-
req.Header.SetMethod(r.Method)
253-
req.SetRequestURI(r.RequestURI)
254-
req.SetHost(r.Host)
255-
req.Header.SetHost(r.Host)
256-
for key, val := range r.Header {
257-
for _, v := range val {
258-
req.Header.Set(key, v)
259-
}
260-
}
261-
262-
if _, _, err := net.SplitHostPort(r.RemoteAddr); err != nil && err.(*net.AddrError).Err == "missing port in address" {
263-
r.RemoteAddr = net.JoinHostPort(r.RemoteAddr, "80")
264-
}
265-
remoteAddr, err := net.ResolveTCPAddr("tcp", r.RemoteAddr)
266-
if err != nil {
267-
http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
268-
return nil
269-
}
270-
271-
var fctx fasthttp.RequestCtx
272-
fctx.Init(req, remoteAddr, nil)
273-
274-
return &fctx
275-
}
276220
```
277221

278222
## Main Application Entry
@@ -306,13 +250,9 @@ func Start(addr string) error {
306250
return app.Listen(addr)
307251
}
308252

309-
// MyCloudFunction Exported http.HandlerFunc to be deployed to as a Cloud Function
310-
func MyCloudFunction(w http.ResponseWriter, r *http.Request) {
311-
err := CloudFunctionRouteToFiber(app, w, r)
312-
if err != nil {
313-
fmt.Fprintf(w, "err : %v", err)
314-
return
315-
}
253+
// ServerFunction Exported http.HandlerFunc to be deployed to as a Cloud Function
254+
func ServerFunction(w http.ResponseWriter, r *http.Request) {
255+
CloudFunctionRouteToFiber(app, w, r)
316256
}
317257
```
318258

firebase-functions/functions.go

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,14 @@
11
package app
22

33
import (
4-
"io"
5-
"net"
64
"net/http"
75

86
"github.com/gofiber/fiber/v2"
9-
"github.com/gofiber/fiber/v2/utils"
10-
"github.com/valyala/fasthttp"
7+
adaptor "github.com/gofiber/fiber/v2/middleware/adaptor"
118
)
129

1310
// CloudFunctionRouteToFiber route cloud function http.Handler to *fiber.App
1411
// Internally, google calls the function with the /execute base URL
15-
func CloudFunctionRouteToFiber(fiberApp *fiber.App, w http.ResponseWriter, r *http.Request) error {
16-
17-
// Convert net/http -> fasthttp Ctx
18-
ctx := ConvertNetHTTPRequestToFastHTTPCtx(r, w)
19-
20-
// Run Fiber
21-
fiberApp.Handler()(ctx)
22-
23-
// Convert fasthttp Ctx -> net/http
24-
ctx.Response.Header.VisitAll(func(k, v []byte) {
25-
w.Header().Add(string(k), string(v))
26-
})
27-
w.WriteHeader(ctx.Response.StatusCode())
28-
_, err := w.Write(ctx.Response.Body())
29-
30-
return err
31-
}
32-
33-
// ConvertNetHTTPRequestToFastHTTPCtx converts a net/http.Request to fasthttp.RequestCtx
34-
func ConvertNetHTTPRequestToFastHTTPCtx(r *http.Request, w http.ResponseWriter) *fasthttp.RequestCtx {
35-
// New fasthttp request
36-
req := fasthttp.AcquireRequest()
37-
defer fasthttp.ReleaseRequest(req)
38-
// Convert net/http -> fasthttp request
39-
if r.Body != nil {
40-
n, err := io.Copy(req.BodyWriter(), r.Body)
41-
req.Header.SetContentLength(int(n))
42-
43-
if err != nil {
44-
http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
45-
return nil
46-
}
47-
}
48-
req.Header.SetMethod(r.Method)
49-
req.SetRequestURI(r.RequestURI)
50-
req.SetHost(r.Host)
51-
req.Header.SetHost(r.Host)
52-
for key, val := range r.Header {
53-
for _, v := range val {
54-
req.Header.Set(key, v)
55-
}
56-
}
57-
58-
if _, _, err := net.SplitHostPort(r.RemoteAddr); err != nil && err.(*net.AddrError).Err == "missing port in address" {
59-
r.RemoteAddr = net.JoinHostPort(r.RemoteAddr, "80")
60-
}
61-
remoteAddr, err := net.ResolveTCPAddr("tcp", r.RemoteAddr)
62-
if err != nil {
63-
http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
64-
return nil
65-
}
66-
67-
var fctx fasthttp.RequestCtx
68-
fctx.Init(req, remoteAddr, nil)
69-
70-
return &fctx
12+
func CloudFunctionRouteToFiber(fiberApp *fiber.App, w http.ResponseWriter, r *http.Request) {
13+
adaptor.FiberApp(fiberApp)(w, r)
7114
}

firebase-functions/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package app
22

33
import (
4-
"fmt"
54
"net/http"
65
"strings"
76

@@ -26,9 +25,5 @@ func Start(addr string) error {
2625

2726
// ServerFunction Exported http.HandlerFunc to be deployed to as a Cloud Function
2827
func ServerFunction(w http.ResponseWriter, r *http.Request) {
29-
err := CloudFunctionRouteToFiber(app, w, r)
30-
if err != nil {
31-
fmt.Fprintf(w, "err : %v", err)
32-
return
33-
}
28+
CloudFunctionRouteToFiber(app, w, r)
3429
}

0 commit comments

Comments
 (0)