-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
147 lines (126 loc) · 5.03 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
147
'use strict';
var restify = require('restify'),
mongoose = require('mongoose'),
swagger = require('swagger-node-restify'),
swaggerModels = {},
corsMiddleware = require('restify-cors-middleware'),
urlExtParser = require('./lib/parsers/urlext-parser'),
filterUUID = require('./lib/util/filter-uuid'),
tokenAuth = require('./lib/auth/token-auth'),
routeAuth = require('./lib/auth/route-auth'),
validateCaptcha = require('./lib/auth/validate-captcha'),
userAliasParser = require('./lib/parsers/user-alias-parser'),
workers = require('./lib/workers'),
config = require('./config.json'),
fs = require('fs-extra'),
Promise = require('bluebird'),
errorReporter = require('./lib/util/error-reporter');
var cors = corsMiddleware({
preflightMaxAge: 5, //Optional
origins: config.frontend.origins,
credentials: config.frontend.credentials,
allowHeaders: ['Authorization', 'Basic']
});
mongoose.Promise = Promise;
Promise.promisifyAll(fs);
Promise.coroutine(function* () {
var path = require('path'),
version = require("./package.json").version;
if (typeof config !== 'object') {
throw new Error('Server has not been configured yet. Please run bin/setup.');
}
if (config.airbrake) {
errorReporter.init(config.airbrake);
}
if (config.workers.use_frontend) {
workers.startFrontend(config.workers.frontend_port);
}
yield fs.mkdirpAsync(path.resolve(path.join(config.file_storage.path, 'photos')));
yield fs.mkdirpAsync(path.resolve(path.join(config.file_storage.path, 'thumbs')));
yield fs.mkdirpAsync(path.resolve('tmp'));
var dburl = 'mongodb://' +
config.mongodb.host + ':' +
config.mongodb.port + '/' +
config.mongodb.dbname;
function addModel(name, data) {
mongoose.model(name, data[name]);
if (data.SwaggerSpec) {
let spec = data.SwaggerSpec;
spec.id = name;
swaggerModels[name] = data.SwaggerSpec;
}
}
mongoose.connect(dburl, {
useMongoClient: true
});
addModel('User', require('./models/user'));
addModel('ApiKey', require('./models/api-key'));
addModel('AccessToken', require('./models/access-token'));
addModel('AclEntry', require('./models/acl-entry'));
addModel('Captcha', require('./models/captcha'));
addModel('Comment', require('./models/comment'));
addModel('Location', require('./models/location'));
addModel('Message', require('./models/message'));
addModel('Region', require('./models/region'));
addModel('Photo', require('./models/photo'));
addModel('Post', require('./models/post'));
var server = restify.createServer({
name: `Leerstandsmelder API Server v${version}`,
version: version
});
restify.defaultResponseHeaders = function() {
this.header('Content-type', 'application/json; encoding=utf-8');
};
server.pre(cors.preflight);
server.pre(restify.pre.userAgentConnection());
server.pre(urlExtParser());
// server.use(cors.actual);
// preflightEnabler(server, {headers: ['Authorization', 'Basic']});
server.use(restify.plugins.fullResponse());
server.use(restify.plugins.gzipResponse());
server.use(restify.plugins.authorizationParser());
server.use(tokenAuth());
server.use(routeAuth);
server.use(userAliasParser());
server.use(restify.plugins.bodyParser());
server.use(restify.plugins.queryParser());
server.use(filterUUID());
server.use(validateCaptcha());
var routes = require('./lib/routes');
yield routes.init([
require('./routes/captchas'),
require('./routes/comments'),
require('./routes/locations'),
require('./routes/messages'),
require('./routes/regions'),
require('./routes/ping'),
require('./routes/photos'),
require('./routes/posts'),
require('./routes/users')
]);
swagger.setAppHandler(server);
swagger.configureSwaggerPaths("", "/api-docs", "");
swagger.addModels({ models: swaggerModels });
yield Promise.map(Object.keys(routes.paths), function (rPath) {
return Promise.map(Object.keys(routes.paths[rPath]), function (method) {
var routeType = routes.paths[rPath][method].overrideVerb || method,
route = {
spec: routes.paths[rPath][method].spec || {},
action: routes.paths[rPath][method].controller
};
if (!route.spec.path) {
route.spec.path = rPath;
}
if (!route.spec.nickname) {
route.spec.nickname = method + route.spec.type;
}
swagger["add" + routeType.toUpperCase()](route);
});
});
let apiConf = config.api_server,
apiUrl = `${apiConf.secure ? "https" : "http"}://${apiConf.host}:${apiConf.port}`;
swagger.configure(apiUrl, version);
server.listen(config.api_server.port, config.api_server.host, function () {
console.log(`${server.name} listening at ${server.url}`);
});
})();