lbry.tech/server.js

115 lines
2.3 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();
// 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-05-12 12:03:21 +02:00
// Cron
var CronJob = require('cron').CronJob;
// Github API
var octokit = require('@octokit/rest')();
// Redis
var redis = require("redis"),
redisClient = redis.createClient(process.env.REDISCLOUD_URL);
2018-03-28 16:26:59 +02:00
app = express();
2018-04-20 15:17:16 +02:00
app.use(serveStatic(__dirname + "/.vuepress/dist"));
2018-03-28 16:26:59 +02:00
2018-04-10 11:08:46 +02:00
app.use(cors());
2018-03-29 08:24:59 +02:00
// enable ssl redirect
2018-03-29 08:28:54 +02:00
app.use(sslRedirect(['other','development', 'staging', 'production']));
2018-03-29 08:24:59 +02:00
2018-04-10 11:08:46 +02:00
app.get('/forward', function(req, res) {
if(typeof req.query.method != "undefined") {
// We should whitelist the query parameters here
2018-04-16 15:39:11 +02:00
// Hardcode the wallet_send amount to be 0.01 always
if(req.query.method == "wallet_send") {
req.query.amount = 0.01;
}
2018-04-10 11:08:46 +02:00
request({
url: "http://daemon.lbry.tech",
qs: req.query
}, function(error, response, body) {
// Should we parse the body before forwarding?
res.setHeader('Content-Type', 'application/json');
res.send(body);
})
}
});
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-03-29 08:24:59 +02:00
var port = process.env.PORT || 8080;
2018-03-28 16:26:59 +02:00
app.listen(port);
console.log('server started '+ 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);
console.log('Updated Github feed');
});
});
}
// Update Github feed every minute
new CronJob("0 * * * * *", updateGithubFeed, null, true, 'America/Los_Angeles');