This repository has been archived by the owner on Jun 22, 2020. It is now read-only.
forked from gcollazo/ember-addons-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (53 loc) · 1.63 KB
/
server.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
var express = require('express'),
app = express(),
cors = require('cors'),
compression = require('compression'),
DB = require('./lib/db'),
dotenv = require('dotenv'),
csv = require('to-csv');
// load vars
dotenv.load();
app.use(compression());
app.use(cors());
app.use(function(req, res, next) {
req.db = new DB({
databaseURL: process.env.DATABASE_URL,
debug: true
});
next();
});
app.get('/', function(req, res) {
res.redirect(301,
'https://' + process.env.AWS_BUCKET_NAME + '.s3.amazonaws.com/' + process.env.PLUGIN_JSON_FILENAME);
});
app.get('/stats', function(req, res) {
req.db.getMetric('total')
.then(function(rows) {
res.json(rows);
req.db.close();
})
.catch(function(err) {
console.error(err);
res.status(500).json({error: 'error'});
req.db.close();
});
});
app.get('/stats.csv', function(req, res) {
req.db.getMetric('total')
.then(function(rows) {
res.set('Content-Type', 'text/csv').send(csv(rows));
req.db.close();
})
.catch(function(err) {
console.error(err);
res.status(500).json({error: 'error'});
req.db.close();
});
});
app.get('/graph', function(req, res) {
res.redirect(301,
'http://www.charted.co/?{%22dataUrl%22:%22http://ember-addons-server.herokuapp.com/stats.csv%22,%22charts%22:[{%22type%22:%22line%22,%22title%22:%22Ember%20Addons%22,%22note%22:%22This%20chart%20shows%20the%20number%20of%20available%20addons%20for%20ember-cli%20on%20npmjs.org.%20http://emberaddons.com%22}]}');
});
app.listen(process.env.PORT, function() {
console.log('listening on port %d', process.env.PORT);
});