-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.js
57 lines (48 loc) · 1.45 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
/**
* Created by axetroy on 17-4-26.
*/
const express = require('express');
const generateSvg = require('./badge');
const app = express();
const PORT = process.env.PORT || 3000;
const CONFIG = require('./config');
const axios = require('axios');
const http = axios.create({
baseURL: 'https://api.github.com',
timeout: 1000 * 10,
params: {
client_id: CONFIG.github_client_id,
client_secret: CONFIG.github_client_secret,
},
withCredentials: false,
responseType: 'json',
headers: { Accept: 'application/json;charset=utf-8' },
});
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
app.get('/:owner/:repo.svg', function(req, res) {
const { owner, repo } = req.params;
res.header('Content-Type', 'image/svg+xml;charset=utf-8');
res.header('Cache-Control', 'no-cache, no-store, must-revalidate');
res.header('Pragma', 'no-cache');
res.header('Expires', 0);
http
.get(`https://api.github.com/repos/${owner}/${repo}`)
.then(function(response) {
res.status(200).send(generateSvg(response.data.size * 1024));
})
.catch(function(err) {
res.status(200).send(generateSvg());
});
});
app.get('*', function(req, res) {
res.send(
'Please try this address format: https://github-size-badge.herokuapp.com/:owner/:repo.svg'
);
});
app.listen(PORT, function() {
console.log(`Listen the port: ${PORT}`);
});