-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·132 lines (116 loc) · 2.71 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
const express = require('express')
const pg = require('pg')
require('dotenv').config();
const app = express();
const rateLimiter = require('./rateLimiter');
/*
1. use rate limiter
2. setup user header for redis
3. setup process.env for database
4. connect to database with pg.pool
5. setup other endpoints to get data from postgres
*/
app.use(rateLimiter);
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('user', 'Nana');
next();
});
const pool = new pg.Pool({
user: process.env.PGUSER,
host: process.env.PGHOST,
database: process.env.PGDATABASE,
password: process.env.PGPASSWORD,
port: process.env.PGPORT
});
const queryHandler = (req, res, next) => {
pool.query(req.sqlQuery).then((r) => {
return res.json(r.rows || [])
}).catch(next)
}
app.get('/', (req, res) => {
res.send('Welcome to EQ Works 😎')
})
app.get('/events', (req, res, next) => {
req.sqlQuery = `
SELECT *
FROM public.hourly_events
ORDER BY date, hour
LIMIT 168;
`
return next()
}, queryHandler)
app.get('/events/hourly', (req, res, next) => {
req.sqlQuery = `
SELECT date, hour, events
FROM public.hourly_events
ORDER BY date, hour
LIMIT 168;
`
return next()
}, queryHandler);
app.get('/events/daily', (req, res, next) => {
req.sqlQuery = `
SELECT date, SUM(events) AS events
FROM public.hourly_events
GROUP BY date
ORDER BY date
LIMIT 7;
`
return next()
}, queryHandler)
app.get('/stats', (req, res, next) => {
req.sqlQuery = `
SELECT *
FROM public.hourly_stats
ORDER BY date, hour
LIMIT 168;
`
return next()
}, queryHandler)
app.get('/stats/hourly', (req, res, next) => {
req.sqlQuery = `
SELECT date, hour, impressions, clicks, revenue
FROM public.hourly_stats
ORDER BY date, hour
LIMIT 168;
`
return next()
}, queryHandler)
app.get('/stats/daily', (req, res, next) => {
req.sqlQuery = `
SELECT date,
SUM(impressions) AS impressions,
SUM(clicks) AS clicks,
SUM(revenue) AS revenue
FROM public.hourly_stats
GROUP BY date
ORDER BY date
LIMIT 7;
`
return next()
}, queryHandler)
app.get('/poi', (req, res, next) => {
req.sqlQuery = `
SELECT *
FROM public.poi;
`
return next()
}, queryHandler)
app.listen(process.env.PORT || 5555, (err) => {
if (err) {
console.error(err)
process.exit(1)
} else {
console.log(`Running on ${process.env.PORT || 5555}`)
}
})
// last resorts
process.on('uncaughtException', (err) => {
console.log(`Caught exception: ${err}`)
process.exit(1)
})
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason)
process.exit(1)
})