-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (45 loc) · 1.42 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
const apicache = require('apicache');
const compression = require('compression');
const express = require('express');
const helmet = require('helmet');
require('dotenv').config();
const bugsnag = require('./config/bugsnag');
const logger = require('./config/winston');
const loteria = require('./loteria');
const app = express();
const port = process.env.PORT || 3000;
const tempFolder = './tmp';
const version = '1.0.1;';
const cache = apicache.middleware;
app.use(compression());
app.use(helmet());
app.use(bugsnag.requestHandler);
app.get('/', (req, res) => res.json({ app: 'APILoteria', version }));
app.get('/megasena', (req, res) => {
console.log('Sending internal request to backend...');
loteria.megaSena(tempFolder)
.then((jsonArray) => {
res.status(200).json(jsonArray);
});
});
app.get('/megasena/last', cache('5 minutes'), (req, res) => {
loteria.megaSena(tempFolder)
.then((jsonArray) => {
res.status(200).json([jsonArray.slice(-1).pop()]);
});
});
app.get('/megasena/:id', (req, res) => {
loteria.megaSena(tempFolder)
.then((jsonArray) => {
res.status(200).json(jsonArray.filter(item => item.Concurso === parseInt(req.params.id, 10)));
});
});
app.get('/lotofacil', (req, res) => {
loteria.lotoFacil(tempFolder)
.then((jsonArray) => {
res.status(200).json(jsonArray);
});
});
app.listen(port, () => {
logger.info(`API Loteria listening on port ${port}`);
});