-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroutes.js
More file actions
164 lines (149 loc) · 4.83 KB
/
routes.js
File metadata and controls
164 lines (149 loc) · 4.83 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var strings = require("./strings.js");
//require filesystem for writing state
var fs = require("fs");
//keep track of state in memory
var popCount = [{ machine: "A", count: "0" }, { machine: "B", count: "0" }];
//import auth token
var AUTH_TOKEN = process.env.API_AUTH_TOKEN;
//import slack
var SLACK_WEBHOOK = process.env.SLACK_WEBHOOK;
var slack = require("slack-notify")(SLACK_WEBHOOK);
//slack shortcut function
function slackmsg(msg) {
slack.send({
text: msg
});
}
function testme() {
slack.send({
text: "Current server stats",
fields: {
"CPU usage": "7.51%",
"Memory usage": "254mb"
}
});
}
// testme();
function saysomething(phrasetype, machine = "", count = 0) {
machine == "A" ? (machine = "Sir Orville") : (machine = "Sir Vic");
switch (phrasetype) {
case "demand":
var greeting =
strings.cornGreetings[
Math.floor(Math.random() * strings.cornGreetings.length)
];
var demand = strings.cornDemands[
Math.floor(Math.random() * strings.cornDemands.length)
]
.replace("$machine", machine)
.replace("greeting", greeting);
slackmsg(demand + " _(" + count + " requests)_");
break;
case "proclaim":
var fact =
strings.cornFacts[Math.floor(Math.random() * strings.cornFacts.length)];
slackmsg("My humble subjects, I bestow upon you my wisdom: " + fact);
break;
case "entertain":
var vid =
strings.ytVideos[Math.floor(Math.random() * strings.ytVideos.length)];
slackmsg("Jester brings entertainment! " + vid);
break;
case "thank":
var thanks = strings.cornThanks[
Math.floor(Math.random() * strings.cornThanks.length)
]
.replace("$machine", machine)
.replace("greeting", greeting);
slackmsg(thanks);
break;
case "stats":
slack.send({
text: "Current popcorn stats",
fields: {
"Sir Orville": popCount["A"]+" requests",
"Sir Vic": popCount["B"]+" requests"
}
});
break;
}
}
//loads states from filesystem into memory when the project restarts
function loadFileData() {
//Assuming machines are "A" and "B", we should make defs for these
fs.readFile(".data/A.json", function(err, data) {
if (err) return console.log(err);
var result = JSON.parse(data);
popCount[result.machine] = result.count;
});
fs.readFile(".data/B.json", function(err, data) {
if (err) return console.log(err);
var result = JSON.parse(data);
popCount[result.machine] = result.count;
});
}
loadFileData();
var routes = function(app) {
//
// defaut response to somebody who goes to popcornapi.gitch.me
//
app.get("/", function(req, res) {
res.send("<h1>popcorn!</h1>");
console.log("Received GET");
});
//
// POST request to /popcorn will update everything. this is where danger be
//
app.post("/popcorn", function(req, res) {
//we need to do input validation on req.body so somebody doesn't hax us
if (!req.body.machine || !req.body.count || req.body.auth != AUTH_TOKEN) {
console.log("Received incomplete POST: " + JSON.stringify(req.body));
return res.send({ status: "error", message: "missing parameter(s)" });
} else {
console.log("Received POST: " + JSON.stringify(req.body));
popCount[req.body.machine] = req.body.count;
fs.writeFileSync(
".data/" + req.body.machine + ".json",
JSON.stringify(req.body)
);
saysomething("demand", req.body.machine, req.body.count);
return res.send(req.body);
}
});
app.post("/popcorn/thanks", function(req, res) {
//we need to do input validation on req.body so somebody doesn't hax us
if (!req.body.machine || req.body.auth != AUTH_TOKEN) {
console.log("Received incomplete POST: " + JSON.stringify(req.body));
return res.send({ status: "error", message: "missing parameter(s)" });
} else {
console.log("Received POST: " + JSON.stringify(req.body));
saysomething("thank", req.body.machine);
return res.send(req.body);
}
});
//
// GET requests return the current state. no auth required. this is used when the machines boot and for dashboards
// can probably combine these if I get smart
app.post("/popcorn/stats", function(req, res) {
saysomething("stats");
console.log("Received GET: " + JSON.stringify(req.body));
return res.send("");
});
app.get("/popcorn/A", function(req, res) {
var dummyData = {
machine: "A",
count: popCount["A"]
};
console.log("Received GET: " + JSON.stringify(req.body));
return res.send(dummyData);
});
app.get("/popcorn/B", function(req, res) {
var dummyData = {
machine: "B",
count: popCount["B"]
};
console.log("Received GET: " + JSON.stringify(req.body));
return res.send(dummyData);
});
};
module.exports = routes;