index.js
1.39 KB
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
var express = require('express');
var config = require('./config.json');
var app = express();
var db = require('./db');
db.init({config: config});
var modelOptions = {config: config, db: db};
var storeUser = require('./store-user')
storeUser.init(modelOptions);
var trans = require('./trans');
trans.init(modelOptions);
var sales = require('./sales');
sales.init(modelOptions);
function isValidApiKey(apikey) {
return config.apikey.indexOf(apikey) >= 0;
}
function checkAuth(req, res, next) {
if (isValidApiKey(req.params.apikey)) {
next();
return;
}
res.send("Invalid auth");
}
function setOutputFormatToCsv(req, res, next) {
req.params.outputFormat = 'csv';
next();
}
app.get("/apikey/:apikey/store-user/by-msisdn/:msisdn", checkAuth, storeUser.getByMSISDN);
app.get("/apikey/:apikey/trans/by-messageid/:messageid", checkAuth, trans.getByMessageid);
app.get("/apikey/:apikey/sales/last-hour/per-chip-info/csv", checkAuth, setOutputFormatToCsv, sales.getLastHoursSalesPerChipInfo);
app.get("/apikey/:apikey/sales/last-hour/per-chip-info/:hour/csv", checkAuth, setOutputFormatToCsv, sales.getLastHoursSalesPerChipInfo);
app.get("/apikey/:apikey/sales/last-hour/per-chip-info", checkAuth, sales.getLastHoursSalesPerChipInfo);
app.get("/apikey/:apikey/sales/last-hour/per-chip-info/:hour", checkAuth, sales.getLastHoursSalesPerChipInfo);
app.listen(config.listen_port);