Created
December 15, 2021 05:44
-
-
Save tjmonsi/67b4f25d6353c5587ee08429e965b5d7 to your computer and use it in GitHub Desktop.
Full app.js v1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Require the framework and instantiate it | |
import fastify from 'fastify'; | |
import stat from 'fastify-static'; | |
import jwt from 'fastify-jwt'; | |
import cookie from 'fastify-cookie'; | |
import session from 'fastify-session'; | |
import openApiGlue from 'fastify-openapi-glue'; | |
import swagger from 'fastify-swagger'; | |
import sensible from 'fastify-sensible'; | |
import { Service } from './services/index.js'; | |
import { Security } from './security/index.js'; | |
import { specification } from './specifications/index.js'; | |
import { connect } from './utils/mongodb/index.js'; | |
import { join, dirname } from 'path'; | |
import { fileURLToPath } from 'url'; | |
import { readFileSync } from 'fs'; | |
const audience = 'this-audience'; | |
const issuer = 'localhost'; | |
/** | |
* This function starts the server | |
* | |
* @param {*} options | |
* @returns {*} | |
*/ | |
export async function server (options = { logger: true }) { | |
const app = fastify(options); | |
app.register(sensible); | |
app.register(stat, { | |
root: join(dirname(fileURLToPath(import.meta.url)), './public'), | |
preCompressed: true | |
}); | |
app.setNotFoundHandler((_req, res) => res.sendFile('index.html')); | |
app.register(jwt, { | |
secret: { | |
private: readFileSync('./cert/private.key', 'utf8'), | |
public: readFileSync('./cert/public.key', 'utf8') | |
}, | |
sign: { | |
algorithm: 'RS256', | |
audience, | |
issuer, | |
expiresIn: '1h' | |
}, | |
verify: { | |
audience, | |
issuer | |
} | |
}); | |
app.register(cookie); | |
app.register(session, { | |
cookieName: 'sessionToken', | |
secret: readFileSync('./cert/private.key', 'utf8'), | |
cookie: { | |
secure: 'auto', | |
httpOnly: true | |
}, | |
maxAge: 60 * 60 | |
}); | |
await connect(); | |
const service = new Service(app); | |
const securityHandlers = new Security(app); | |
const openApiOptions = { | |
specification, | |
service, | |
securityHandlers, | |
noAdditional: true | |
}; | |
const swaggerOptions = { | |
// @ts-ignore | |
openapi: specification, | |
routePrefix: '/docs', | |
exposeRoute: process.env.NODE_ENV !== 'production' | |
}; | |
app.register(swagger, swaggerOptions); | |
app.register(openApiGlue, openApiOptions); | |
return app; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment