"use strict"; require("dotenv").config(); require("date-format-lite"); // P A C K A G E S const color = require("colorette"); const cors = require("cors"); const local = require("app-root-path").require; const fastify = require("fastify")({ logger: { level: "warn", prettyPrint: process.env.NODE_ENV === "development" ? true : false } }); // V A R I A B L E S const handleSocketMessages = local("app/sockets"); const logSlackError = local("app/helpers/slack"); // P R O G R A M fastify.use(cors()); fastify.register(require("fastify-compress")); fastify.register(require("fastify-ws")); fastify.register(require("fastify-helmet"), { hidePoweredBy: { setTo: "LBRY" } }); fastify.register(require("fastify-static"), { prefix: "/assets/", root: `${__dirname}/app/dist/` }); fastify.register(require("choo-ssr/fastify"), { app: require("./app") }); fastify.ready(err => { if (err) throw err; fastify.ws.on("connection", socket => { socket.on("message", data => { data = JSON.parse(data); return handleSocketMessages(socket, data); }); socket.on("close", () => socket.terminate()); }); }); // B E G I N 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); } process.env.NODE_ENV === "development" ? process.stdout.write(`\n— ${color.green("⚡")} ${fastify.server.address().port}\n`) : logSlackError(`Server started at port \`${fastify.server.address().port}\``) ; }; start();