index.js 1.39 KB
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);