-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (117 loc) · 4.51 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
// index.js
// =============================================================================
// BASE SETUP
// =============================================================================
// Call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser'); // used to retrieve post params
var morgan = require('morgan'); // used to log received requests
var jwt = require('jsonwebtoken'); // JsonWebToken for session validation
var cookieParser = require('cookie-parser');// simple cookie parser for Express
var path = __dirname + '/public/'; // public path
// To pass original headers through NGINX
app.set('trust proxy', 'loopback');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Use cookieParser
app.use(cookieParser());
// use Morgan to log requests to the console
app.use(morgan('dev')); // 'dev' for development / 'short' for production
var port = 8083; // set our port
// =============================================================================
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// =============================Login User Data=================================
app.use(function (req, res, next) {
// Remover la siguient elinea cuando se vata prod
try {
var jwt_token = req.cookies["access-token"];
var jwt_token = jwt_token || req.headers["access-token"];
req.decoded = jwt.verify(jwt_token, process.env.JWT_SECRET);
next();
} catch(err) {
console.log(req.headers);
res.redirect(302, '/users/login');
}
});
// ==========================Arquicoins Cassandra===============================
var Arquicoins = require('./arquicoins/arquicoins/arquicoinsCtrl.js');
var Users = require('./arquicoins/arquicoins/usersCtrl.js');
router.get('/data/arquicoins', function(req, res){
function callback(err, result) {
if (err) {
res.status(400);
res.json(err);
}
else { res.json(result); }
}
Arquicoins.getArquicoins(req.decoded._doc.username, callback)
});
router.post('/data/arquicoins/buy', function(req, res){
function callback(err, result) {
if (err) {
res.status(400);
res.json(err);
}
else { res.json(result); }
}
Arquicoins.buyArquicoins(req.decoded._doc.username, req.decoded._doc.firstname, req.decoded._doc.lastname, req.body.amount, callback);
});
router.post('/data/arquicoins/transfer', function(req, res){
function callback(err, result) {
if (err) {
res.status(400);
res.json(err);
}
else { res.json(result); }
}
Arquicoins.transferArquicoins(req.decoded._doc.username, req.body.toUsername, req.body.amount, callback);
});
router.get('/data/paymentinfo', function(req, res){
function callback(err, result) {
if (err) {
res.status(400);
res.json(err);
}
else { res.json(result); }
}
Users.getPaymentInfo(req.decoded._doc.username, callback)
});
router.patch('/data/paymentinfo', function(req, res){
function callback(err, result) {
if (err) {
res.status(400);
res.json(err);
}
else { res.json(result); }
}
var paymentInfo = {
accountType: req.body.accountType,
creditNumber: req.body.creditNumber,
csvNumber: req.body.csvNumber
};
Users.updatePaymentInfo(req.decoded._doc.username, paymentInfo, callback)
});
// =========================Public Folder=======================================
app.use('/arquicoins', express.static('public'));
// =============================================================================
// Register Routes
// =============================================================================
app.use('/arquicoins', router);
// Catch 404
app.use("*",function(req,res){
res.status(404);
res.sendFile(path + "404.html");
});
// =============================================================================
// START THE SERVER
// =============================================================================
app.listen(port);
var currentDate = new Date();
currentDate.setTime(Date.now());
dateString = currentDate.toUTCString();
console.log(dateString + ' - Server running at port ' + port);