reorganized back end routes

This commit is contained in:
bill bittner 2017-06-13 11:00:50 -07:00
parent 51d9f00bc5
commit 88a8a738d0
10 changed files with 104 additions and 99 deletions

View file

@ -47,7 +47,7 @@ module.exports = {
publish: function(name, filepath, license, nsfw, socket) {
// update the client
socket.emit("publish-status", "Your image is being published (this might take a second)...");
visitor.event("Publish", "Publish Request", name, {filepath, license, nsfw}).send();
visitor.event("Publish Route", "Publish Request", name, {filepath, license, nsfw}).send();
// create the publish object
var publishParams = createPublishParams(name, filepath, license, nsfw);
// get a promise to publish
@ -59,7 +59,7 @@ module.exports = {
deleteTemporaryFile(filepath);
})
.catch(function(error){
visitor.event("Publish", "Publish Failure", name, {filepath, license, nsfw}).send();
visitor.event("Publish Route", "Publish Failure", name, {filepath, license, nsfw}).send();
console.log("error:", error);
socket.emit("publish-failure", handlePublishError(error));
deleteTemporaryFile(filepath);

View file

@ -1,13 +1,10 @@
var path = require('path');
var axios = require('axios');
var lbryApi = require('../helpers/lbryApi.js');
module.exports = function(app){
module.exports = function(app, axios){
// route to return claim list in json
app.get("/claim_list/:claim", function(req, res){
app.get("/api/claim_list/:claim", function(req, res){
var claim = req.params.claim;
// make a call to the daemon
axios.post('http://localhost:5279/lbryapi', {
axios.post('http://localhost:5279/lbryapi', { // to do: extrapolate into lbryApi 'claim list' method and call that
method: "claim_list",
params: {
name: claim

10
routes/home-routes.js Normal file
View file

@ -0,0 +1,10 @@
module.exports = function(app){
// route for the home page
app.get("/", function(req, res){
res.status(200).render('index');
});
// a catch-all route if someone visits a page that does not exist
app.use("*", function(req, res){
res.status(404).render('fourOhFour');
});
}

View file

@ -1,65 +0,0 @@
var path = require('path');
var routeHelpers = require('../helpers/routeHelpers.js');
var lbryApi = require('../helpers/lbryApi.js');
var ua = require('universal-analytics');
var config = require('config');
var googleAnalyticsId = config.get('AnalyticsConfig.googleId');
var visitor = ua(googleAnalyticsId, {https: true});
module.exports = function(app){
// route to fetch all free public claims
app.get("/:name/all", function(req, res){
console.log(">> GET request on /" + req.params.name + " (all)");
visitor.event("Claim Request", "Name/all", req.params.name + "/all").send();
// create promise
lbryApi.getAllClaims(req.params.name)
.then(function(orderedFreePublicClaims){
console.log("/:name/all success.");
res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
return;
})
.catch(function(error){
console.log("/:name/all error:", error);
routeHelpers.handleRequestError(error, res);
})
});
// route to fetch one free public claim
app.get("/:name/:claim_id", function(req, res){
visitor.event("Claim Request", "Name/ClaimId", req.params.name + "/" + req.params.claim_id).send();
var uri = req.params.name + "#" + req.params.claim_id;
console.log(">> GET request on /" + uri);
// create promise
lbryApi.getClaimBasedOnUri(uri)
.then(function(filePath){
console.log("/:name/:claim_id success.");
res.status(200).sendFile(filePath);
})
.catch(function(error){
console.log("/:name/:claim_id error:", error)
routeHelpers.handleRequestError(error, res);
});
});
// route to fetch one free public claim
app.get("/:name", function(req, res){
visitor.event("Claim Request", "Name", req.params.name).send();
console.log(">> GET request on /" + req.params.name);
// create promise
lbryApi.getClaimBasedOnNameOnly(req.params.name)
.then(function(filePath){
console.log("/:name success.")
res.status(200).sendFile(filePath);
}).catch(function(error){
console.log("/:name error:", error);
routeHelpers.handleRequestError(error, res);
});
});
// route for the home page
app.get("/", function(req, res){
res.status(200).render('index');
});
// a catch-all route if someone visits a page that does not exist
app.use("*", function(req, res){
res.status(404).render('fourOhFour');
});
}

32
routes/serve-routes.js Normal file
View file

@ -0,0 +1,32 @@
module.exports = function(app, routeHelpers, lbryApi, ua, googleAnalyticsId){
// route to fetch one free public claim
app.get("/:name/:claim_id", function(req, res){
ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name/claimId", req.params.name + "/" + req.params.claim_id).send();
var uri = req.params.name + "#" + req.params.claim_id;
console.log(">> GET request on /" + uri);
// create promise
lbryApi.getClaimBasedOnUri(uri)
.then(function(filePath){
console.log("/:name/:claim_id success.");
res.status(200).sendFile(filePath);
})
.catch(function(error){
console.log("/:name/:claim_id error:", error)
routeHelpers.handleRequestError(error, res);
});
});
// route to fetch one free public claim
app.get("/:name", function(req, res){
ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name", req.params.name).send();
console.log(">> GET request on /" + req.params.name);
// create promise
lbryApi.getClaimBasedOnNameOnly(req.params.name)
.then(function(filePath){
console.log("/:name success.")
res.status(200).sendFile(filePath);
}).catch(function(error){
console.log("/:name error:", error);
routeHelpers.handleRequestError(error, res);
});
});
}

19
routes/show-routes.js Normal file
View file

@ -0,0 +1,19 @@
module.exports = function(app, routeHelpers, lbryApi, ua, googleAnalyticsId){
// route to fetch all free public claims
app.get("/:name/all", function(req, res){
console.log(">> GET request on /" + req.params.name + " (all)");
ua(googleAnalyticsId, {https: true}).event("Show Routes", "/name/all", req.params.name + "/all").send();
// create promise
lbryApi.getAllClaims(req.params.name)
.then(function(orderedFreePublicClaims){
console.log("/:name/all success.");
res.status(200).render('allClaims', { claims: orderedFreePublicClaims });
return;
})
.catch(function(error){
console.log("/:name/all error:", error);
routeHelpers.handleRequestError(error, res);
})
});
}

View file

@ -1,13 +1,4 @@
var path = require('path');
var siofu = require("socketio-file-upload");
var socketHelpers = require('../helpers/socketHelpers.js');
var ua = require('universal-analytics');
var config = require('config');
var googleAnalyticsId = config.get('AnalyticsConfig.googleId');
var visitor = ua(googleAnalyticsId, {https: true});
module.exports = function(app) {
module.exports = function(app, path, siofu, socketHelpers) {
var http = require('http').Server(app);
var io = require('socket.io')(http);
@ -23,7 +14,6 @@ module.exports = function(app) {
})
uploader.on("saved", function(event){
console.log("saved " + event.file.name);
visitor.event("Publish", "Publish Request", event.file.name).send();
if (event.file.success){
socket.emit("publish-status", "file upload successfully completed");
socketHelpers.publish(event.file.meta.name, event.file.pathName, event.file.meta.license, event.file.meta.nsfw, socket)

View file

@ -4,6 +4,16 @@ var bodyParser = require('body-parser');
var path = require('path');
var siofu = require("socketio-file-upload");
var expressHandlebars = require("express-handlebars");
var Handlebars = require('handlebars');
var axios = require('axios');
var config = require('config');
var ua = require('universal-analytics');
var socketHelpers = require('./helpers/socketHelpers.js');
var routeHelpers = require('./helpers/routeHelpers.js');
var lbryApi = require('./helpers/lbryApi.js');
var googleAnalyticsId = config.get('AnalyticsConfig.googleId');
// set port
var PORT = 3000;
@ -19,16 +29,35 @@ app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(siofu.router);
// configure handlebars
app.engine('handlebars', expressHandlebars({defaultLayout: 'main'}));
// configure handlebars & register it with Express app
var hbs = expressHandlebars.create({
defaultLayout: 'main', // sets the default layout
handlebars: Handlebars, // includes basic handlebars for access to that library
helpers: { // define any extra helpers you may need
googleAnalytics: function(){
var googleApiKey = config.get('AnalyticsConfig.googleId')
return new Handlebars.SafeString(`<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', ${googleApiKey}, 'auto');
ga('send', 'pageview');
</script>`);
}
}
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// require express routes
require("./routes/api-routes.js")(app);
require("./routes/html-routes.js")(app);
require("./routes/api-routes.js")(app, axios);
require("./routes/show-routes.js")(app, routeHelpers, lbryApi, ua, googleAnalyticsId);
require("./routes/serve-routes.js")(app, routeHelpers, lbryApi, ua, googleAnalyticsId);
require("./routes/home-routes.js")(app);
// wrap the server in socket.io to intercept incoming sockets requests
var http = require("./routes/sockets-routes.js")(app);
var http = require("./routes/sockets-routes.js")(app, path, siofu, socketHelpers);
// start server
http.listen(PORT, function() {

View file

@ -16,15 +16,6 @@
</head>
<body>
{{{ body }}}
<!-- google analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-60403362-2', 'auto');
ga('send', 'pageview');
</script>
<!-- JQuery -->
<script type="text/javascript" src="/vendors/MDB/js/jquery-3.1.1.min.js"></script>
<!-- Bootstrap tooltips -->
@ -33,5 +24,7 @@
<script type="text/javascript" src="/vendors/MDB/js/bootstrap.min.js"></script>
<!-- MDB core JavaScript -->
<script type="text/javascript" src="/vendors/MDB/js/mdb.min.js"></script>
<!-- google analytics -->
{{ googleAnalytics }}
</body>
</html>

View file

@ -2,7 +2,7 @@
<div class="col-md-12">
<div class="card card-block default-color-dark white-text">
<div class="card-title">
<h1><a class="white-text" href="https://spee.ch">Spee.ch</a></h1>
<h1><a class="white-text" href="/">Spee.ch</a></h1>
</div>
<p>Spee.ch is a single-serving site that reads and publishes images to and from the <a class="white-text" href="https://lbry.io">LBRY</a> blockchain.</p>
</div>