-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
138 lines (114 loc) · 3.96 KB
/
Copy pathapp.js
File metadata and controls
138 lines (114 loc) · 3.96 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
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
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var Item = require('./models/menu.js').Item;
var Deal= require('./models/deal.js').Deal;
var CoinList= require('./models/coin.js').CoinList;
var CurrencyList= require('./models/currencies.js').CurrencyList;
var GlobalData= require('./models/global.js').GlobalData;
var Language = require('./models/languages.js').Language;
var passport = require('passport');
var request = require("request");
var redis = require('redis');
var helper = require('./helpers/helper.js');
// Create Redis Client that can be used globally
global.client = redis.createClient();
client.on('error', function(err){
console.log('Something went wrong ', err)
});
mongoose.connect('mongodb://localhost/admin', {
keepAlive: true,
reconnectTries: Number.MAX_VALUE,
useMongoClient: true
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Database connection activated...');
});
require('./config/passport');
var routesApi = require('./routes/index');
var app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(bodyParser.json({type: 'application/json'}));
app.use('/', routesApi);
var port = 5000;
app.listen(port,function(err) {
console.log('Running server on port '+ port);
});
// BACKEND PROCESS NEEEDED
var setCoinList = function() {
request('https://api.coinmarketcap.com/v1/ticker/?limit=0', function (error, response, body) {
var coinlist1 = new CoinList({ _id:'1' ,coinList: body});
CoinList.count({}, function(err, count){
if(count==0){
coinlist1.save(function(err,coinlist) {
console.log('Coin List has been saved');
});
}
else if(count==1 && body != null){
helper.addCoinListToReddis(JSON.stringify(body));
CoinList.update({ _id: '1' }, { $set: { coinList: body }}).exec();
console.log('Coin List has been updated');
}
});
});
};
// Set Currency List
var setCurrencyList = function() {
// Currencies to show
var currencyparams = 'CAD,AUD,BGN,BRL,CHF,CNY,CZK,DKK,GBP,HRK,HKD,HUF,IDR,ILS,INR,JPY,KRW,MXN,MYR,NOK,NZD,PHP,PLN,RON,RUB,SEK,SGD,THB,TRY,ZAR,EUR'
request('http://data.fixer.io/api/latest?access_key=7570ce4d97b1166c5fb5e423d9e3dd1c&format=1&symbols='+currencyparams, function (error, response, body) {
var currencyList= new CurrencyList({ _id:'1' ,currencyList: body});
CurrencyList.count({}, function(err, count){
if(count==0){
currencyList.save(function(err,currencyList) {
console.log('Currency List has been saved' );
});
}
else if(count==1 && body != null){
CurrencyList.update({ _id: '1' }, { $set: { currenctList: body }}).exec();
console.log('Currency List has been updated' );
}
});
});
};
// Set Global Data
var setGlobalData = function() {
request('https://api.coinmarketcap.com/v2/global/', function (error, response, body) {
var globaldata1 = new GlobalData({ _id:'1' ,globalData: body});
GlobalData.count({}, function(err, count){
if(count==0){
globaldata1.save(function(err,globaldata) {
console.log('Global data has been saved');
});
}
else if(count==1 && body != null){
helper.addGlobalDataToReddis(JSON.stringify(body));
GlobalData.update({ _id: '1' }, { $set: { globalData: body }}).exec();
console.log('Global Data has been updated');
}
});
});
};
var setLanguages= function (languages){
languages.map(function(n){
var language = new Language({symbol: n});
language.save(function(err,language) {
console.log('language has been saved'+ language);
});
});
};
// Initialize currency list and coin list
setCoinList();
setCurrencyList();
setGlobalData();
//create languages english urdu and french for the app
setLanguages(['en','ur','fr']);
setInterval(setCoinList,240000);
setInterval(setCurrencyList,18000000);
setInterval(setGlobalData,86400000);