-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
157 lines (134 loc) · 3.8 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict'
// Special thanks to @victorfern91 @hugoduraes @ludwig801 @palminha @zepcp @lcfb91 @jcazevedo @cchostak for the work and efforts to bootstrap this service!
// Feel free to change / improve / delete everything you want!
const fastify = require('fastify')({logger: false})
const path = require('path')
const AutoLoad = require('fastify-autoload')
const fsequelize = require('fastify-sequelize')
const oauthPlugin = require('fastify-oauth2')
const oas = require('fastify-oas')
const swagger = require('./config/swagger')
// store application root
global.__basedir = __dirname
fastify.register(oas, swagger.options)
const dotEnv = require('dotenv').config()
const sequelizeConfig = {
instance: 'sequelize',
autoConnect: true,
dialect: 'postgres',
timezone: 'utc',
dialectOptions: {
dateStrings: true,
typeCast: true,
},
pool: {
max: 100,
min: 1,
acquire: 30000,
idle: 10000,
},
host: process.env.DB_HOST,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
username: process.env.DB_USER,
password: process.env.DB_PASS,
logging: false
}
/*
fastify.register(
require('fastify-rate-limit'), {
max: 60,
timeWindow: '1 minute'
})
*/
fastify.register(
require('fastify-helmet'), {
hidePoweredBy: {
setTo: 'Covidografia API Server'
}
})
fastify
.register(fsequelize, sequelizeConfig)
.ready((err) => {
if (!err) {
fastify.setupModels();
} else {
console.log("ERROR registering sequelize ", err);
throw err;
}
})
const corsOptions = {
origin: process.env.CORS_ORIGIN.split(','),
methods: 'OPTIONS,GET,HEAD,PUT,PATCH,POST,DELETE',
optionsSuccessStatus: 200 // to support some legacy browsers
}
fastify.use(require('cors')(corsOptions))
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'plugins'),
//options: Object.assign({}, opts)
})
fastify.register(AutoLoad, {
dir: path.join(__dirname, 'controllers/v1'),
options: Object.assign({}, { prefix: '/api/v1' })
})
if (process.env.PRODUCTION != '1') {
fastify.register(require('fastify-static'), {
root: path.join(__dirname, 'public'),
prefix: '/public/',
});
}
fastify.register(require('fastify-axios'))
fastify.register(oauthPlugin, {
name: 'facebookOAuth2',
credentials: {
client: {
id: process.env.FB_APP_ID,
secret: process.env.FB_APP_SECRET
},
auth: oauthPlugin.FACEBOOK_CONFIGURATION
},
generateStateFunction: function(){
//return require('uuid/v4')();
return require('crypto').randomBytes(10).toString('hex')
},
checkStateFunction: function(state, callback){
callback()
},
startRedirectPath: '/login/facebook',
callbackUri: `${process.env.FB_CALLBACK_URL}/login/facebook/callback`,
scope: process.env.FB_APP_SCOPE // 'email,public_profile'
})
fastify.register(oauthPlugin, {
name: 'googleOAuth2',
credentials: {
client: {
id: process.env.GOOGLE_APP_ID,
secret: process.env.GOOGLE_APP_SECRET
},
auth: oauthPlugin.GOOGLE_CONFIGURATION
},
generateStateFunction: function(){
//return require('uuid/v4')();
return require('crypto').randomBytes(10).toString('hex')
},
checkStateFunction: function(state, callback){
callback()
},
startRedirectPath: '/login/google',
callbackUri: `${process.env.GOOGLE_CALLBACK_URL}/login/google/callback`,
scope: process.env.GOOGLE_APP_SCOPE // 'profile email openid'
})
// Support for AWS Lambda
if (process.env.LAMBDA_TASK_ROOT && process.env.AWS_EXECUTION_ENV) {
const serverless = require('serverless-http');
module.exports.handler = serverless(fastify);
} else {
fastify.listen(process.env.PORT, err => {
if (err) {
console.log(err);
process.exit(1);
}
fastify.oas();
console.log(`server listening on ${fastify.server.address().port}`);
})
}