spee.ch/server.js

67 lines
2.5 KiB
JavaScript
Raw Normal View History

// load dependencies
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
2017-06-01 04:48:09 +02:00
var siofu = require("socketio-file-upload");
2017-06-10 01:46:57 +02:00
var expressHandlebars = require("express-handlebars");
2017-06-13 20:00:50 +02:00
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');
2017-06-14 00:49:58 +02:00
var lbryHelpers = require('./helpers/lbryHelpers.js');
2017-06-13 20:00:50 +02:00
var googleAnalyticsId = config.get('AnalyticsConfig.googleId');
2017-05-25 07:50:02 +02:00
// set port
2017-05-24 23:22:56 +02:00
var PORT = 3000;
2017-05-25 07:50:02 +02:00
// initialize express app
var app = express();
2017-05-25 07:50:02 +02:00
// make express look in the public directory for assets (css/js/img)
app.use(express.static(__dirname + '/public'));
2017-05-25 07:50:02 +02:00
// configure express app
2017-06-10 01:46:57 +02:00
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
2017-06-01 04:48:09 +02:00
app.use(siofu.router);
2017-05-25 07:50:02 +02:00
2017-06-13 20:00:50 +02:00
// 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');
2017-06-13 20:21:42 +02:00
ga('create', '${googleApiKey}', 'auto');
2017-06-13 20:00:50 +02:00
ga('send', 'pageview');
</script>`);
}
}
});
app.engine('handlebars', hbs.engine);
2017-06-10 01:46:57 +02:00
app.set('view engine', 'handlebars');
2017-05-25 07:50:02 +02:00
// require express routes
2017-06-14 00:39:38 +02:00
require("./routes/api-routes.js")(app, routeHelpers, lbryApi);
2017-06-14 00:49:58 +02:00
require("./routes/show-routes.js")(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId);
require("./routes/serve-routes.js")(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId);
2017-06-13 20:00:50 +02:00
require("./routes/home-routes.js")(app);
2017-05-25 07:50:02 +02:00
2017-06-10 01:46:57 +02:00
// wrap the server in socket.io to intercept incoming sockets requests
2017-06-13 21:12:41 +02:00
var http = require("./routes/sockets-routes.js")(app, path, siofu, socketHelpers, ua, googleAnalyticsId);
2017-05-25 07:50:02 +02:00
// start server
2017-05-25 07:50:02 +02:00
http.listen(PORT, function() {
console.log("Listening on PORT " + PORT);
});