2018-07-05 20:50:18 +02:00
|
|
|
"use strict"; require("dotenv").config();
|
2018-06-01 22:23:53 +02:00
|
|
|
|
|
|
|
|
2018-03-28 16:26:59 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
// P A C K A G E S
|
2018-04-10 11:08:46 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
const chalk = require("chalk");
|
2018-07-05 20:50:18 +02:00
|
|
|
const cors = require("cors");
|
2018-07-12 17:21:42 +02:00
|
|
|
// const local = require("app-root-path").require;
|
2018-04-10 11:08:46 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
// V A R I A B L E S
|
2018-05-23 16:25:16 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
const fastify = require("fastify")({
|
|
|
|
logger: {
|
|
|
|
level: "warn",
|
|
|
|
prettyPrint: process.env.NODE_ENV === "development" ? true : false
|
|
|
|
}
|
|
|
|
});
|
2018-05-23 16:25:16 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
const log = console.log; // eslint-disable-line
|
|
|
|
// const logSlackError = local("/helpers/slack");
|
2018-05-23 16:25:16 +02:00
|
|
|
|
|
|
|
|
2018-04-16 15:39:11 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
// P R O G R A M
|
2018-05-23 16:25:16 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
fastify.use(cors());
|
|
|
|
fastify.register(require("fastify-compress"));
|
|
|
|
fastify.register(require("fastify-ws"));
|
2018-06-08 13:45:56 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
fastify.register(require("fastify-helmet"), {
|
|
|
|
hidePoweredBy: { setTo: "LBRY" }
|
2018-07-05 20:50:18 +02:00
|
|
|
});
|
2018-06-08 13:45:56 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
fastify.register(require("fastify-static"), {
|
|
|
|
root: `${__dirname}/public/`,
|
|
|
|
prefix: "/assets/"
|
2018-07-05 20:50:18 +02:00
|
|
|
});
|
2018-05-30 07:50:48 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
fastify.register(require("choo-ssr/fastify"), {
|
|
|
|
app: require("./client"),
|
|
|
|
plugins: [
|
|
|
|
[ require("choo-bundles/ssr"), {} ]
|
|
|
|
]
|
|
|
|
});
|
2018-05-23 16:25:16 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
/*
|
|
|
|
fastify.decorate("io", new WebSocket.Server({ server: fastify.server }));
|
2018-04-10 11:08:46 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
fastify.io.on("connection", (socket, req) => {
|
|
|
|
console.log("connected");
|
|
|
|
socket.url = req.url;
|
2018-06-26 20:45:06 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
socket.on("disconnect", () => {
|
|
|
|
console.log("someone left");
|
|
|
|
});
|
2018-05-12 12:03:21 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
// On message broadcast to everyone
|
|
|
|
socket.on("message", data => {
|
|
|
|
// Broadcast to everyone else
|
|
|
|
fastify.io.clients.forEach(client => {
|
|
|
|
console.log(socket.url, client.url);
|
2018-05-12 12:03:21 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
if (socket.url === client.url && client.readyState === WebSocket.OPEN) {
|
|
|
|
client.send(data);
|
|
|
|
}
|
2018-07-05 20:50:18 +02:00
|
|
|
});
|
2018-06-08 11:12:49 +02:00
|
|
|
});
|
|
|
|
});
|
2018-07-12 17:21:42 +02:00
|
|
|
*/
|
2018-06-08 11:12:49 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
fastify.ready(err => {
|
|
|
|
if (err) throw err;
|
2018-05-30 17:25:04 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
fastify.ws.on("connection", socket => {
|
|
|
|
// console.log("Client connected.");
|
|
|
|
socket.send("welcome");
|
2018-05-30 17:25:04 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
socket.on("message", msg => {
|
|
|
|
if (msg === "landed on homepage") {
|
|
|
|
//
|
|
|
|
}
|
2018-03-28 16:26:59 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
socket.send(msg); // Creates an echo server
|
2018-05-12 12:03:21 +02:00
|
|
|
});
|
2018-07-05 20:50:18 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
socket.on("close", () => console.log("Client disconnected."));
|
2018-07-05 20:50:18 +02:00
|
|
|
});
|
2018-07-12 17:21:42 +02:00
|
|
|
});
|
|
|
|
|
2018-06-01 09:10:22 +02:00
|
|
|
|
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
// B E G I N
|
2018-06-01 09:10:22 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
const start = async () => {
|
|
|
|
try {
|
|
|
|
await fastify.listen(process.env.PORT || 8080, process.env.IP || "0.0.0.0");
|
|
|
|
} catch (err) {
|
|
|
|
fastify.log.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2018-06-01 09:10:22 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
log(`\n— ${chalk.green("⚡")} ${fastify.server.address().port}\n`);
|
|
|
|
// logSlackError(`Server started at port \`${fastify.server.address().port}\``);
|
|
|
|
};
|
2018-06-06 13:02:16 +02:00
|
|
|
|
2018-07-12 17:21:42 +02:00
|
|
|
start();
|