-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
86 lines (79 loc) · 2.35 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
74
75
76
77
78
79
80
81
82
83
84
85
86
const Ajv = require('ajv')
const Fastify = require('fastify')
const routes = require('./routes')
const stripeRoutes = require('./routes/stripe')
const { authPlugin } = require('./auth')
const { dbPlugin } = require('./db')
const { sqsPlugin } = require('./sqs')
const { s3Plugin } = require('./s3')
const { configPlugin } = require('./config')
const { stripePlugin } = require('./stripe')
const { registryPlugin } = require('./registry')
const { emailPlugin } = require('./email')
const { urlPlugin } = require('./url')
const { githubPlugin } = require('./github')
const { ethicalAdsPlugin } = require('./ethicalAds')
module.exports = async function buildFastify (deps) {
const {
db,
auth,
sqs,
s3,
email,
registry,
stripe,
url,
config,
github,
ethicalAds,
logger = true,
csrf = true
} = deps
const fastify = Fastify({ logger })
// Create custom ajv schema declaration to remove _all_ additional fields by default
const ajv = new Ajv({
removeAdditional: 'all',
useDefaults: true,
coerceTypes: true,
allErrors: true,
nullable: true
})
fastify.setSchemaCompiler(function (schema) {
return ajv.compile(schema)
})
fastify.register(require('fastify-helmet'))
fastify.register(require('fastify-cookie'))
fastify.register(stripeRoutes)
const allowedOrigins = [
'https://flossbank.com',
'https://advertiser.flossbank.com',
'https://maintainer.flossbank.com',
'https://user.flossbank.com'
]
if (process.env.NODE_ENV !== 'production') {
allowedOrigins.push(
'http://localhost:3000',
/\.flossbank\.vercel\.app$/,
/\.flossbank\.now\.sh$/
)
}
fastify.register(require('fastify-cors'), {
origin: allowedOrigins,
methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Authorization', 'Content-Type', 'X-Requested-With'],
credentials: true
})
fastify.register(routes, { csrf })
fastify.register(dbPlugin(db))
fastify.register(authPlugin(auth))
fastify.register(emailPlugin(email))
fastify.register(stripePlugin(stripe))
fastify.register(sqsPlugin(sqs))
fastify.register(s3Plugin(s3))
fastify.register(registryPlugin(registry))
fastify.register(urlPlugin(url))
fastify.register(configPlugin(config))
fastify.register(githubPlugin(github))
fastify.register(ethicalAdsPlugin(ethicalAds))
return fastify
}