-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
165 lines (153 loc) · 5.49 KB
/
main.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
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
165
const fs = require("fs");
const path = require("path");
const { Pool } = require("pg");
require("dotenv").config();
const render_route = require("./render_route.js");
const error_page = require("./error_page.js");
const create_page = require("./create_page.js");
var db = {};
var autosave = {};
const isProduction = process.env.NODE_ENV === "production";
const connectionString = `postgresql://${process.env.PG_USER}:${process.env.PG_PASSWORD}@${process.env.PG_HOST}:${process.env.PG_PORT}/${process.env.PG_DATABASE}`;
const pool = new Pool({
connectionString: isProduction ? process.env.DATABASE_URL : connectionString,
ssl: {
rejectUnauthorized: false,
},
});
const generateUnique = (length = 24) => {
let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let str = "";
for (let i = 0; i < length; i++) {
str += chars.charAt(Math.floor(Math.random() * chars.length));
}
return str;
};
const save = (route) => {
pool.query("UPDATE Documents SET body = $1 where route = $2;", [db[route], route])
.then(() => {
console.log("Row updated successfully");
})
.catch((e) => console.error(e.stack));
};
var app = require("http")
.createServer(function (req, res) {
if (req.url === "/") {
let route = generateUnique();
pool
.query(
`INSERT INTO Documents(route,body) Values('${route}','/root\n# Hello\nThis is the first state\n[Go to next state](#next)\n\n/next\n# Next state\nThis is the next state!\n[Back](#root)');`
)
.then(() => {
create_page(route).then(
function (html) {
res.writeHead(200, { "Content-Type": "text/html" });
res.write(html);
res.end();
}.bind(res));
})
.catch((e) => console.error(e.stack));
} else if (req.url.match("public/stolen_victory_duospace_regular.ttf")) {
var fileStream = fs.createReadStream(path.join(__dirname + "/public/stolen_victory_duospace_regular.ttf"));
res.writeHead(200, { "Content-Type": "application/x-font-ttf" });
fileStream.pipe(res);
} else if (req.url.match("public/stolen_victory_duospace_bold.ttf")) {
var fileStream = fs.createReadStream(path.join(__dirname + "/public/stolen_victory_duospace_bold.ttf"));
res.writeHead(200, { "Content-Type": "application/x-font-ttf" });
fileStream.pipe(res);
} else if (req.url.match("public/pop.ttf")) {
var fileStream = fs.createReadStream(path.join(__dirname + "/public/pop.ttf"));
res.writeHead(200, { "Content-Type": "application/x-font-ttf" });
fileStream.pipe(res);
} else if (req.url.match("public/favicon.svg")) {
var fileStream = fs.createReadStream(
path.join(__dirname + "/public/favicon.svg"),
"UTF-8"
);
res.writeHead(200, { "Content-Type": "image/svg+xml" });
fileStream.pipe(res);
} else if (req.url.match("public/style.css")) {
var fileStream = fs.createReadStream(
path.join(__dirname + "/public/style.css"),
"UTF-8"
);
res.writeHead(200, { "Content-Type": "text/css" });
fileStream.pipe(res);
} else if (req.url.match("public/syntax.css")) {
var fileStream = fs.createReadStream(
path.join(__dirname + "/public/syntax.css"),
"UTF-8"
);
res.writeHead(200, { "Content-Type": "text/css" });
fileStream.pipe(res);
} else if (req.url.match("public/script.js")) {
var fileStream = fs.createReadStream(
path.join(__dirname + "/public/script.js"),
"UTF-8"
);
res.writeHead(200, { "Content-Type": "text/javascript" });
fileStream.pipe(res);
} else if (req.url.match(/^\/([A-Za-z0-9]{16})/)) {
var route = req.url.slice(1);
pool
.query("SELECT * FROM Documents WHERE route = $1;", [route])
.then((result) => {
var body, message;
if (result.rows.length > 0) {
body = result.rows[0].body;
render_route(route, body).then(
function (html) {
res.writeHead(200, { "Content-Type": "text/html" });
res.write(html);
res.end();
}.bind(res)
);
} else {
error_page().then(
function (html) {
res.writeHead(404, { "Content-Type": "text/html" });
res.write(html);
res.end();
}.bind(res));
}
})
.catch((e) => console.error(e.stack));
} else {
error_page().then(
function (html) {
res.writeHead(404, { "Content-Type": "text/html" });
res.write(html);
res.end();
}.bind(res));
}
})
.listen(process.env.PORT || 8080);
var io = require("socket.io")(app);
io.on("connection", (socket) => {
const route = socket.handshake.query.name;
socket.join(route);
console.log("A user has connected from " + route);
autosave[route] = setTimeout(() => {}, 1);
socket.on("refresh", function () {
body = db[route];
});
socket.on("updateDb", function (body) {
db[route] = body;
clearTimeout(autosave[route]);
autosave[route] = setTimeout(function () {
save(route);
}, 1000);
});
socket.on("change", function (op) {
if (
op.origin == "+input" ||
op.origin == "paste" ||
op.origin == "+delete"
) {
socket.broadcast.to(route).emit("change", op);
}
});
socket.on("disconnect", function () {
// TODO: save doc on the last disconnect?
});
});