forked from okfn/dataportals.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
130 lines (110 loc) · 3.12 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
var express = require('express')
, bodyParser = require('body-parser')
, methodOverride = require('method-override')
, morgan = require('morgan')
// , serveFavicon = require('serve-favicon')
, serveStatic = require('serve-static')
, path = require('path')
, fs = require('fs')
, nunjucks = require('nunjucks')
, config = require('./lib/config.js')
, model = require('./lib/model.js')
, routes = require('./routes/index.js')
;
var app = express();
app.set('port', process.env.PORT || 5000);
app.set('views', __dirname + '/views');
app.use(bodyParser.json());
app.use(methodOverride());
app.use(serveStatic(path.join(__dirname, 'public')));
var env = process.env.NODE_ENV || 'development';
if ('development' == env) {
app.use(morgan('dev'));
// app.use(serveFavicon());
}
var env = new nunjucks.Environment(new nunjucks.FileSystemLoader('views'));
env.express(app);
app.get('/', function(req, res) {
var catalogs = model.catalog.query();
var total = catalogs.length;
res.render('home.html', {
catalogs: catalogs,
total: total
});
})
app.get('/about', function(req, res) {
res.render('about.html');
});
app.get('/search', function(req, res) {
var catalogs = model.catalog.query();
var total = catalogs.length;
res.render('search.html', {
catalogs: catalogs,
total: total
});
});
app.get('/add', function(req, res) {
res.render('add.html');
});
app.get('/contribute', function(req, res) {
res.render('contribute.html');
});
app.get('/catalog/:id', function(req, res) {
res.redirect(301, '/portal/' + req.params.id);
});
app.get('/portal/:id', function(req, res) {
var id = req.params.id;
var thiscatalog = model.catalog.get(id)
if (!thiscatalog) {
res.status(404).send('Not Found');
} else {
res.render('catalog.html', {
catalog: thiscatalog
});
}
});
app.get('/group/:id', function(req, res) {
var id = req.params.id;
var catalogs = model.catalog.getGroup(id)
res.render('group.html', {
group: id,
catalogs: catalogs,
total: catalogs.length
});
});
app.get('/api/data.json', function(req, res) {
res.json(model.catalog._cache);
// res.status(status).json(model.catalog._cache);
});
app.get('/api/catalogs/:id', function(req, res) {
var id = req.params.id;
var c = model.catalog.get(id);
console.log(c)
if (!c) {
res.status(404).send('Not Found');
}
res.header("Content-Type", "application/json; charset=utf-8");
res.write(JSON.stringify(c));
res.end();
});
app.get('/api/catalogs', function(req, res) {
var catalogs = model.catalog.query();
res.header("Content-Type", "application/json; charset=utf-8");
res.write(JSON.stringify(catalogs));
res.end();
});
app.get('/api/groups', function(req, res) {
var groups = model.catalog.getGroups();
res.header("Content-Type", "application/json; charset=utf-8");
res.write(JSON.stringify(groups));
res.end();
});
app.get('/admin/reload', routes.reload);
model.catalog.loadUrl(config.databaseUrl, function(err) {
if (err) {
console.error('Failed to load dataset info');
}
app.listen(app.get('port'), function() {
console.log("Listening on " + app.get('port'));
});
});