Skip to content

Instantly share code, notes, and snippets.

@dbauszus-glx
Created April 12, 2019 10:28
Show Gist options
  • Save dbauszus-glx/0695e74db5959247201c0047e98ef852 to your computer and use it in GitHub Desktop.
Save dbauszus-glx/0695e74db5959247201c0047e98ef852 to your computer and use it in GitHub Desktop.
const fastify = require('fastify')({
trustProxy: true,
logger: {
level: process.env.LOG_LEVEL || 'error',
prettifier: require('pino-pretty'),
prettyPrint: {
errorProps: 'hint, detail',
levelFirst: true,
crlf: true
}
}
});
// Register fastify modules and routes.
fastify
.register(require('fastify-helmet'), {
contentSecurityPolicy: {
directives: {
defaultSrc: ['\'self\'', '*.logrocket.io'],
baseURI: ['\'self\''],
objectSrc: ['\'self\''],
workerSrc: ['\'self\'', 'blob:'],
frameSrc: ['\'self\'', 'www.google.com', 'www.gstatic.com'],
formAction: ['\'self\''],
styleSrc: ['\'self\'', '\'unsafe-inline\'', 'fonts.googleapis.com'],
fontSrc: ['\'self\'', 'fonts.gstatic.com'],
scriptSrc: ['\'self\'', '\'unsafe-inline\'', 'www.google.com', 'www.gstatic.com', '*.logrocket.io'],
imgSrc: ['\'self\'', '*.tile.openstreetmap.org', 'api.mapbox.com', 'res.cloudinary.com', 'raw.githubusercontent.com', 'data:']
},
setAllHeaders: true
},
// Must be set to false to allow iframe embeds.
frameguard: false,
noCache: true
})
.register(require('fastify-cors'), {
origin: true
})
.register(require('fastify-formbody'))
.register(require('fastify-static'), {
root: global.appRoot + '/public',
prefix: (process.env.DIR || '') + '/'
})
.register(require('fastify-auth'))
.decorate('login', require(global.appRoot + '/routes/login')(fastify))
.decorate('authToken', require(global.appRoot +'/mod/authToken')(fastify))
.decorate('evalParam', require(global.appRoot +'/mod/evalParam')(fastify))
.register(require('fastify-jwt'), {
secret: process.env.SECRET || 'ChinaCatSunflower'
})
.register(require('fastify-swagger'), {
routePrefix: global.dir + '/swagger',
exposeRoute: true,
})
.addContentTypeParser('*', (req, done) => done())
.register((fastify, opts, next) => {
require('./routes/_routes')(fastify);
next();
}, { prefix: global.dir });
fastify.listen(process.env.PORT || 3000, '0.0.0.0', err => {
if (err) process.exit(1);
fastify.swagger();
console.log(`Fastify listening.`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment