-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
92 lines (77 loc) · 2.11 KB
/
index.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
const express = require("express");
const app = express();
const server = require("http").Server(app);
// Shared GUN scope for ROOM management only (no signaling here)
var Gun = require("gun");
require("gun/lib/open.js");
require("gun/lib/not.js");
var gun = Gun({ peers: ["https://gundb-multiserver.glitch.me/openhouse"],
multicast: false,
localStorage: false,
radisk: false,
file: false
});
// GUN Rooms object - this is not persisting.....
var gunRooms = gun.get("rooms");
function resyncRooms(){
gunRooms.open(function(data){
rooms = clean(data);
//console.log('room data resync', rooms);
})
}
// Force Provision the lobby?
//gunRooms.get('lobby').put({ title: 'Lobby', id: 'lobby', locked: false, owner: 'openhouse-admin', count: 99 });
resyncRooms();
const bodyParser = require("body-parser");
const { v4: uuidv4 } = require("uuid");
// Helper
function clean(obj) {
for (var propName in obj) {
if (obj[propName] === null || obj[propName] === undefined || propName == "_" ) {
delete obj[propName];
}
}
return obj;
}
var env = {};
var rooms = {};
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json({ type: "application/json" }));
app.use("/favicon.ico", express.static("favicon.ico"));
app.get("/", async (req, res) => {
resyncRooms();
res.redirect('/rooms')
});
app.get("/r/:id", (req, res) => {
// replace with gun check
if (!rooms[req.params.id]) {
resyncRooms();
console.log('missing room',req.params.id, rooms);
res.redirect('/rooms');
//res.render("rooms", { rooms: rooms });
return;
}
res.render("room", {
room: rooms[req.params.id],
peerjs: {}
});
});
app.post("/rooms", (req, res) => {
var room = {
id: uuidv4(),
title: req.body.title,
peers: {},
locked: req.body.locked
};
rooms[room.id] = room;
res.json(room);
});
// GUN Rooms
app.use("/rooms", express.static(__dirname + "/views/rooms.html"));
// NOT FOUND
app.get("*", function(req, res) {
res.redirect('/rooms')
//res.render("404");
});
server.listen(process.env.PORT || 3000);