-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
178 lines (160 loc) · 5.59 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var express = require('express')
, methodOverride = require('method-override')
, bodyParser = require('body-parser')
, logger = require('morgan')
, favicon = require('serve-favicon')
, path = require('path')
, nunjucks = require('nunjucks')
, bodyParser = require('body-parser')
, methodOverride = require('method-override')
, routes = require('./routes')
;
var app = express();
var redirect_base = 'http://frictionlessdata.io'
//CORS middleware
var CORSSupport = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
app.set('port', process.env.PORT || 5000);
app.set('views', __dirname + '/views');
// app.set('view engine', 'html');
// app.engine('html', require('hbs').__express);
// app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride());
app.use(CORSSupport);
app.use(express.static(path.join(__dirname, 'public')));
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader('views'));
env.express(app);
env.addFilter('truncateHomepage', function (str) {
return str.replace(/^https?:\/\//, '');
});
// middleware to add trailing slash
app.use(function(req, res, next) {
if(req.url.substr(-1) === '/' && req.url.length > 1) {
res.redirect(301, req.url.slice(0, req.url.length-1));
}
else {
next();
}
});
app.get('/', redirect('/'));
app.get('/contact', function(req, res) {
res.redirect(redirect_base + '/get-involved/#contact');
});
app.get('/about/contribute', function(req, res) {
res.redirect('/contribute');
});
app.get('/contribute', function(req, res) {
res.redirect(redirect_base + '/get-involved/');
});
app.get('/publish', redirect('/docs/data-packages/publish'));
app.get('/roadmap', redirect('/docs'));
app.get('/roadmap/core-datasets', function(req, res) {
res.render('core-datasets.html', {title: 'Core Datasets'})
});
app.get('/vision', function(req, res) {
res.redirect(redirect_base + '/about/');
});
// Standards and patterns
app.get('/standards', redirect('/docs/data-packages'));
app.get('/standards/data-package', function(req, res) {
res.redirect(redirect_base + '/data-packages/');
});
app.get('/standards/simple-data-format', function(req, res) {
res.redirect(redirect_base + '/guides/tabular-data-package/');
});
app.get('/standards/csv', function(req, res) {
res.redirect(redirect_base + '/guides/csv/');
});
// Docs (patterns, standards etc) - new redirects for datahub.io
app.get('/doc', redirect('/docs'))
app.get('/doc/core-data-curators', redirect('/docs/core-data/curators'))
app.get('/doc/data-package', redirect('/docs/data-packages'))
app.get('/doc/csv', redirect('/docs/data-packages/csv'))
app.get('/doc/publish-any', redirect('/docs/data-packages/publish-any'))
app.get('/doc/publish-faq', redirect('/docs/data-packages/publish-faq'))
app.get('/doc/publish-geo', redirect('/docs/data-packages/publish-geo'))
app.get('/doc/publish-online', redirect('/docs/data-packages/publish-online'))
app.get('/doc/publish-tabular', redirect('/docs/data-packages/publish-tabular'))
app.get('/doc/publish', redirect('/docs/data-packages/publish'))
app.get('/doc/tabular-data-package', redirect('/docs/data-packages/tabular'))
app.get('/doc/:page', redirect('/docs'))
// Tools
app.get('/tools', redirect('/'));
app.get('/tools/create', redirectOld('http://datapackagist.okfnlabs.org/'));
app.get('/tools/validate.json', routes.toolsDpValidateJSON);
app.get('/tools/validate', redirect('/tools/validate'));
app.get('/tools/view', routes.toolsDpView);
app.get('/tools/dataproxy', routes.toolsDataProxy);
// Data
app.get('/data', routes.data);
app.get('/data.json', routes.dataJson);
app.get('/data/search', routes.dataSearch);
// new redirects for datahub.io
app.get(['/data/core', '/data/core/*'], redirect('/core'))
app.get(['/data/datasets', '/data/datasets/*'], redirect('/core'))
app.get('/data/*', function(req, res) {
var pathParts = req.params[0].split('/')
if (pathParts.length === 1) {
res.redirect(302, 'https://datahub.io/core/' + pathParts[0])
} else {
res.redirect(302, 'https://datahub.io')
}
}) // end of redirects for datahub.io
// backwards compatibility
app.get('/data/:name/datapackage.json', function(req, res) {
res.redirect('/data/core/' + req.params.name + '/datapackage.json');
});
// more data stuff
app.get('/data/:owner/:id/datapackage.json', routes.dataPackageShowJSON);
// data "API"
app.get('/data/:owner/:id/r/:name', routes.dataShow);
app.get('/data/:owner/:id', routes.dataPackageShow);
app.get('/data/:owner', routes.communityUser);
// Admin
app.get('/admin/reload', routes.adminReload);
// OLD / OBSOLETE
// Community redirect
app.get('/community/:username/:repo', function(req, res) {
// hard-coded redirects
var repo = req.params.repo;
var changes = {
'publicbodies': 'public-bodies',
'dataset-gla': 'os-gb-local-gla'
};
if (repo in changes) {
repo = changes[repo];
}
var url = '/data/' + req.params.username + '/' + repo;
res.redirect(url);
});
function redirect(path) {
return function(req, res) {
var dest = 'https://datahub.io' + path;
if (req.params[0]) {
dest += '/' + req.params[0]
}
res.redirect(302, dest);
}
}
function redirectOld(url) {
return function(req, res) {
res.redirect(url);
}
}
routes.bootApp(function(err) {
if (err) {
console.error(err);
throw err;
}
app.listen(app.get('port'), function() {
console.log("Listening on " + app.get('port'));
});
});
exports.app = app;