-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
88 lines (71 loc) · 2.38 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
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
var port = process.env.PORT || 3000;
var apiUrl = process.env.API_URL;
const url = require('url');
var express = require('express');
var compression = require('compression');
var app = express();
app.use(compression());
app.use(express.static(__dirname + '/dist'));
const GitHubCache = require('./gitHubCache');
let cache = null;
app.get('/cached-api/:owner/:project', function (req, res) {
cache.getStats(req.params.owner, req.params.project).then((stats) => {
res.send(stats);
}).catch((err) => {
res.status(err.statusCode).send(err.statusMessage);
});
});
// ####### MOCK API ##########
let stats = {
'as-ideas': {
'crowdsource': {
name: "crowdsource",
owner: {
avatar_url: "https://avatars1.githubusercontent.com/u/6329093?v=3",
},
html_url: "https://github.com/as-ideas/crowdsource",
stargazers_count: 5,
forks_count: 3,
watchers_count: 6
},
'bla-bla-bla': {
name: "bla-bla-bla",
owner: {
avatar_url: "https://avatars1.githubusercontent.com/u/6329093?v=3",
},
html_url: "https://github.com/as-ideas/crowdsource",
stargazers_count: 9,
forks_count: 2,
watchers_count: 3
}
},
'bild-de': {
'videoplayer': {
name: "videoplayer",
owner: {
avatar_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Logo_BILD.svg/2000px-Logo_BILD.svg.png",
},
html_url: "https://github.com/as-ideas/crowdsource",
stargazers_count: 12,
forks_count: 23,
watchers_count: 16
}
}
}
app.get('/repos/:owner/:project', function (req, res) {
if (stats[req.params.owner] && stats[req.params.owner][req.params.project]) {
res.send(stats[req.params.owner][req.params.project]);
} else {
res.status(404).send('id not found');
}
});
// ###########################
app.listen(port, function () {
console.log('server is now starting on port ', port);
let gitHubApiUrl = apiUrl || 'http://' + this.address().address + ':' + this.address().port; // Use mock API on dev
// initialize GitHubCache
cache = new GitHubCache({
url: url.parse(gitHubApiUrl),
apiToken: process.env.API_TOKEN
});
});