spee.ch/server.js

70 lines
2.4 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 googleAnalyticsId = config.get('AnalyticsConfig.googleId');
2017-06-17 04:31:31 +02:00
var hostedContentPath = config.get('Database.PublishUploadPath');
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();
//require our models for syncing
var db = require("./models");
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
require("./routes/api-routes.js")(app);
require("./routes/show-routes.js")(app, ua, googleAnalyticsId);
require("./routes/serve-routes.js")(app, ua, googleAnalyticsId);
2017-06-13 20:00:50 +02:00
require("./routes/home-routes.js")(app);
2017-05-25 07:50:02 +02:00
// require socket.io routes
var http = require("./routes/sockets-routes.js")(app, siofu, hostedContentPath, ua, googleAnalyticsId);
2017-05-25 07:50:02 +02:00
// sync sequelize
// wrap the server in socket.io to intercept incoming sockets requests
// start server
db.sequelize.sync({})
.then(function(){
http.listen(PORT, function() {
console.log("Listening on PORT " + PORT);
});
});