Aktueller Stand
This commit is contained in:
189
backend/node_modules/fastify/docs/Guides/Serverless.md
generated
vendored
189
backend/node_modules/fastify/docs/Guides/Serverless.md
generated
vendored
@@ -1,20 +1,20 @@
|
||||
<h1 align="center">Serverless</h1>
|
||||
|
||||
Run serverless applications and REST APIs using your existing Fastify
|
||||
application. By default, Fastify will not work on your serverless platform of
|
||||
choice, you will need to make some small changes to fix this. This document
|
||||
contains a small guide for the most popular serverless providers and how to use
|
||||
application. You may need to make code changes to work on your
|
||||
serverless platform of choice. This document contains a small guide
|
||||
for the most popular serverless providers and how to use
|
||||
Fastify with them.
|
||||
|
||||
#### Should you use Fastify in a serverless platform?
|
||||
|
||||
That is up to you! Keep in mind that functions as a service should always use
|
||||
That is up to you! Keep in mind, functions as a service should always use
|
||||
small and focused functions, but you can also run an entire web application with
|
||||
them. It is important to remember that the bigger the application the slower the
|
||||
initial boot will be. The best way to run Fastify applications in serverless
|
||||
environments is to use platforms like Google Cloud Run, AWS Fargate, and Azure
|
||||
Container Instances, where the server can handle multiple requests at the same
|
||||
time and make full use of Fastify's features.
|
||||
environments is to use platforms like Google Cloud Run, AWS Fargate, Azure
|
||||
Container Instances, and Vercel where the server can handle multiple requests
|
||||
at the same time and make full use of Fastify's features.
|
||||
|
||||
One of the best features of using Fastify in serverless applications is the ease
|
||||
of development. In your local environment, you will always run the Fastify
|
||||
@@ -25,21 +25,21 @@ snippet of code.
|
||||
### Contents
|
||||
|
||||
- [AWS](#aws)
|
||||
- [Genezio](#genezio)
|
||||
- [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)
|
||||
- [Vercel](#vercel)
|
||||
|
||||
## AWS
|
||||
|
||||
To integrate with AWS, you have two choices of library:
|
||||
|
||||
- Using [@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify)
|
||||
- Using [@fastify/aws-lambda](https://github.com/fastify/aws-lambda-fastify)
|
||||
which only adds API Gateway support but has heavy optimizations for fastify.
|
||||
- Using [@h4ad/serverless-adapter](https://github.com/H4ad/serverless-adapter)
|
||||
which is a little slower as it creates an HTTP request for each AWS event but
|
||||
- Using [@h4ad/serverless-adapter](https://github.com/H4ad/serverless-adapter)
|
||||
which is a little slower as it creates an HTTP request for each AWS event but
|
||||
has support for more AWS services such as: AWS SQS, AWS SNS and others.
|
||||
|
||||
So you can decide which option is best for you, but you can test both libraries.
|
||||
@@ -129,6 +129,13 @@ If you need to integrate with more AWS services, take a look at
|
||||
[@h4ad/serverless-adapter](https://viniciusl.com.br/serverless-adapter/docs/main/frameworks/fastify)
|
||||
on Fastify to find out how to integrate.
|
||||
|
||||
## Genezio
|
||||
|
||||
[Genezio](https://genezio.com/) is a platform designed to simplify the deployment
|
||||
of serverless applications to the cloud.
|
||||
|
||||
[Genezio has a dedicated guide for deploying a Fastify application.](https://genezio.com/docs/frameworks/fastify/)
|
||||
|
||||
## Google Cloud Functions
|
||||
|
||||
### Creation of Fastify instance
|
||||
@@ -230,14 +237,13 @@ npx @google-cloud/functions-framework --target=fastifyFunction
|
||||
Or add this command to your `package.json` scripts:
|
||||
```json
|
||||
"scripts": {
|
||||
...
|
||||
"dev": "npx @google-cloud/functions-framework --target=fastifyFunction"
|
||||
...
|
||||
...
|
||||
"dev": "npx @google-cloud/functions-framework --target=fastifyFunction"
|
||||
...
|
||||
}
|
||||
```
|
||||
and run it with `npm run dev`.
|
||||
|
||||
|
||||
### Deploy
|
||||
```bash
|
||||
gcloud functions deploy fastifyFunction \
|
||||
@@ -263,7 +269,7 @@ curl -X POST https://$GOOGLE_REGION-$GOOGLE_PROJECT.cloudfunctions.net/me \
|
||||
|
||||
## Google Firebase Functions
|
||||
|
||||
Follow this guide if you want to use Fastify as the HTTP framework for
|
||||
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) => {}`.
|
||||
|
||||
@@ -280,8 +286,8 @@ 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:
|
||||
in a function that will register routes, await the server's processing of
|
||||
plugins, hooks, and other settings. As follows:
|
||||
|
||||
```js
|
||||
const fastify = require("fastify")({
|
||||
@@ -297,10 +303,10 @@ const fastifyApp = async (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.
|
||||
Firebase Function's HTTP layer already parses the request and makes a JSON
|
||||
payload available through the property `payload.body` below. It also provides
|
||||
access to the raw body, unparsed, which is useful for calculating request
|
||||
signatures to validate HTTP webhooks.
|
||||
|
||||
Add as follows to the `registerRoutes()` function:
|
||||
|
||||
@@ -318,7 +324,7 @@ async function registerRoutes (fastify) {
|
||||
})
|
||||
|
||||
// define your endpoints here...
|
||||
fastify.post("/some-route-here", async (request, reply) => {}
|
||||
fastify.post("/some-route-here", async (request, reply) => {})
|
||||
|
||||
fastify.get('/', async (request, reply) => {
|
||||
reply.send({message: 'Hello World!'})
|
||||
@@ -326,6 +332,24 @@ async function registerRoutes (fastify) {
|
||||
}
|
||||
```
|
||||
|
||||
**Failing to add this `ContentTypeParser` may lead to the Fastify process
|
||||
remaining stuck and not processing any other requests after receiving one with
|
||||
the Content-Type `application/json`.**
|
||||
|
||||
When using Typescript, since the type of `payload` is a native `IncomingMessage`
|
||||
that gets modified by Firebase, it won't be able to find the property
|
||||
`payload.body`.
|
||||
|
||||
In order to suppress the error, you can use the following signature:
|
||||
|
||||
```ts
|
||||
declare module 'http' {
|
||||
interface IncomingMessage {
|
||||
body?: unknown;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Export the function using Firebase onRequest
|
||||
|
||||
Final step is to export the Fastify app instance to Firebase's own
|
||||
@@ -369,7 +393,6 @@ firebase functions:log
|
||||
- [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
|
||||
@@ -384,7 +407,7 @@ familiar with gcloud or just follow their
|
||||
|
||||
### Adjust Fastify server
|
||||
|
||||
In order for Fastify to properly listen for requests within the container, be
|
||||
For Fastify to properly listen for requests within the container, be
|
||||
sure to set the correct port and address:
|
||||
|
||||
```js
|
||||
@@ -455,7 +478,7 @@ CMD [ "npm", "start" ]
|
||||
To keep build artifacts out of your container (which keeps it small and improves
|
||||
build times) add a `.dockerignore` file like the one below:
|
||||
|
||||
```.dockerignore
|
||||
```dockerignore
|
||||
Dockerfile
|
||||
README.md
|
||||
node_modules
|
||||
@@ -482,12 +505,11 @@ gcloud beta run deploy --image gcr.io/PROJECT-ID/APP-NAME --platform managed
|
||||
|
||||
Your app will be accessible from the URL GCP provides.
|
||||
|
||||
|
||||
## netlify-lambda
|
||||
|
||||
First, please perform all preparation steps related to **AWS Lambda**.
|
||||
|
||||
Create a folder called `functions`, then create `server.js` (and your endpoint
|
||||
Create a folder called `functions`, then create `server.js` (and your endpoint
|
||||
path will be `server.js`) inside the `functions` folder.
|
||||
|
||||
### functions/server.js
|
||||
@@ -556,106 +578,27 @@ Add this command to your `package.json` *scripts*
|
||||
|
||||
```json
|
||||
"scripts": {
|
||||
...
|
||||
"build:functions": "netlify-lambda build functions --config ./webpack.config.netlify.js"
|
||||
...
|
||||
...
|
||||
"build:functions": "netlify-lambda build functions --config ./webpack.config.netlify.js"
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
Then it should work fine
|
||||
|
||||
## Platformatic Cloud
|
||||
|
||||
[Platformatic](https://platformatic.dev) provides zero-configuration deployment
|
||||
for Node.js applications.
|
||||
To use it now, you should wrap your existing Fastify application inside a
|
||||
[Platformatic Service](https://oss.platformatic.dev/docs/reference/service/introduction),
|
||||
by running the following:
|
||||
|
||||
|
||||
```bash
|
||||
npm create platformatic@latest -- service
|
||||
```
|
||||
|
||||
The wizard would ask you to fill in a few answers:
|
||||
|
||||
```
|
||||
? Where would you like to create your project? .
|
||||
? Do you want to run npm install? yes
|
||||
? Do you want to use TypeScript? no
|
||||
? What port do you want to use? 3042
|
||||
[13:04:14] INFO: Configuration file platformatic.service.json successfully created.
|
||||
[13:04:14] INFO: Environment file .env successfully created.
|
||||
[13:04:14] INFO: Plugins folder "plugins" successfully created.
|
||||
[13:04:14] INFO: Routes folder "routes" successfully created.
|
||||
? Do you want to create the github action to deploy this application to Platformatic Cloud dynamic workspace? no
|
||||
? Do you want to create the github action to deploy this application to Platformatic Cloud static workspace? no
|
||||
```
|
||||
|
||||
Then, head to [Platformatic Cloud](https://platformatic.cloud) and sign in
|
||||
with your GitHub account.
|
||||
Create your first application and a static workspace: be careful to download the
|
||||
API key as an env file, e.g. `yourworkspace.txt`.
|
||||
|
||||
Then, you can easily deploy your application with the following command:
|
||||
|
||||
```bash
|
||||
platformatic deploy --keys `yourworkspace.txt`
|
||||
```
|
||||
|
||||
Check out the [Full Guide](https://blog.platformatic.dev/how-to-migrate-a-fastify-app-to-platformatic-service)
|
||||
on how to wrap Fastify application in Platformatic.
|
||||
Then it should work fine.
|
||||
|
||||
## Vercel
|
||||
|
||||
[Vercel](https://vercel.com) provides zero-configuration deployment for Node.js
|
||||
applications. To use it now, it is as simple as configuring your `vercel.json`
|
||||
file like the following:
|
||||
[Vercel](https://vercel.com) fully supports deploying Fastify applications.
|
||||
Additionally, with Vercel's
|
||||
[Fluid compute](https://vercel.com/docs/functions/fluid-compute), you can combine
|
||||
server-like concurrency with the autoscaling properties of traditional
|
||||
serverless functions.
|
||||
|
||||
```json
|
||||
{
|
||||
"rewrites": [
|
||||
{
|
||||
"source": "/(.*)",
|
||||
"destination": "/api/serverless.js"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
Get started with the
|
||||
[Fastify template on Vercel](
|
||||
https://vercel.com/templates/backend/fastify-on-vercel).
|
||||
|
||||
Then, write `api/serverless.js` like so:
|
||||
|
||||
```js
|
||||
"use strict";
|
||||
|
||||
// Read the .env file.
|
||||
import * as dotenv from "dotenv";
|
||||
dotenv.config();
|
||||
|
||||
// Require the framework
|
||||
import Fastify from "fastify";
|
||||
|
||||
// Instantiate Fastify with some config
|
||||
const app = Fastify({
|
||||
logger: true,
|
||||
});
|
||||
|
||||
// Register your application as a normal plugin.
|
||||
app.register(import("../src/app.js"));
|
||||
|
||||
export default async (req, res) => {
|
||||
await app.ready();
|
||||
app.server.emit('request', req, res);
|
||||
}
|
||||
```
|
||||
|
||||
In `src/app.js` define the plugin.
|
||||
```js
|
||||
async function routes (fastify, options) {
|
||||
fastify.get('/', async (request, reply) => {
|
||||
return { hello: 'world' }
|
||||
})
|
||||
}
|
||||
|
||||
export default routes;
|
||||
```
|
||||
[Fluid compute](https://vercel.com/docs/functions/fluid-compute) currently
|
||||
requires an explicit opt-in. Learn more about enabling Fluid compute
|
||||
[here](
|
||||
https://vercel.com/docs/functions/fluid-compute#how-to-enable-fluid-compute).
|
||||
Reference in New Issue
Block a user