-
Notifications
You must be signed in to change notification settings - Fork 3
/
legacy-redirect.js
62 lines (54 loc) · 1.97 KB
/
legacy-redirect.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
'use strict';
var restify = require('restify'),
mongoose = require('mongoose'),
config = require('./config.json'),
Promise = require('bluebird'),
version = require("./package.json").version;
mongoose.Promise = Promise;
if (typeof config !== 'object') {
throw new Error('Server has not been configured yet. Please run bin/setup.');
}
var dburl = 'mongodb://' +
config.mongodb.host + ':' +
config.mongodb.port + '/' +
config.mongodb.dbname;
mongoose.connect(dburl);
mongoose.model('Location', require('./models/location').Location);
mongoose.model('Region', require('./models/region').Region);
var server = restify.createServer({
name: `Leerstandsmelder Legacy Redirect Server v${version}`,
version: version
});
server.pre(restify.pre.userAgentConnection());
server.use(restify.fullResponse());
server.use(restify.gzipResponse());
server.use(restify.authorizationParser());
server.get('/:region_slug/places/:location_slug', function (req, res, next) {
var redirect = config.redirect_server.redirect_host;
return mongoose.model('Region').findOne({slug: req.params.region_slug})
.then(function (region) {
if (region) {
redirect += '/' + (region.slug || region.uuid);
return mongoose.model('Location').findOne({legacy_slug: req.params.location_slug});
} else {
throw new Error();
}
})
.then(function (location) {
if (location) {
redirect += '/' + (location.slug || location.uuid);
res.setHeader('Location', redirect);
res.send(301);
next();
} else {
throw new Error();
}
})
.catch(function () {
res.send(restify.NotFoundError());
next();
});
});
server.listen(config.redirect_server.port, config.redirect_server.host, function () {
console.log(`${server.name} listening at ${server.url}`);
});