Skip to content

Commit

Permalink
feat: add a Firebase Functions step by step guide (#5318)
Browse files Browse the repository at this point in the history
* feat: add a Firebase Functions step by step guide

This PR adds a new section to the Serverless Guide on the website that explains how to use Fastify as the wrapper around Firebase's own onRequest HTTP handler

Signed-off-by: Liran Tal <liran.tal@gmail.com>

* Update docs/Guides/Serverless.md

Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com>
Signed-off-by: Liran Tal <liran.tal@gmail.com>

* Update docs/Guides/Serverless.md

Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com>
Signed-off-by: Liran Tal <liran.tal@gmail.com>

* Update docs/Guides/Serverless.md

Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com>
Signed-off-by: Liran Tal <liran.tal@gmail.com>

* Update docs/Guides/Serverless.md

Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com>
Signed-off-by: Liran Tal <liran.tal@gmail.com>

---------

Signed-off-by: Liran Tal <liran.tal@gmail.com>
Co-authored-by: James Sumners <321201+jsumners@users.noreply.github.com>
  • Loading branch information
lirantal and jsumners committed Feb 19, 2024
1 parent 54f8e3c commit 7e50e53
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions docs/Guides/Serverless.md
Expand Up @@ -26,6 +26,7 @@ snippet of code.

- [AWS](#aws)
- [Google Cloud Functions](#google-cloud-functions)
- [Google Firebase Functions](#google-firebase-functions)
- [Google Cloud Run](#google-cloud-run)
- [Netlify Lambda](#netlify-lambda)
- [Platformatic Cloud](#platformatic-cloud)
Expand Down Expand Up @@ -260,6 +261,115 @@ curl -X POST https://$GOOGLE_REGION-$GOOGLE_PROJECT.cloudfunctions.net/me \
- [Google Cloud Functions - Node.js Quickstart
](https://cloud.google.com/functions/docs/quickstart-nodejs)

## Google Firebase Functions

Follow this guide if you want to use Fastify as the HTTP framework for
Firebase Functions instead of the vanilla JavaScript router provided with
`onRequest(async (req, res) => {}`.

### The onRequest() handler

We use the `onRequest` function to wrap our Fastify application instance.

As such, we'll begin with importing it to the code:

```js
const { onRequest } = require("firebase-functions/v2/https")
```

### Creation of Fastify instance

Create the Fastify instance and encapsulate the returned application instance
in a function which will register routes, await the server's processing of
plugins, hooks and other settings. As follows:

```js
const fastify = require("fastify")({
logger: true,
})

const fastifyApp = async (request, reply) => {
await registerRoutes(fastify)
await fastify.ready()
fastify.server.emit("request", request, reply)
}
```

### Add Custom `contentTypeParser` to Fastify instance and define endpoints

Firebase Function's HTTP layer already parses the request
and makes a JSON payload available. It also provides access
to the raw body, unparsed, which is useful in order to calculate
request signatures to validate HTTP webhooks.

Add as follows to the `registerRoutes()` function:

```js
async function registerRoutes (fastify) {
fastify.addContentTypeParser("application/json", {}, (req, payload, done) => {
// useful to include the request's raw body on the `req` object that will
// later be available in your other routes so you can calculate the HMAC
// if needed
req.rawBody = payload.rawBody

// payload.body is already the parsed JSON so we just fire the done callback
// with it
done(null, payload.body)
})

// define your endpoints here...
fastify.post("/some-route-here", async (request, reply) => {}

fastify.get('/', async (request, reply) => {
reply.send({message: 'Hello World!'})
})
}
```
### Export the function using Firebase onRequest
Final step is to export the Fastify app instance to Firebase's own
`onRequest()` function so it can pass the request and reply objects to it:
```js
exports.app = onRequest(fastifyApp)
```
### Local test
Install the Firebase tools functions so you can use the CLI:
```bash
npm i -g firebase-tools
```
Then you can run your function locally with:
```bash
firebase emulators:start --only functions
```
### Deploy
Deploy your Firebase Functions with:
```bash
firebase deploy --only functions
```
#### Read logs
Use the Firebase tools CLI:
```bash
firebase functions:log
```
### References
- [Fastify on Firebase Functions](https://github.com/lirantal/lemon-squeezy-firebase-webhook-fastify/blob/main/package.json)
- [An article about HTTP webhooks on Firebase Functions and Fastify: A Practical Case Study with Lemon Squeezy](https://lirantal.com/blog/http-webhooks-firebase-functions-fastify-practical-case-study-lemon-squeezy)
## Google Cloud Run
Unlike AWS Lambda or Google Cloud Functions, Google Cloud Run is a serverless
Expand Down

0 comments on commit 7e50e53

Please sign in to comment.