This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
102 lines (85 loc) · 2.96 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
var r = require("rethinkdb");
var pubnub = require("pubnub");
var express = require("express");
var bodyParser = require("body-parser");
var stylus = require("stylus");
var jwt = require("express-jwt");
require("rethinkdb-init")(r);
var auth = require("./auth");
var config = require("./config");
var pn = pubnub(config.pubnub);
pn.grant({
write: true, read: false,
callback: function(c) { console.log("Permission set:", c); }
});
var app = express();
app.use(bodyParser.json());
app.use(stylus.middleware(__dirname + "/public"));
app.use(express.static(__dirname + "/public"));
app.listen(config.port, function() {
console.log("Server started on port " + config.port);
});
function validStr(s) {
return typeof s === "string" && s.trim();
}
r.init(config.database, [
{name: "users", indexes: ["username"]},
"updates"
])
.then(function(conn) {
return r.table("updates").changes()("new_val").run(conn);
})
.then(function(changes) {
changes.each(function(err, item) {
console.log("Received:", item);
pn.publish({channel: "updates", message: item,
error: function(err) { console.log("Failure:" , err); }});
});
});
function authHandler(authfn) {
return function(req, res) {
if (!(validStr(req.body.username) && validStr(req.body.password)))
return res.status(400).json({success: false,
error: "Must provide username and password"});
authfn(req.body.username, req.body.password).then(function(acct) {
pn.grant({
channel: "updates", auth_key: acct.token,
read: true, write: acct.user.admin,
callback: function(c) { console.log("Set permissions:", c); }
});
res.json({success: true, token: acct.token, user: acct.user});
})
.catch(function(err) {
console.log(err);
res.status(400).json({success: false, error: err});
});
};
}
app.post("/api/user/create", authHandler(auth.create));
app.post("/api/user/login", authHandler(auth.login));
app.use(jwt({secret: config.jwt.secret, credentialsRequired: false}));
app.post("/api/send", function(req, res) {
if (!req.user.admin)
return res.status(401).json({success: false, error: "Unauthorized User"});
if (!validStr(req.body.message))
return res.status(400).json({success: false,
error: "Must include a message to send"});
r.connect(config.database).then(function(conn) {
return r.table("updates").insert({
text: req.body.message,
sender: req.user.username,
time: r.now()
}).run(conn).finally(function() { conn.close(); });
})
.then(function() { res.json({success: true}); });
});
app.get("/api/history", function(req, res) {
if (!req.user)
return res.status(401).json({success: false, error: "Unauthorized User"});
r.connect(config.database).then(function(conn) {
return r.table("updates").orderBy(r.desc("time")).run(conn)
.finally(function() { conn.close(); });
})
.then(function(stream) { return stream.toArray(); })
.then(function(output) { res.json(output); });
});