server.js cleanup

This commit is contained in:
ポール ウェッブ 2018-07-05 13:50:18 -05:00
parent abd63a7352
commit cfb1b28ac6
2 changed files with 116 additions and 154 deletions

View file

@ -21,6 +21,7 @@
"cron": "^1.3.0", "cron": "^1.3.0",
"dotenv": "^5.0.1", "dotenv": "^5.0.1",
"express": "^4.16.3", "express": "^4.16.3",
"graceful-fs": "^4.1.11",
"heroku-ssl-redirect": "0.0.4", "heroku-ssl-redirect": "0.0.4",
"highlight.js": "^9.12.0", "highlight.js": "^9.12.0",
"jquery": "2.1.3", "jquery": "2.1.3",

243
server.js
View file

@ -1,85 +1,102 @@
// server.js "use strict"; require("dotenv").config();
// env variables
require('dotenv').config();
// fs // P A C K A G E S
var fs = require('fs');
// Async const async = require("async");
var async = require("async"); const bodyParser = require("body-parser");
// Express etc const cors = require("cors");
var sslRedirect = require('heroku-ssl-redirect'); const CronJob = require("cron").CronJob;
var express = require('express'); const express = require("express");
var serveStatic = require('serve-static'); const fs = require("graceful-fs");
var request = require('request'); const octokit = require("@octokit/rest")();
var cors = require('cors'); const redis = require("redis");
var bodyParser = require('body-parser'); const request = require("request");
// Cron const serveStatic = require("serve-static");
var CronJob = require('cron').CronJob; const sslRedirect = require("heroku-ssl-redirect");
// Github API
var octokit = require('@octokit/rest')();
if(typeof process.env.GITHUB_OAUTH_TOKEN != 'undefined') {
// V A R I A B L E S
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" });
let Slack;
let slack;
if (typeof process.env.GITHUB_OAUTH_TOKEN !== "undefined") {
octokit.authenticate({ octokit.authenticate({
type: 'oauth', type: "oauth",
token: process.env.GITHUB_OAUTH_TOKEN token: process.env.GITHUB_OAUTH_TOKEN
}); });
} }
// Redis
var redis = require("redis"), if (typeof process.env.SLACK_WEBHOOK_URL !== "undefined") {
redisClient = redis.createClient(process.env.REDISCLOUD_URL); Slack = require("slack-node");
// Slack slack = new Slack();
if(typeof process.env.SLACK_WEBHOOK_URL != 'undefined') {
var Slack = require('slack-node');
var slack = new Slack();
slack.setWebhook(process.env.SLACK_WEBHOOK_URL); slack.setWebhook(process.env.SLACK_WEBHOOK_URL);
} }
app = express();
// enable ssl redirect
// P R O G R A M
app.listen(port);
app.use(sslRedirect()); app.use(sslRedirect());
app.use(serveStatic(`${__dirname}/content/.vuepress/dist`));
app.use(serveStatic(__dirname + "/content/.vuepress/dist"));
app.use(cors()); app.use(cors());
var textParser = bodyParser.text({
limit: '256kb'
app.get("/github-feed", (req, res) => {
redisClient.zrevrange("events", 0, 9, (err, reply) => {
const events = [];
reply.forEach(item => events.push(JSON.parse(item)));
res.json(events);
});
}); });
var jsonParser = bodyParser.json(); 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");
}
});
app.post('/forward', jsonParser, function(req, res) {
var allowedMethods = ["wallet_send", "resolve", "publish"];
if(typeof req.body.method != "undefined") { app.post("/forward", jsonParser, (req, res) => {
const allowedMethods = ["wallet_send", "resolve", "publish"];
if (typeof req.body.method === "undefined") return;
if (allowedMethods.includes(req.body.method)) { if (allowedMethods.includes(req.body.method)) {
if (req.body.method === "wallet_send") {
req.body.amount = 0.01; // Hardcode the wallet_send amount
// We should whitelist the query parameters here const allowedClaims = [ // Whitelist claim ids
"fbdcd44a97810522d23d5f1335b8ca04be9d776c",
"de7f7fa33e8d879b2bae7238d2bdf827a39f9301",
"5b7c7a202201033d99e1be2930d290c127c0f4fe",
"a1372cf5523885f5923237bfe522f02f5f054362"
];
if(req.body.method == "wallet_send") { if (!allowedClaims.includes(req.body.claim_id)) res.json({});
// Hardcode the wallet_send amount to be always 0.01 always
req.body.amount = 0.01;
// Whitelist claim ids
var allowedClaims = ["fbdcd44a97810522d23d5f1335b8ca04be9d776c", "de7f7fa33e8d879b2bae7238d2bdf827a39f9301", "5b7c7a202201033d99e1be2930d290c127c0f4fe", "a1372cf5523885f5923237bfe522f02f5f054362"];
if(!allowedClaims.includes(req.body.claim_id)) {
res.json({});
} }
} if (req.body.method === "publish") {
req.body.bid = 0.001; // Hardcode the publish amount
if(req.body.method == "publish") {
// Hardcode the publish amount to be always 0.001 always
req.body.bid = 0.001;
// Fix the internal image path in daemon // Fix the internal image path in daemon
req.body.file_path = process.env.LBRY_DAEMON_IMAGES_PATH + req.body.file_path; req.body.file_path = process.env.LBRY_DAEMON_IMAGES_PATH + req.body.file_path;
} }
req.body.access_token = process.env.LBRY_DAEMON_ACCESS_TOKEN; req.body.access_token = process.env.LBRY_DAEMON_ACCESS_TOKEN;
@ -87,132 +104,76 @@ app.post('/forward', jsonParser, function(req, res) {
request({ request({
url: "http://daemon.lbry.tech", url: "http://daemon.lbry.tech",
qs: req.body qs: req.body
}, function(error, response, body) { }, (error, response, body) => {
// Should we filter the body parameters before forwarding to user?
body = JSON.parse(body); body = JSON.parse(body);
if(typeof body.error != "undefined") { if (typeof body.error !== "undefined") logSlackError("ERROR: Got error from daemon:\n", "```" + JSON.stringify(body.error) + "```");
logSlackError('ERROR: Got error from daemon: ' + JSON.stringify(body.error));
}
res.json(body); res.json(body);
}); });
} }
}
}); });
app.get('/github-feed', function(req, res) { app.post("/upload-image", textParser, (req, res) => {
redisClient.zrevrange('events', 0, 9, function(err, reply) {
var events = [];
reply.forEach(function(item) {
events.push(JSON.parse(item));
});
res.json(events);
});
});
app.post('/upload-image', textParser, function(req, res) {
request({ request({
method: "PUT", method: "PUT",
url: "http://daemon.lbry.tech/images.php", url: "http://daemon.lbry.tech/images.php",
headers: { headers: {
'Content-Type': 'text/plain' "Content-Type": "text/plain"
}, },
qs: { qs: {
access_token: process.env.LBRY_DAEMON_ACCESS_TOKEN access_token: process.env.LBRY_DAEMON_ACCESS_TOKEN
}, },
body: req.body, body: req.body,
}, function(error, response, body) { }, (error, response, body) => {
body = JSON.parse(body); body = JSON.parse(body);
res.json(body); res.json(body);
}); });
}); });
app.get('/*', function(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');
}
}); // H E L P E R S
var port = process.env.PORT || 8080; logSlackError(`Server started at port \`${port}\``);
app.listen(port);
logSlackError('Server started at port ' + port);
function updateGithubFeed() { function updateGithubFeed() {
octokit.activity.getEventsForOrg({ octokit.activity.getEventsForOrg({
org: 'lbryio', org: "lbryio",
per_page: 20, per_page: 20,
page: 1 page: 1
}).then(function({data}) { }).then(({ data }) => {
async.eachSeries(data, (item, callback) => {
async.eachSeries(data, function(item, callback) { const eventString = JSON.stringify(item);
var eventString = JSON.stringify(item);
redisClient.zrank('events', eventString, function(err, reply) {
if(reply == null) {
redisClient.zadd('events', item.id, eventString, callback);
} else {
callback();
}
redisClient.zrank("events", eventString, (err, reply) => {
if (reply === null) redisClient.zadd("events", item.id, eventString, callback);
else callback();
}); });
}, () => {
}, function() {
// Keep the latest 50 events // Keep the latest 50 events
redisClient.zremrangebyrank('events', 0, -51); redisClient.zremrangebyrank("events", 0, -51);
console.log('Updated Github feed');
}); });
}).catch(err => {
}).catch(function(err) { logSlackError("ERROR: Unable to update Github feed:\n", "```" + JSON.stringify(err) + "```");
logSlackError('ERROR: Couldnt update Github feed: ' + JSON.stringify(err));
}); });
} }
function logSlackError(text) { function logSlackError(text) {
if (typeof slack === "undefined") return;
if(typeof slack != 'undefined') {
slack.webhook({ slack.webhook({
channel: 'dottech-errors', channel: "dottech-errors",
username: 'lbrytech-bot', username: "lbrytech-bot",
text: text text: text
}, function(err, response) { }, (err, response) => { // eslint-disable-line
// do nothing?
}); });
}
} }
// Update Github feed every minute // Update Github feed every minute
new CronJob("0 * * * * *", updateGithubFeed, null, true, 'America/Los_Angeles'); new CronJob("0 * * * * *", updateGithubFeed, null, true, "America/Los_Angeles");
module.exports = app;
// E X P O R T
module.exports = exports = app;