"use strict"; require("dotenv").config(); require("date-format-lite"); // P A C K A G E S const chalk = require("chalk"); const cors = require("cors"); const dedent = require("dedent"); const fastify = require("fastify")({ logger: { level: "warn", prettyPrint: process.env.NODE_ENV === "development" ? true : false } }); const octokit = require("@octokit/rest")(); const redis = require("redis"); const relativeDate = require("relative-date"); const local = require("app-root-path").require; // V A R I A B L E S const github = local("/helpers/github"); const log = console.log; // eslint-disable-line const logSlackError = local("/helpers/slack"); let client; if (typeof process.env.GITHUB_OAUTH_TOKEN !== "undefined") { octokit.authenticate({ type: "oauth", token: process.env.GITHUB_OAUTH_TOKEN }); } if (typeof process.env.REDISCLOUD_URL !== "undefined") { client = redis.createClient(process.env.REDISCLOUD_URL); client.on("error", redisError => { process.env.NODE_ENV === "development" ? log("Unable to connect to Redis client. You may be missing an .env file") : logSlackError("An error occured with Redis", redisError) ; }); } // 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"), { root: `${__dirname}/public/`, prefix: "/assets/" }); fastify.register(require("choo-ssr/fastify"), { app: require("./client"), plugins: [ [ require("choo-bundles/ssr"), {} ] ] }); fastify.ready(err => { if (err) throw err; fastify.ws.on("connection", socket => { socket.send(JSON.stringify({ "message": "welcome" })); socket.on("message", msg => { if (msg === "landed on homepage") { generateGitHubFeed(result => { socket.send(JSON.stringify({ "message": "updated html", "html": result, "selector": "#github-feed" })); }); } }); socket.on("close", () => log("Client disconnected.")); // TODO: Close socket? }); }); // 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" ? log(`\n— ${chalk.green("⚡")} ${fastify.server.address().port}\n`) : logSlackError(`Server started at port \`${fastify.server.address().port}\``) ; }; start(); // H E L P E R S function generateGitHubFeed(displayGitHubFeed) { if (typeof process.env.REDISCLOUD_URL !== "undefined") { client.zrevrange("events", 0, 9, (err, reply) => { if (err) return; // TODO: Render a div with nice error message const events = []; const renderedEvents = []; reply.forEach(item => events.push(JSON.parse(item))); for (const event of events) { renderedEvents.push(`

${github.generateEvent(event)} ${event.repo.name} ${relativeDate(new Date(event.created_at))}

`); } // TODO: Update `.last-updated` every minute displayGitHubFeed(dedent`

GitHub

Last updated: ${new Date().format("YYYY-MM-DD at H:mm:ss A").toLowerCase().replace(/-/g, "·")}
${renderedEvents.join("")} `); }); } }