lbry.tech/server.js

219 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-03-28 16:26:59 +02:00
// server.js
2018-05-12 12:03:21 +02:00
// env variables
require('dotenv').config();
2018-05-30 17:25:04 +02:00
// fs
var fs = require('fs');
2018-05-12 12:03:21 +02:00
// Async
var async = require("async");
// Express etc
2018-03-29 08:24:59 +02:00
var sslRedirect = require('heroku-ssl-redirect');
2018-03-28 16:26:59 +02:00
var express = require('express');
var path = require('path');
var serveStatic = require('serve-static');
2018-04-10 11:08:46 +02:00
var request = require('request');
var cors = require('cors');
2018-06-08 11:56:28 +02:00
var bodyParser = require('body-parser');
2018-05-12 12:03:21 +02:00
// Cron
var CronJob = require('cron').CronJob;
// Github API
var octokit = require('@octokit/rest')();
2018-06-01 10:32:40 +02:00
if(typeof process.env.GITHUB_OAUTH_TOKEN != 'undefined') {
octokit.authenticate({
type: 'oauth',
token: process.env.GITHUB_OAUTH_TOKEN
});
}
2018-05-12 12:03:21 +02:00
// Redis
var redis = require("redis"),
redisClient = redis.createClient(process.env.REDISCLOUD_URL);
2018-06-01 09:10:22 +02:00
// 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);
}
2018-03-28 16:26:59 +02:00
app = express();
2018-06-01 22:23:53 +02:00
// enable ssl redirect
app.use(sslRedirect());
2018-05-30 11:24:43 +02:00
app.use(serveStatic(__dirname + "/content/.vuepress/dist"));
2018-03-28 16:26:59 +02:00
2018-04-10 11:08:46 +02:00
app.use(cors());
2018-06-08 12:23:04 +02:00
var textParser = bodyParser.text({
limit: '256kb'
});
2018-06-08 11:56:28 +02:00
2018-06-15 07:55:57 +02:00
var jsonParser = bodyParser.json();
app.post('/forward', jsonParser, function(req, res) {
2018-04-10 11:08:46 +02:00
2018-05-23 16:25:16 +02:00
var allowedMethods = ["wallet_send", "resolve", "publish"];
2018-06-15 07:55:57 +02:00
if(typeof req.body.method != "undefined") {
2018-04-10 11:08:46 +02:00
2018-06-15 07:55:57 +02:00
if(allowedMethods.includes(req.body.method)) {
2018-04-10 11:08:46 +02:00
2018-05-23 16:25:16 +02:00
// We should whitelist the query parameters here
2018-06-15 07:55:57 +02:00
if(req.body.method == "wallet_send") {
2018-05-23 16:25:16 +02:00
// Hardcode the wallet_send amount to be always 0.01 always
2018-06-15 07:55:57 +02:00
req.body.amount = 0.01;
2018-05-23 16:25:16 +02:00
// Whitelist claim ids
var allowedClaims = ["fbdcd44a97810522d23d5f1335b8ca04be9d776c", "de7f7fa33e8d879b2bae7238d2bdf827a39f9301", "5b7c7a202201033d99e1be2930d290c127c0f4fe", "a1372cf5523885f5923237bfe522f02f5f054362"];
2018-04-16 15:39:11 +02:00
2018-06-15 07:55:57 +02:00
if(!allowedClaims.includes(req.body.claim_id)) {
2018-05-23 16:25:16 +02:00
res.json({});
}
}
2018-06-15 07:55:57 +02:00
if(req.body.method == "publish") {
2018-06-08 13:45:56 +02:00
// Hardcode the publish amount to be always 0.001 always
2018-06-15 07:55:57 +02:00
req.body.bid = 0.001;
2018-06-08 13:45:56 +02:00
// Fix the internal image path in daemon
2018-06-15 07:55:57 +02:00
req.body.file_path = process.env.LBRY_DAEMON_IMAGES_PATH + req.body.file_path;
2018-06-08 13:45:56 +02:00
}
2018-06-15 07:55:57 +02:00
req.body.access_token = process.env.LBRY_DAEMON_ACCESS_TOKEN;
2018-05-30 07:50:48 +02:00
2018-05-23 16:25:16 +02:00
request({
url: "http://daemon.lbry.tech",
2018-06-15 07:55:57 +02:00
qs: req.body
2018-05-23 16:25:16 +02:00
}, function(error, response, body) {
2018-06-02 12:35:07 +02:00
// Should we filter the body parameters before forwarding to user?
2018-06-08 12:23:04 +02:00
body = JSON.parse(body);
2018-06-01 10:23:08 +02:00
if(typeof body.error != "undefined") {
2018-06-01 09:10:22 +02:00
logSlackError('ERROR: Got error from daemon: ' + JSON.stringify(body.error));
2018-05-30 11:50:19 +02:00
}
2018-06-02 12:35:07 +02:00
res.json(body);
2018-05-23 16:25:16 +02:00
});
}
2018-04-10 11:08:46 +02:00
}
});
2018-05-12 12:03:21 +02:00
app.get('/github-feed', function(req, res) {
redisClient.zrevrange('events', 0, 9, function(err, reply) {
var events = [];
reply.forEach(function(item) {
events.push(JSON.parse(item));
});
res.json(events);
});
});
2018-06-08 12:23:04 +02:00
app.post('/upload-image', textParser, function(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: {
'Content-Type': 'text/plain'
},
2018-06-08 11:12:49 +02:00
qs: {
access_token: process.env.LBRY_DAEMON_ACCESS_TOKEN
},
body: req.body,
}, function(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
app.get('/*', function(req, res) {
2018-06-20 11:42:19 +02:00
if(fs.existsSync(__dirname + "/content/.vuepress/dist" + req.path + ".html")) {
2018-05-30 17:25:04 +02:00
res.redirect(req.path + ".html");
} else {
res.status(404);
res.send('Not found');
}
});
2018-03-29 08:24:59 +02:00
var port = process.env.PORT || 8080;
2018-03-28 16:26:59 +02:00
app.listen(port);
2018-06-01 09:10:22 +02:00
logSlackError('Server started at port ' + port);
2018-05-12 12:03:21 +02:00
function updateGithubFeed() {
octokit.activity.getEventsForOrg({
org: 'lbryio',
per_page: 20,
page: 1
}).then(function({data}) {
async.eachSeries(data, function(item, callback) {
var eventString = JSON.stringify(item);
redisClient.zrank('events', eventString, function(err, reply) {
if(reply == null) {
redisClient.zadd('events', item.id, eventString, callback);
} else {
callback();
}
});
}, function() {
// Keep the latest 50 events
redisClient.zremrangebyrank('events', 0, -51);
2018-06-01 09:10:22 +02:00
console.log('Updated Github feed');
2018-05-12 12:03:21 +02:00
});
2018-05-30 11:59:17 +02:00
}).catch(function(err) {
2018-05-30 11:50:19 +02:00
2018-06-01 09:10:22 +02:00
logSlackError('ERROR: Couldnt update Github feed: ' + JSON.stringify(err));
2018-05-30 11:50:19 +02:00
2018-05-12 12:03:21 +02:00
});
}
2018-06-01 09:10:22 +02:00
function logSlackError(text) {
2018-06-01 10:00:39 +02:00
if(typeof slack != 'undefined') {
2018-06-01 09:10:22 +02:00
slack.webhook({
channel: 'dottech-errors',
username: 'lbrytech-bot',
text: text
2018-06-01 10:00:39 +02:00
}, function(err, response) {
2018-06-01 10:23:08 +02:00
2018-06-01 09:10:22 +02:00
});
}
}
2018-05-12 12:03:21 +02:00
// Update Github feed every minute
2018-06-01 09:10:22 +02:00
new CronJob("0 * * * * *", updateGithubFeed, null, true, 'America/Los_Angeles');
2018-06-06 13:02:16 +02:00
module.exports = app;