-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
143 lines (116 loc) · 4.43 KB
/
server.js
File metadata and controls
143 lines (116 loc) · 4.43 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
137
138
139
140
141
142
143
var express = require('express'),
path = require('path'),
config = require('./config.js'),
routes = require('./server/routes'),
passport = require("passport"),
pingHost = require("./server/helpers/pingHost.js"),
port = process.env.PORT || 5000,
app = express(),
flash = require("connect-flash"),
allowCrossDomain = require('./server/middleware/generalMiddleware.js')().allowCrossDomain;
// Log process.env.NODE_ENV
console.log("****************************");
console.log("* Current ENV:", app.get('env'));
console.log("****************************");
// Initialize database models
var ORM = require("./server/models/initModel.js")(app),
Models = ORM.Models,
schema = ORM.schema;
// Initialize passport
require('./server/config/passport')(passport, config, Models);
// Configure Express server
app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/server/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: 'keyboard cat' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.methodOverride());
app.use(flash());
app.use(allowCrossDomain);
app.use(app.router);
app.use(express.static(path.join(__dirname, 'client')));
});
// Initialize routing
routes.frontend(app, passport, Models, schema);
// TODO: add api routes
// If in development, use Express error handler
if (' development' == app.get(' env')){
app.use( express.errorHandler());
}
app.use(function(err, req, res, next){
console.log("ERROR:",err);
res.status(err.status || 500);
res.render('500', { error: err });
});
app.use(function(req, res, next){
res.status(404);
if (req.accepts('html')) {
res.render('404', { url: req.url });
return;
}
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
res.type('txt').send('Not found');
});
app.listen(port, function(){
// make database query to make database connection alive
var request = require('request');
var httpReqToPing = function(){
request('http://datsy-dev.azurewebsites.net/search/tag', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("azure database is alive"); // Print the google web page.
} else {
console.log('ERROR: azure database is DEAD!!!',response.statusCode);
}
});
};
var httpReqToPing2 = function(){
request('http://datsy-dev.azurewebsites.net/search/table?name=tonys_fitbit_data', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("azure database is alive:search/table"); // Print the google web page.
} else {
console.log('ERROR: azure database is DEAD!!!:search/table',response.statusCode);
}
});
};
var httpReqToPing3 = function(){
request('http://datsy-dev.azurewebsites.net/search/meta', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("azure database is alive:search/table"); // Print the google web page.
} else {
console.log('ERROR: azure database is DEAD!!!:search/table',response.statusCode);
}
});
};
var httpReqToPing4 = function(){
request('http://datsy-dev.azurewebsites.net/search/tag?tag=san+francisco', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("azure database is alive:search/tag?tag=san+francisco"); // Print the google web page.
} else {
console.log('ERROR: azure database is DEAD!!!:search/tag?tag=san+francisco',response.statusCode);
}
});
};
// var useDatabaseReqToPing = function(){
// var User = Models.User;
// User.findOne({where: {id: 1}},
// function (err, result) {
// var msg = err ? 'ERROR: azure database is DEAD!!!' + err : 'azure database is alive';
// console.log(msg);
// });
// };
setInterval(httpReqToPing, 10000);
setInterval(httpReqToPing2, 20000);
setInterval(httpReqToPing3, 20000);
setInterval(httpReqToPing4, 40000);
//ping virtual macihne not working at this time
//pingHost([config[app.get('env')].database.host]);
});