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-05 20:50:18 +02:00
|
|
|
const async = require("async");
|
|
|
|
const bodyParser = require("body-parser");
|
|
|
|
const cors = require("cors");
|
|
|
|
const CronJob = require("cron").CronJob;
|
|
|
|
const express = require("express");
|
|
|
|
const fs = require("graceful-fs");
|
|
|
|
const octokit = require("@octokit/rest")();
|
|
|
|
const redis = require("redis");
|
|
|
|
const request = require("request");
|
|
|
|
const serveStatic = require("serve-static");
|
|
|
|
const sslRedirect = require("heroku-ssl-redirect");
|
2018-06-08 11:56:28 +02:00
|
|
|
|
2018-06-15 07:55:57 +02:00
|
|
|
|
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-05 20:50:18 +02:00
|
|
|
const app = express();
|
|
|
|
const jsonParser = bodyParser.json();
|
|
|
|
const port = process.env.PORT || 8080;
|
|
|
|
const redisClient = redis.createClient(process.env.REDISCLOUD_URL);
|
|
|
|
const textParser = bodyParser.text({ limit: "256kb" });
|
2018-04-10 11:08:46 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
let Slack;
|
|
|
|
let slack;
|
2018-04-10 11:08:46 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
if (typeof process.env.GITHUB_OAUTH_TOKEN !== "undefined") {
|
|
|
|
octokit.authenticate({
|
|
|
|
type: "oauth",
|
|
|
|
token: process.env.GITHUB_OAUTH_TOKEN
|
|
|
|
});
|
|
|
|
}
|
2018-05-23 16:25:16 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
if (typeof process.env.SLACK_WEBHOOK_URL !== "undefined") {
|
|
|
|
Slack = require("slack-node");
|
|
|
|
slack = new Slack();
|
|
|
|
slack.setWebhook(process.env.SLACK_WEBHOOK_URL);
|
|
|
|
}
|
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-05 20:50:18 +02:00
|
|
|
app.listen(port);
|
|
|
|
app.use(sslRedirect());
|
|
|
|
app.use(serveStatic(`${__dirname}/content/.vuepress/dist`));
|
|
|
|
app.use(cors());
|
2018-05-23 16:25:16 +02:00
|
|
|
|
2018-06-08 13:45:56 +02:00
|
|
|
|
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
app.get("/github-feed", (req, res) => {
|
|
|
|
redisClient.zrevrange("events", 0, 9, (err, reply) => {
|
|
|
|
const events = [];
|
2018-06-08 13:45:56 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
reply.forEach(item => events.push(JSON.parse(item)));
|
|
|
|
res.json(events);
|
|
|
|
});
|
|
|
|
});
|
2018-06-08 13:45:56 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
app.get("/*", (req, res) => {
|
|
|
|
if (fs.existsSync(`${__dirname}/content/.vuepress/dist${req.path}.html`)) {
|
|
|
|
res.redirect(`${req.path}.html`);
|
|
|
|
} else {
|
|
|
|
res.status(404);
|
|
|
|
res.send("Not found");
|
|
|
|
}
|
|
|
|
});
|
2018-05-30 07:50:48 +02:00
|
|
|
|
2018-05-23 16:25:16 +02:00
|
|
|
|
2018-04-10 11:08:46 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
app.post("/forward", jsonParser, (req, res) => {
|
|
|
|
const allowedMethods = ["wallet_send", "resolve", "publish"];
|
2018-04-10 11:08:46 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
if (typeof req.body.method === "undefined") return;
|
2018-04-10 11:08:46 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
if (allowedMethods.includes(req.body.method)) {
|
|
|
|
if (req.body.method === "wallet_send") {
|
|
|
|
req.body.amount = 0.01; // Hardcode the wallet_send amount
|
2018-05-12 12:03:21 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
const allowedClaims = [ // Whitelist claim ids
|
|
|
|
"fbdcd44a97810522d23d5f1335b8ca04be9d776c",
|
|
|
|
"de7f7fa33e8d879b2bae7238d2bdf827a39f9301",
|
|
|
|
"5b7c7a202201033d99e1be2930d290c127c0f4fe",
|
|
|
|
"a1372cf5523885f5923237bfe522f02f5f054362"
|
|
|
|
];
|
2018-05-12 12:03:21 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
if (!allowedClaims.includes(req.body.claim_id)) res.json({});
|
|
|
|
}
|
2018-05-12 12:03:21 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
if (req.body.method === "publish") {
|
|
|
|
req.body.bid = 0.001; // Hardcode the publish amount
|
2018-06-26 20:45:06 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
// Fix the internal image path in daemon
|
|
|
|
req.body.file_path = process.env.LBRY_DAEMON_IMAGES_PATH + req.body.file_path;
|
|
|
|
}
|
2018-05-12 12:03:21 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
req.body.access_token = process.env.LBRY_DAEMON_ACCESS_TOKEN;
|
2018-05-12 12:03:21 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
request({
|
|
|
|
url: "http://daemon.lbry.tech",
|
|
|
|
qs: req.body
|
|
|
|
}, (error, response, body) => {
|
|
|
|
body = JSON.parse(body);
|
|
|
|
if (typeof body.error !== "undefined") logSlackError("ERROR: Got error from daemon:\n", "```" + JSON.stringify(body.error) + "```");
|
|
|
|
res.json(body);
|
|
|
|
});
|
|
|
|
}
|
2018-05-12 12:03:21 +02:00
|
|
|
});
|
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
app.post("/upload-image", textParser, (req, res) => {
|
2018-06-08 11:12:49 +02:00
|
|
|
request({
|
|
|
|
method: "PUT",
|
|
|
|
url: "http://daemon.lbry.tech/images.php",
|
2018-06-08 13:00:55 +02:00
|
|
|
headers: {
|
2018-07-05 20:50:18 +02:00
|
|
|
"Content-Type": "text/plain"
|
2018-06-08 13:00:55 +02:00
|
|
|
},
|
2018-06-08 11:12:49 +02:00
|
|
|
qs: {
|
|
|
|
access_token: process.env.LBRY_DAEMON_ACCESS_TOKEN
|
|
|
|
},
|
|
|
|
body: req.body,
|
2018-07-05 20:50:18 +02:00
|
|
|
}, (error, response, body) => {
|
2018-06-08 12:23:04 +02:00
|
|
|
body = JSON.parse(body);
|
2018-06-08 11:12:49 +02:00
|
|
|
res.json(body);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-30 17:25:04 +02:00
|
|
|
|
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
// H E L P E R S
|
2018-03-28 16:26:59 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
logSlackError(`Server started at port \`${port}\``);
|
2018-05-12 12:03:21 +02:00
|
|
|
|
|
|
|
function updateGithubFeed() {
|
|
|
|
octokit.activity.getEventsForOrg({
|
2018-07-05 20:50:18 +02:00
|
|
|
org: "lbryio",
|
2018-05-12 12:03:21 +02:00
|
|
|
per_page: 20,
|
|
|
|
page: 1
|
2018-07-05 20:50:18 +02:00
|
|
|
}).then(({ data }) => {
|
|
|
|
async.eachSeries(data, (item, callback) => {
|
|
|
|
const eventString = JSON.stringify(item);
|
2018-05-12 12:03:21 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
redisClient.zrank("events", eventString, (err, reply) => {
|
|
|
|
if (reply === null) redisClient.zadd("events", item.id, eventString, callback);
|
|
|
|
else callback();
|
2018-05-12 12:03:21 +02:00
|
|
|
});
|
2018-07-05 20:50:18 +02:00
|
|
|
}, () => {
|
2018-05-12 12:03:21 +02:00
|
|
|
// Keep the latest 50 events
|
2018-07-05 20:50:18 +02:00
|
|
|
redisClient.zremrangebyrank("events", 0, -51);
|
2018-05-12 12:03:21 +02:00
|
|
|
});
|
2018-07-05 20:50:18 +02:00
|
|
|
}).catch(err => {
|
|
|
|
logSlackError("ERROR: Unable to update Github feed:\n", "```" + JSON.stringify(err) + "```");
|
2018-05-12 12:03:21 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-01 09:10:22 +02:00
|
|
|
function logSlackError(text) {
|
2018-07-05 20:50:18 +02:00
|
|
|
if (typeof slack === "undefined") return;
|
|
|
|
|
|
|
|
slack.webhook({
|
|
|
|
channel: "dottech-errors",
|
|
|
|
username: "lbrytech-bot",
|
|
|
|
text: text
|
|
|
|
}, (err, response) => { // eslint-disable-line
|
|
|
|
// do nothing?
|
|
|
|
});
|
|
|
|
}
|
2018-06-01 09:10:22 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
// Update Github feed every minute
|
|
|
|
new CronJob("0 * * * * *", updateGithubFeed, null, true, "America/Los_Angeles");
|
2018-06-01 09:10:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
// E X P O R T
|
2018-06-06 13:02:16 +02:00
|
|
|
|
2018-07-05 20:50:18 +02:00
|
|
|
module.exports = exports = app;
|