1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-28 12:12:26 -05:00
kaiwa/server.js

118 lines
3.4 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');
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());
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,
2013-06-03 18:51:30 -04:00
libraries: [
2013-08-30 01:37:24 -04:00
__dirname + '/clientapp/libraries/zepto.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');
var pkginfo = JSON.parse(fs.readFileSync('./package.json'));
var cacheManifest = fs.readFileSync('clientapp/templates/misc/manifest.cache', 'utf-8');
cacheManifest = cacheManifest
.replace('#{version}', pkginfo.version + ' ' + Date.now())
.replace('#{jsFileName}', '/' + clientApp.jsFileName())
.replace('#{cssFileName}', '/' + clientApp.cssFileName());
fs.writeFileSync('./public/x-manifest.cache', cacheManifest);
};
}
2013-09-05 19:53:23 -04:00
app.set('view engine', 'jade');
app.get('/login', function (req, res) {
res.render('login');
});
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) {
fs.readFile('./public/x-manifest.webapp', function doneReadingManifest(err, manifestContents) {
if (err) return next(err);
res.set('Content-Type', 'application/x-web-app-manifest+json');
res.send(manifestContents);
});
2013-10-12 01:39:30 -04:00
});
app.get('/manifest.cache', function (req, res, next) {
fs.readFile('./public/x-manifest.cache', function doneReadingManifestCache(err, manifestCacheContents) {
if (err) return next(err);
res.set('Content-Type', 'text/cache-manifest');
res.send(manifestCacheContents);
});
});
2013-06-03 18:51:30 -04:00
// serves app on every other url
app.get('*', clientApp.html());
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-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);