-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp.js
146 lines (122 loc) · 4.14 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
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
/**
* ZRECore.js - A REST-ful set of commerce related data models.
* @author ZRECommerce
* @license GPL v3 or higher.
*/
/**
* Module dependencies.
*/
var port = 8080;
var useSSL = false;
var sslCertificatePath = '';
var sslKeyPath = '';
var sslCaPath = '';
var databaseHost = 'localhost';
var databaseName = 'zrecore';
var authorizationRequired = false; // Set to false while you add your first User, then set back to true and restart the app.
var http = require('http')
, path = require('path')
, fs = require('fs')
, restify = require('restify')
, _ = require('underscore');
var ZRECORE_DIR = __dirname;
var crud = require(ZRECORE_DIR + '/lib/crud.js');
// Load config if found
try {
// Found. Load them.
var config = require(ZRECORE_DIR + '/_config.js')
if (!_.isUndefined(config.port)) port = config.port;
if (!_.isUndefined(config.useSSL)) useSSL = config.useSSL;
if (!_.isUndefined(config.sslCertificatePath)) sslCertificatePath = config.sslCertificatePath;
if (!_.isUndefined(config.sslKeyPath)) sslKeyPath = config.sslKeyPath;
if (!_.isUndefined(config.sslCaPath)) sslCaPath = config.sslCaPath;
if (!_.isUndefined(config.databaseHost)) databaseHost = config.databaseHost;
if (!_.isUndefined(config.databaseName)) databaseName = config.databaseName;
if (!_.isUndefined(config.authorizationRequired)) authorizationRequired = config.authorizationRequired;
console.log('Loaded config.');
} catch (e) {
// Not found. Continue
}
var server;
// ...Do we want to enable SSL support?
if ( useSSL == true) {
server = restify.createServer({
certificate: fs.readFileSync(sslCertificatePath),
key: fs.readFileSync(sslKeyPath),
ca: fs.readFileSync(sslCaPath),
requestCert: true,
rejectUnauthorized: true
});
} else {
server = restify.createServer({
});
}
// ...Set up our connection to the database.
var mongoose = require("mongoose");
mongoose.connect('mongodb://' + databaseHost + '/' + databaseName);
try {
console.log( 'Try to fix mquery issue' );
delete mongoose.__proto__.mquery.permissions.count;
console.log( 'mquery fix applied to this instance' );
} catch (ex) {
console.log( 'Cannot fix count with sort issue from mquery');
}
/**
* Set up our REST controllers.
*/
// ...Enforce API authentication
server.use(restify.fullResponse());
server.use(restify.bodyParser());
server.use(restify.queryParser());
if (authorizationRequired) {
server.pre(function (req, res, next) {
var api_user = req.header('API-USER');
var api_key = req.header('API-KEY');
var api_version = req.header('API-VERSION');
if ( _.isUndefined( api_user ) || _.isUndefined(api_key) ) {
res.send(401, '401 Access Denied');
} else {
// ...Attempt to look it up.
var User = require('./models/User.js');
var authUser = User.findOne({"handle": api_user}, function (err, doc) {
if (err) {
console.error(err.toString());
res.send(500, 'Internal server error.');
} else {
if (doc) {
doc.comparePassword(api_key, function (err, isMatch) {
if (err || !isMatch) {
res.send(401, '401 Access Denied');
} else {
// OK! ...Just continue execution.
next();
}
});
} else {
// User not found
res.send(401, '401 Access Denied');
}
}
});
}
});
}
var routeFiles = fs.readdirSync( ZRECORE_DIR + '/routes');
var route = '';
var models = [];
for (var r = 0; r < routeFiles.length; r++) {
route = path.basename(routeFiles[r], '.js');
if (route != '') models.push(route);
}
var model = null;
for (var i = 0; i < models.length; i++) {
console.log('...Routing ' + models[i]);
model = require('./routes/' + models[i]);
crud.setUpServer(server, '/' + models[i], model);
}
/**
* Ok! Let's start the show.
*/
server.listen(port, function () {
console.log('Server is running on port ' + port);
});