-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
105 lines (88 loc) · 3.56 KB
/
server.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
var restify = require('restify');
var bunyan = require('bunyan');
function createServer (opts) {
if (opts) {
opts.handleUpgrades = true;
}
var logger = bunyan.createLogger({ name: 'provisioner', level: 'info'
, serializers: {
req: bunyan.stdSerializers.req,
// res: restify.bunyan.serializers.response
}
});
if (!'log' in opts) {
opts.log = logger;
}
var server = restify.createServer(opts);
server.on('after', restify.auditLogger({
log: bunyan.createLogger({
name: 'audit',
level: 'info',
stream: process.stdout
})
}));
server.use(restify.dateParser());
server.use(restify.queryParser());
server.use(restify.jsonp());
server.use(restify.gzipResponse());
server.use(restify.bodyParser());
server.use(function (req, res, next) {
req.db = opts.db;
next( );
});
var admin = require('./lib/admin/')(opts);
var account = require('./lib/account/')(opts);
var user = require('./lib/user/')(opts);
var sites = require('./lib/sites/')(opts);
server.get('/dbs', admin.list, admin.format);
server.get('/db/:db', function (req, res, next) {
var q = { name: req.params.db };
console.log('fetch', q);
req.db.models.Account.findOne(q, function (err, docs) {
if (err) { return next(err); }
res.send(docs || err);
next( );
});
});
server.post('/accounts', account.suggest, account.create, account.format);
server.get('/accounts', account.list, account.format);
server.get('/accounts/:account', account.by_account, account.format);
server.del('/accounts/:account', admin.adminUser, account.by_account, account.remove_by_account, account.format);
server.get('/accounts/:account/dbs', account.by_account, function (req, res, next) { });
server.get('/accounts/:account/users', account.by_account, user.list, user.format);
server.get('/accounts/:account/users/:user', account.by_account, user.by_id, user.format);
server.del('/accounts/:account/users/:user', admin.adminUser, account.by_account, user.by_id, user.remove_by_user, user.format);
// server.post('/accounts/:account/users/:user/password', account.by_account, user.by_id, user.format);
server.post('/accounts/:account/users', account.by_account, admin.adminUser, user.suggest, user.create, user.format);
server.post('/accounts/:account/users/:name', account.by_account, admin.adminUser, user.suggest, user.create, user.format);
// server.get('/accounts/:account/sites', account.by_account, user.list, sites.format);
server.get('/accounts/:account/sites/:site', account.by_account, sites.find_by_name, sites.query_multienv, sites.format);
server.del('/accounts/:account/sites/:site', account.by_account, sites.find_by_name, admin.adminUser, sites.remove_by_site, sites.format);
server.post('/accounts/:account/sites', account.by_account, admin.adminUser
, user.suggest
, user.lookup_prexists
, user.create
, sites.suggest
, sites.create
, sites.format);
server.post('/accounts/:account/sites/:name', account.by_account, admin.adminUser
, user.suggest
, user.lookup_prexists
, user.create
, sites.suggest
, sites.create
, sites.format);
return server;
}
exports = module.exports = createServer;
if (!module.parent) {
var env = require('./env');
var bootevent = require('./lib/bootevent');
bootevent(env).boot(function (ctx) {
var server = createServer(ctx);
console.log('ok', 'booted', ctx);
server.listen(env.PORT, function ( ) {
console.log('listening on', server.address( ));
});
});
}