kaiwa/server.js

143 lines
4.3 KiB
JavaScript
Raw Normal View History

2013-09-05 19:53:23 -04:00
var fs = require('fs');
var https = require('https');
2013-08-20 13:45:06 -04:00
var express = require('express');
2013-08-29 23:38:28 -04:00
var helmet = require('helmet');
2015-02-09 09:22:17 -05:00
var Moonboots = require('moonboots-express');
2013-08-20 13:45:06 -04:00
var config = require('getconfig');
2013-08-29 23:38:28 -04:00
var templatizer = require('templatizer');
2014-01-01 19:24:11 -05:00
var async = require('async');
2013-06-03 18:51:30 -04:00
2013-08-29 23:38:28 -04:00
var app = express();
2015-02-09 09:22:17 -05:00
var compression = require('compression');
var serveStatic = require('serve-static');
2013-10-12 01:39:30 -04:00
2015-02-09 09:22:17 -05:00
app.use(compression());
app.use(serveStatic(__dirname + '/public'));
2013-08-29 23:38:28 -04:00
if (!config.isDev) {
app.use(helmet.xframe());
}
app.use(helmet.iexss());
app.use(helmet.contentTypeOptions());
var webappManifest = fs.readFileSync('./public/x-manifest.webapp');
2014-01-06 22:44:02 -05:00
2015-02-09 09:22:17 -05:00
app.set('views', __dirname + '/views');
2013-09-05 19:53:23 -04:00
app.set('view engine', 'jade');
app.get('/login', function (req, res) {
res.render("login", {"config": config.server});
2013-09-05 19:53:23 -04:00
});
2013-09-05 19:53:23 -04:00
app.get('/logout', function (req, res) {
res.render('logout');
});
app.get('/config.js', function (req, res) {
2014-12-06 11:21:49 -05:00
res.type('application/javascript');
res.send("var SERVER_CONFIG = " + JSON.stringify(config.server) + ";");
});
2015-01-25 14:21:57 -05:00
app.get('/sounds/*', function (req, res) {
console.log(req.baseUrl);
res.type('audio/wav');
res.redirect("./public" + req.baseUrl);
});
app.get('/oauth/login', function (req, res) {
res.redirect('https://apps.andyet.com/oauth/authorize?client_id=' + config.andyetAuth.id + '&response_type=token');
});
app.get('/oauth/callback', function (req, res) {
res.render('oauthLogin');
});
2013-08-29 23:38:28 -04:00
app.get('/manifest.webapp', function (req, res, next) {
2014-01-06 22:44:02 -05:00
res.set('Content-Type', 'application/x-web-app-manifest+json');
res.send(webappManifest);
2013-10-12 01:39:30 -04:00
});
2013-10-20 18:00:51 -04:00
app.use(function handleError(err, req, res, next) {
2013-10-20 18:44:22 -04:00
var errorResult = {message: 'Something bad happened :('};
2013-12-31 18:02:07 -05:00
2013-10-20 18:00:51 -04:00
if (config.isDev) {
if (err instanceof Error) {
if (err.message) {
errorResult.message = err.message;
}
if (err.stack) {
errorResult.stack = err.stack;
}
}
}
2013-10-20 18:44:22 -04:00
res.status(500);
res.render('error', errorResult);
2013-10-20 18:00:51 -04:00
});
2015-02-09 09:22:17 -05:00
var clientApp = new Moonboots({
moonboots: {
main: __dirname + '/clientapp/app.js',
developmentMode: config.isDev,
libraries: [
__dirname + '/clientapp/libraries/jquery.js',
__dirname + '/clientapp/libraries/ui.js',
__dirname + '/clientapp/libraries/resampler.js',
__dirname + '/clientapp/libraries/IndexedDBShim.min.js',
__dirname + '/clientapp/libraries/sugar-1.2.1-dates.js',
__dirname + '/clientapp/libraries/jquery.oembed.js'
2015-02-09 09:22:17 -05:00
],
browserify: {
debug: false
},
stylesheets: [
__dirname + '/public/css/otalk.css',
__dirname + '/public/css/jquery.oembed.css'
2015-02-09 09:22:17 -05:00
],
beforeBuildJS: function () {
if (config.isDev) {
var clientFolder = __dirname + '/clientapp';
templatizer(clientFolder + '/templates', clientFolder + '/templates.js');
}
}
},
server: app,
cachePeriod: 0,
render: function (req, res) {
res.render('index');
}
});
clientApp.on('ready', function () {
console.log('Client app ready');
var pkginfo = JSON.parse(fs.readFileSync(__dirname + '/package.json'));
var manifestTemplate = fs.readFileSync(__dirname + '/clientapp/templates/misc/manifest.cache', 'utf-8');
var cacheManifest = manifestTemplate
.replace('#{version}', pkginfo.version + config.isDev ? ' ' + Date.now() : '')
.replace('#{jsFileName}', clientApp.moonboots.jsFileName())
.replace('#{cssFileName}', clientApp.moonboots.cssFileName());
console.log('Cache manifest generated');
app.get('/manifest.cache', function (req, res, next) {
res.set('Content-Type', 'text/cache-manifest');
res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
res.send(cacheManifest);
});
// serves app on every other url
app.get('*', function (req, res) {
res.render(clientApp.moonboots.htmlSource());
});
});
//https.createServer({
// key: fs.readFileSync(config.http.key),
// cert: fs.readFileSync(config.http.cert)
//}, app).listen(config.http.port);
2015-02-09 09:22:17 -05:00
app.listen(config.http.port, function () {
console.log('demo.stanza.io running at: ' + config.http.baseUrl);
})