-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
154 lines (123 loc) · 4.24 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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 flash = require('connect-flash');
var mkdirp = require('mkdirp');
var armrest = require('armrest');
var util = require('util');
var router = require('./lib/gx-express-router');
var gx = require('gx');
var passport = require('passport');
var strategy = require('./lib/passport-strategy');
var User = require('./lib/user');
var app = express();
app.showcase = {};
var views = __dirname + '/views';
swig.init({ root: views, allowErrors: true });
var externalMiddleware = [];
exports.middleware = function(fn) {
externalMiddleware.push(fn);
};
exports.initialize = function(config) {
config = config || {};
config.files = config.files || {};
config.auth = config.auth || {};
if (!config.files.tmp_path) {
throw new Error("please specify files.tmp_path in config");
}
if (!config.files.storage_path) {
throw new Error("please specify files.storage_path in config");
}
var secret = config.secret;
if (!secret) {
console.warn("falling back to default session secret; please send a secret to showcase.initialize");
secret = "arthur is fond of jimz";
}
var dreamer = Dreamer.initialize({
app: app,
schema: __dirname + "/spec/schema.md",
resources: __dirname + "/spec/resources.md",
fixtures: __dirname + "/spec/fixtures.md",
database: config.database
});
app.dreamer = dreamer;
config.auth.passport_strategy = config.auth.passport_strategy || strategy.local;
passport.use(config.auth.passport_strategy);
var storagePath;
if (!config.files.storage_path.match(/^\//)) {
var relativeBase = path.dirname(require.main.filename);
storagePath = path.join(relativeBase, config.files.storage_path);
} else {
storagePath = config.files.storage_path;
}
mkdirp.sync(config.files.storage_path + "/files");
var File = require('./lib/file');
File.methods(config.files);
var middleware = require('./lib/middleware').initialize(app);
app.showcase.middleware = middleware;
app.showcase.config = config;
app.configure(function(){
app.engine('.html', consolidate.swig);
app.set('router', router);
app.set('view engine', 'html');
app.set('views', views);
app.set('port', process.env.PORT || config.port || 3000);
app.use(middleware.errorHandler);
app.use(express.static(path.join(__dirname, 'public')), { maxAge: 600 });
app.use(express.static(storagePath));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('__SECRET__'));
app.use('/files', express.cookieSession({ secret: secret }));
app.use('/workspaces', express.cookieSession({ secret: secret }));
app.use('/admin', express.cookieSession({ secret: secret }));
app.use(flash());
app.use(middleware.flashLoader);
app.use(passport.initialize());
app.use(middleware.sessionLocalizer);
app.use(middleware.setupChecker);
externalMiddleware.forEach(function(fn) { fn(app) });
app.use(middleware.fixturesLoader);
app.use(app.router);
});
app.get('/', function(req, res) {
res.redirect("/workspaces");
});
require('./routes/setup.js').initialize(app);
require('./routes/workspaces.js').initialize(app);
require('./routes/collection.js').initialize(app);
require('./routes/item.js').initialize(app);
require('./routes/api.js').initialize(app);
require('./routes/users.js').initialize(app);
require('./routes/login.js').initialize(app);
require('./routes/files.js').initialize(app);
var Radio = function() {};
util.inherits(Radio, require('events').EventEmitter);
app.radio = new Radio();
exports.radio = app.radio;
}
var plugins = require('./lib/plugins');
var registerPlugins = function() {
var image_list = require('./plugins/image_list');
plugins.register('field', image_list);
var image = require('./plugins/image');
plugins.register('field', image);
}
exports.plugins = plugins;
exports.registerField = function(field) {
plugins.register('field', field);
};
exports.run = function() {
registerPlugins();
plugins.route(app);
var server = app.dreamer.dream();
return server;
};
exports.app = app;
exports.mergeUser = User.merge;