-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
114 lines (95 loc) · 3.53 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const app = require('fastify')({ logger: true, prettyPrint: { colorize: true, ignore: 'pid,hostname'}, ajv: { customOptions: {coerceTypes: true}}})
const corsFatify = require('@fastify/cors')
const envFastify = require('@fastify/env')
const dbOptions = require('./src/models/dbSchema')
/*
*Setting Up Server
*/
const start = async () => {
try {
//Registering @cors
await app.register(corsFatify, {
origin: '0.0.0.0',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
exposedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
maxAge: 3600
})
app.log.info("Cors Registered")
//Registering @env envoirment variables
await app.register(envFastify, dbOptions);
const dbUserName = encodeURIComponent(app.config.DB_USERNAME);
const dbPassword = encodeURIComponent(app.config.DB_PASSWORD);
const dbName = encodeURIComponent(app.config.DB_NAME);
const dbUrl = `mongodb+srv://${dbUserName}:${dbPassword}@cluster0.uvzqwgp.mongodb.net/${dbName}?retryWrites=true&w=majority`;
app.log.info("Dotenv Configured")
//Registering and connecting to MongoDB
app.register(require('@fastify/mongodb'), {
url: dbUrl,
forceClose: true,
useUnifiedTopology: true,
}).after(() => {
const db = app.mongo.db;
const usersCollection = db.collection('users');
usersCollection.createIndex({ username: 1 }, { unique: true })});
//Debugging DB connection
app.log.info("MongoDB Connected")
/*
*Registering Secure Session
*/
//Registering @Fastify/JWT
app.register(require('@fastify/jwt'), {
secret: encodeURIComponent(app.config.SECRET),
sign: { expiresIn: '2h' }
})
app.log.info("JWT decoding token and verifying signature")
//Registering @Fastify/Auth
app.register(require('@fastify/auth')).after(() => {
app.decorate('authenticate', async function (request, reply) {
await request.jwtVerify();
})});
app.log.info("Routes protected with JWT and Auth")
/*
*Registering Routes
*/
//Root route
app.get('/', async (request, reply) => {
reply.send({ Server_Status: 'Running' })
});
/*
*Documentation
*/
//Registering @Fastify/Swagger
app.register(require('@fastify/swagger'), {})
app.register(require('@fastify/swagger-ui'), {
routePrefix: '/api/v1/documentation',
swagger: {
info: {
title: 'Job Tracker API',
description: 'API for Job Tracker',
version: '0.1.0'
},
schemes: ['http', 'https'],
consumes: ['application/json'],
produces: ['application/json'],
// Definir o método HTTP como GET para a rota do Swagger
methods: ['GET']
},
exposeRoute: true
})
app.log.info("Swagger Documentation configured")
//Routes
app.register(require('./src/routes/register') , { prefix: '/api/v1' }); //Signup
app.register(require('./src/routes/login') , { prefix: '/api/v1' }); //Login
app.register(require('./src/routes/user') , { prefix: '/api/v1' }); //User
app.register(require("./src/routes/job") , { prefix: '/api/v1' }); //Job
app.register(require('./src/routes/recruiter') , { prefix: '/api/v1' }); //Recruiter'))
//Running Server
await app.listen({port:3000})
}catch (err) {
app.log.error(err)
process.exit(1)
}
}
start()