lbry.tech/server.js

156 lines
3.9 KiB
JavaScript
Raw Normal View History

2018-07-12 23:07:16 +02:00
"use strict"; require("dotenv").config(); require("date-format-lite");
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 23:07:16 +02:00
const dedent = require("dedent");
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 23:07:16 +02:00
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");
2018-07-12 17:21:42 +02:00
const log = console.log; // eslint-disable-line
2018-07-12 23:07:16 +02:00
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)
;
});
}
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());
2018-07-12 23:07:16 +02:00
2018-07-12 17:21:42 +02:00
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.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 => {
2018-07-12 23:07:16 +02:00
socket.send(JSON.stringify({ "message": "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-07-12 23:07:16 +02:00
generateGitHubFeed(result => {
socket.send(JSON.stringify({
"message": "updated html",
"html": result,
"selector": "#github-feed"
}));
});
2018-07-12 17:21:42 +02:00
}
2018-05-12 12:03:21 +02:00
});
2018-07-05 20:50:18 +02:00
2018-07-12 23:07:16 +02:00
socket.on("close", () => log("Client disconnected.")); // TODO: Close socket?
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 23:07:16 +02:00
process.env.NODE_ENV === "development" ?
log(`\n${chalk.green("⚡")} ${fastify.server.address().port}\n`) :
logSlackError(`Server started at port \`${fastify.server.address().port}\``)
;
2018-07-12 17:21:42 +02:00
};
2018-06-06 13:02:16 +02:00
2018-07-12 17:21:42 +02:00
start();
2018-07-12 23:07:16 +02:00
// 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(`
<div class='github-feed__event'>
<a href="${github.generateUrl("actor", event)}" target="_blank" rel="noopener noreferrer">
<img src="${event.actor.avatar_url}" class="github-feed__event__avatar" alt=""/>
</a>
<p>
${github.generateEvent(event)}
<a href="${github.generateUrl("repo", event)}" title="View this repo on GitHub" target="_blank" rel="noopener noreferrer"><strong>${event.repo.name}</strong></a>
<em class="github-feed__event__time">${relativeDate(new Date(event.created_at))}</em>
</p>
</div>
`);
}
// TODO: Update `.last-updated` every minute
displayGitHubFeed(dedent`
<h3>GitHub</h3>
<h5 class="last-updated">Last updated: ${new Date().format("YYYY-MM-DD at H:mm:ss A").toLowerCase().replace(/-/g, "&middot;")}</h5>
${renderedEvents.join("")}
`);
});
}
}