1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-22 01:02:23 -05:00
kaiwa/server.js

171 lines
5.1 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');
2013-08-20 13:45:06 -04:00
var Moonboots = require('moonboots');
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 oembed = require('oembed');
var async = require('async');
2013-06-03 18:51:30 -04:00
2013-08-29 23:38:28 -04:00
var app = express();
2013-10-12 01:39:30 -04:00
2013-08-29 23:38:28 -04:00
app.use(express.compress());
2013-06-03 18:51:30 -04:00
app.use(express.static(__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());
2014-01-02 03:52:26 -05:00
oembed.EMBEDLY_URL = config.embedly.url || 'https://api.embed.ly/1/oembed';
2014-01-01 19:24:11 -05:00
oembed.EMBEDLY_KEY = config.embedly.key;
2013-06-03 18:51:30 -04:00
var clientApp = new Moonboots({
2013-08-29 23:38:28 -04:00
main: __dirname + '/clientapp/app.js',
2013-10-12 01:39:30 -04:00
templateFile: __dirname + '/clientapp/templates/main.html',
2013-09-27 03:27:35 -04:00
developmentMode: config.isDev,
2014-01-06 23:00:16 -05:00
cachePeriod: 0,
2013-06-03 18:51:30 -04:00
libraries: [
2014-01-01 19:24:11 -05:00
__dirname + '/clientapp/libraries/jquery.js',
2013-09-12 14:18:44 -04:00
__dirname + '/clientapp/libraries/ui.js',
2013-09-18 19:24:40 -04:00
__dirname + '/clientapp/libraries/resampler.js',
2013-09-27 01:52:54 -04:00
__dirname + '/clientapp/libraries/IndexedDBShim.min.js'
2013-08-29 23:38:28 -04:00
],
2013-10-15 03:02:45 -04:00
browserify: {
debug: false
},
2013-08-29 23:38:28 -04:00
stylesheets: [
2013-09-16 05:19:07 -04:00
__dirname + '/public/css/otalk.css'
2013-06-03 18:51:30 -04:00
],
2013-10-12 01:39:30 -04:00
server: app
2013-06-03 18:51:30 -04:00
});
2013-10-12 01:39:30 -04:00
if (config.isDev) {
clientApp.config.beforeBuildJS = function () {
2013-10-12 01:39:30 -04:00
var clientFolder = __dirname + '/clientapp';
templatizer(clientFolder + '/templates', clientFolder + '/templates.js');
};
}
clientApp.on('ready', function () {
2014-01-06 23:34:25 -05:00
console.log('Client app ready');
var pkginfo = JSON.parse(fs.readFileSync(__dirname + '/package.json'));
2014-01-06 22:44:02 -05:00
2014-01-06 23:34:25 -05:00
var manifestTemplate = fs.readFileSync(__dirname + '/clientapp/templates/misc/manifest.cache', 'utf-8');
var cacheManifest = manifestTemplate
.replace('#{version}', pkginfo.version + config.isDev ? ' ' + Date.now() : '')
2014-01-06 23:15:56 -05:00
.replace('#{jsFileName}', clientApp.jsFileName())
.replace('#{cssFileName}', clientApp.cssFileName());
2014-01-06 23:34:25 -05:00
console.log('Cache manifest generated');
2014-01-06 22:44:02 -05:00
app.get('/manifest.cache', function (req, res, next) {
res.set('Content-Type', 'text/cache-manifest');
2014-01-06 23:15:56 -05:00
res.set('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
res.send(cacheManifest);
});
2014-01-06 23:21:04 -05:00
// serves app on every other url
app.get('*', clientApp.html());
});
2014-01-06 22:44:02 -05:00
var webappManifest = fs.readFileSync('./public/x-manifest.webapp');
2014-01-06 22:44:02 -05:00
2013-09-05 19:53:23 -04:00
app.set('view engine', 'jade');
app.get('/login', function (req, res) {
res.render('login');
});
2013-09-05 19:53:23 -04:00
app.get('/logout', function (req, res) {
res.render('logout');
});
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
});
2014-01-01 19:24:11 -05:00
app.get('/oembed', function (req, res) {
var callback = req.query.callback;
if (req.query.url) {
oembed.fetch(req.query.url, req.query, function (err, result) {
if (err || !result) {
return res.status(500).send();
}
res.status(200);
res.set('Content-Type', oembed.MIME_OEMBED_JSON);
if (callback) {
res.send(callback + '(' + JSON.stringify(result) + ')');
} else {
res.send(JSON.stringify(result));
}
});
} else if (req.query.urls) {
var cache = {};
var urls = req.query.urls.split(',');
delete req.query.urls;
async.forEach(urls, function (url, cb) {
oembed.fetch(url, req.query, function (err, result) {
if (err || !result) {
result = {type: 'error'};
}
cache[url] = result;
cb();
});
}, function () {
res.status(200);
var results = [];
urls.forEach(function (url) {
results.push(cache[url]);
});
if (callback) {
res.set('Content-Type', 'application/javascript');
res.send(callback + '(' + JSON.stringify(results) + ')');
} else {
res.set('Content-Type', 'application/json');
res.send(JSON.stringify(results));
}
});
} else {
res.status(400).send();
}
});
2013-08-29 23:38:28 -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
});
//https.createServer({
// key: fs.readFileSync(config.http.key),
// cert: fs.readFileSync(config.http.cert)
//}, app).listen(config.http.port);
app.listen(config.http.port);
2013-08-20 13:45:06 -04:00
console.log('demo.stanza.io running at: ' + config.http.baseUrl);