forked from dchester/showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (83 loc) · 2.38 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
var http = require('http');
var express = require('express');
var path = require('path');
var swig = require('swig');
var consolidate = require('consolidate');
var Dreamer = require('dreamer');
var async = require('async');
var tamejs = require('tamejs').register();
var flash = require('connect-flash');
var config = require('config');
var mkdirp = require('mkdirp');
var armrest = require('armrest');
var app = express();
var views = __dirname + '/views';
swig.init({ root: views, allowErrors: true });
var errorHandler = function(req, res, next) {
req.error = function(error) {
if (arguments.length == 2) {
var status = arguments[0];
var error = arguments[1];
} else {
var status = 500;
var error = arguments[0];
}
try {
var response = JSON.parse(JSON.stringify(error));
response.message = error.toString();
} catch(e) {
var response = {};
}
res.json(status, response);
};
next();
};
var flashLoader = function(req, res, next) {
var _render = res.render;
res.render = function() {
res.locals.messages = req.flash();
_render.apply(res, arguments);
};
next();
};
var storagePath;
if (!config.files.storage_path.match(/^\//)) {
storagePath = path.join(__dirname, config.files.storage_path);
} else {
storagePath = config.files.storage_path;
}
mkdirp.sync(config.files.storage_path + "/files");
app.configure(function(){
app.engine('.html', consolidate.swig);
app.set('view engine', 'html');
app.set('views', views);
app.set('port', process.env.PORT || config.port || 3000);
app.use(errorHandler);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('__SECRET__'));
app.use('/admin', express.session());
app.use(flash());
app.use(flashLoader);
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(storagePath));
});
var dreamer = Dreamer.initialize({
app: app,
schema: "spec/schema.md"
});
app.dreamer = dreamer;
app.get('/', function(req, res) {
res.redirect("/admin/entities");
});
app.get ('/admin/fields', function(req, res) {
var fields = require('./lib/fields');
res.render("fields.html", { fields: fields.controls });
});
require('./routes/entity.tjs').initialize(app);
require('./routes/item.tjs').initialize(app);
require('./routes/api.tjs').initialize(app);
dreamer.dream();