-
Notifications
You must be signed in to change notification settings - Fork 106
/
app.js
73 lines (67 loc) · 3.03 KB
/
app.js
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
import AutoLoad from '@fastify/autoload'
import Sensible from '@fastify/sensible'
import Env from '@fastify/env'
import Cors from '@fastify/cors'
import UnderPressure from '@fastify/under-pressure'
import S from 'fluent-json-schema'
import { join } from 'desm'
/**
* This is the entry point of our application. As everything in Fastify is a plugin.
* The main reason why the entry point is a plugin as well is that we can easily
* import it in our testing suite and add this application as a subcomponent
* of another Fastify application. The encapsulaton system, of Fastify will make sure
* that you are not leaking dependencies and business logic.
* For more info, see https://www.fastify.io/docs/latest/Encapsulation/
*/
export default async function (fastify, opts) {
// It's very common to pass secrets and configuration
// to your application via environment variables.
// The `fastify-env` plugin will expose those configuration
// under `fastify.config` and validate those at startup.
await fastify.register(Env, {
schema: S.object()
.prop('NODE_ENV', S.string().required())
.prop('ELASTIC_CLOUD_ID', S.string())
.prop('ELASTIC_ADDRESS', S.string())
.prop('ELASTIC_API_KEY', S.string().required())
.prop('GITHUB_APP_ID', S.string().required())
.prop('GITHUB_APP_SECRET', S.string().required())
.prop('COOKIE_SECRET', S.string().required())
.prop('ALLOWED_USERS', S.string().required())
.valueOf()
})
// Fastify is an extremely lightweight framework, it does very little for you.
// Every feature you might need, such as cookies or database coonnectors
// is provided by external plugins.
// See the list of recognized plugins by the core team! https://www.fastify.io/ecosystem/
// `fastify-sensible` adds many small utilities, such as nice http errors.
await fastify.register(Sensible)
// This plugin is especially useful if you expect an high load
// on your application, it measures the process load and returns
// a 503 if the process is undergoing too much stress.
await fastify.register(UnderPressure, {
maxEventLoopDelay: 1000,
maxHeapUsedBytes: 1000000000,
maxRssBytes: 1000000000,
maxEventLoopUtilization: 0.98
})
// Enables the use of CORS in a Fastify application.
// https://en.wikipedia.org/wiki/Cross-origin_resource_sharing
await fastify.register(Cors, {
origin: false
})
// Normally you would need to load by hand each plugin. `fastify-autoload` is an utility
// we wrote to solve this specific problems. It loads all the content from the specified
// folder, even the subfolders. Take at look at its documentation, as it's doing a lot more!
// First of all, we require all the plugins that we'll need in our application.
await fastify.register(AutoLoad, {
dir: join(import.meta.url, 'plugins'),
options: Object.assign({}, opts)
})
// Then, we'll load all of our routes.
await fastify.register(AutoLoad, {
dir: join(import.meta.url, 'routes'),
dirNameRoutePrefix: false,
options: Object.assign({}, opts)
})
}