This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
205 lines (176 loc) · 5.01 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const bodyParser = require('koa-bodyparser');
const enforceHttps = require('koa-sslify');
const KeyGrip = require('keygrip');
const Koa = require('koa');
const Router = require('koa-router');
const session = require('koa-session');
const views = require('koa-views');
const winston = require('winston');
const config = require('./src/infrastructure/config');
const helperList = require('./src/helpers/view-list');
const redirectHelper = require('./src/helpers/redirect');
const md5 = require('./src/helpers/md5');
const app = new Koa();
const router = new Router();
if (process.env.NODE_ENV !== 'production') {
require('dotenv').load();
}
//Adicionando suporte a sessão
winston.log('info', 'INICIALIZANDO SESSION', {
key: 'initilize'
});
app.keys = new KeyGrip([process.env.APP_KEY1, process.env.APP_KEY2], 'sha256');
app.use(session(config.SESSION, app));
if (process.env.SSL_ONLY === '1') {
app.use(enforceHttps({
trustProtoHeader: true
}))
}
//Adicionando suporte ao body-parser
app.use(bodyParser());
//Adicionando suporte a views com o handlebars
winston.log('info', 'INICIALIZANDO SUPORTE A VIEWS', {
key: 'initilize'
});
app.use(views(__dirname + config.DIR_VIEWS, {
map: {
hbs: 'handlebars'
},
options: {
helpers: {
list: helperList.list
}
}
}));
router.get('/', ctx => {
const eventService = require('./src/services/event');
return new Promise((resolve, reject) => {
eventService
.getAll()
.then(result => {
ctx.state = {
events: result.map(e => {
return {
name: e.name,
url: process.env.BASE_URL + 'event/' + e.id.toString()
}
})
};
resolve(ctx.render('./index.hbs'));
})
.catch(error => {
console.log(error);
reject(error)
});
});
})
router.get('/event/:id', ctx => {
return new Promise((resolve, reject) => {
const eventService = require('./src/services/event');
eventService
.getOne(parseInt(ctx.params.id))
.then(event => {
winston.log('info', 'Evento selecionado', {
key: 'event_selected',
event: event
});
ctx.cookies.set('event', JSON.stringify(event), { signed: true });
if (event.type === 'email') {
return resolve(ctx.render('./email.hbs', {
eventId: event.id
}));
}
if (event.type === 'meetup') {
return resolve(ctx.redirect('/authorize'));
}
})
.catch(error => {
winston.log('error', 'Evento selecionado', {
key: 'event_selected',
error: error
});
reject(ctx.redirect('/error'));
});
});
});
router.post('/event/access', async ctx => {
try {
winston.log('info', 'Evento selecionado', {
key: 'event_access'
});
const event = JSON.parse(ctx.cookies.get('event'));
const url = event.url + md5(ctx.request.body.email) + '.pdf';
winston.log('info', 'Event access', {
key: 'event_access',
url: url,
event_url: ctx.session.event_url
});
return redirectHelper.redirect(ctx, url);
} catch (e) {
winston.log('error', 'Erro ao acessar evento', {
key: 'event_selected',
error: e
})
return ctx.redirect('/error')
}
})
router.get('/authorize', async ctx => {
const meetupService = require('./src/services/meetup');
winston.log('info', 'Redirect realizado', {
key: 'event_authorize',
event_url: ctx.session.event_url
});
return ctx.redirect(
meetupService.getRedirectURL(
config.MEETUP_OAUTH_URL,
process.env.MEETUP_KEY,
process.env.AUTH_REDIRECT_URI
)
);
})
router.get('/process', async ctx => {
try {
const meetupService = require('./src/services/meetup');
winston.log('info', 'Requisição de dados para o Meetup', {
key: 'event_process',
code: ctx.query.code
});
const accessToken = await meetupService.getAccessToken(
process.env.MEETUP_KEY,
process.env.MEETUP_SECRET,
process.env.AUTH_REDIRECT_URI,
ctx.query.code,
config.MEETUP_OAUTH_URL
);
const member = await meetupService.getMember(
config.MEETUP_API_URL,
accessToken
);
const event = JSON.parse(ctx.cookies.get('event'));
const eventUrl = event.url + md5(member.data.id.toString()) + '.pdf';
winston.log('info', 'URL de certificado criada', {
key: 'event_process',
urlCertificado: eventUrl,
memberId: member.data.id.toString()
});
return redirectHelper.redirect(ctx, eventUrl);
} catch (e) {
winston.log('error', 'Erro ao processar acesso', {
key: 'event_process',
error: e
});
return ctx.redirect('/error')
}
})
router.get('/error', async ctx => {
ctx.body = "Some error happen"
ctx.status = 500;
})
router.get('/certificate-not-found', async ctx => {
return ctx.render('./certificate-not-found.hbs')
})
app.use(router.routes())
winston.log('info', 'APLICAÇÃO AGUARDANDO REQUISIÇÕES', {
key: 'initilize'
});
app.listen(process.env.PORT || 3000)