-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
134 lines (115 loc) · 4.85 KB
/
app.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
function heartbeat() {
try {
var colors = require("colors");
var fs = require("fs");
var app = require("express")();
var http = require("http").Server(app);
var config = require("./config.js");
var io, socket_end_point;
if (config.production) {
io = require("socket.io")(http, { path: '/socket.io' });
config.socket = { path: '/healthy/socket.io' };
} else {
io = require("socket.io")(http);
config.socket = {};
}
var express = require("express");
var convert = require("./modules/convert.js");
var branch = "master";
var git = require('git-rev');
app.use(require("express").static('public'));
app.get("/", function(req, res) {
console.log("app.js - " + "[SubjectRefresh - ".green + "Healthy".blue + "]".green + " A user connected".blue);
fs.readFile("pages/index.html", "utf-8", function(err, data) {
res.send(data);
});
});
app.get("/get_config", function(req, res){
res.json(config);
});
app.get("/privacy-policy", function(req, res) {
console.log("app.js - " + "[SubjectRefresh - ".green + "Healthy".blue + "]".green + " A user requested the privacy policy".blue);
fs.readFile("pages/privacy-policy.html", "utf-8", function(err, data) {
res.send(data);
});
});
io.on("connection", function(socket) {
socket.on("getFoodSuggestions", function(packet) {
console.log("app.js - " + "[SubjectRefresh - ".green + "Healthy".blue + "]".green + " A user requested suggestions for ".blue + (packet.food).green);
convert.getNDB(packet.food, function(suggestions) {
io.sockets.connected[socket.id].emit("suggestions", { suggestions: suggestions });
});
});
socket.on("getFood", function(packet) {
convert.getCalorie(packet.food, function(calorie) {
convert.energyBurnt(packet.age, packet.weight, "running", 60, packet.gender, function(runningSpeed) {
convert.energyBurnt(packet.age, packet.weight, "walking", 60, packet.gender, function(walkingSpeed) {
convert.energyBurnt(packet.age, packet.weight, "swimming", 60, packet.gender, function(swimmingSpeed) {
io.sockets.connected[socket.id].emit("receiveCalories", {
running: calorie / runningSpeed,
walking: calorie / walkingSpeed,
swimming: calorie / swimmingSpeed
});
});
});
});
});
});
});
app.post("/update-git", function(req, res) {
console.log("Reloading git repo");
updateGit();
res.send({
message: "OK",
code: 200
})
});
function run_cmd(cmd, args, cb, end) {
var spawn = require('child_process').spawn,
child = spawn(cmd, args),
me = this;
child.stdout.on('data', function(buffer) {
cb(me, buffer)
});
child.stdout.on('end', end);
}
function gitFetch(callback) {
var git_fetch_output = new run_cmd(
'git', ['fetch', '--all'],
function(me, buffer) {
me.stdout += buffer.toString()
},
function() {
callback(git_fetch_output.stdout);
}
);
}
function gitReset(callback) {
var git_reset_output = new run_cmd(
'git', ['reset', '--hard', 'origin/' + branch],
function(me, buffer) {
me.stdout += buffer.toString()
},
function() {
callback(git_reset_output.stdout);
}
);
}
function updateGit() {
gitFetch(function(output) {
console.log("app.js - " + output.green);
gitReset(function(output) {
console.log("app.js - " + output.green);
});
});
}
http.listen(3001, function() {
console.log("app.js - " + "[SubjectRefresh - ".green + "Healthy".blue + "] Running at ".green + "http://localhost:3001".blue);
});
} catch (e) {
console.log("app.js - " + "[SubjectRefresh - ".green + "Healthy".blue + "]".green + " FATAL ERROR".red);
console.log(e);
heartbeat();
}
}
heartbeat();