Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 7635922

Browse files
committed
0.5
1 parent 957ff98 commit 7635922

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

cache.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const logger = (require("log4js")).getLogger("Cache");
22
const axios = require("axios");
3+
const conf = require("./config");
34

45
logger.level = "debug";
56
axios.defaults.baseURL = "https://api.github.com";
@@ -36,9 +37,10 @@ class LightCache {
3637
}
3738

3839
class ApiCache extends LightCache {
39-
constructor(token) {
40+
constructor() {
4041
super();
41-
this.token = token;
42+
this.token = conf.token;
43+
this.expiration = conf.expiration;
4244
}
4345
async syncAxios(url) {
4446
logger.debug("Request GitHub API address:", url);
@@ -50,12 +52,12 @@ class ApiCache extends LightCache {
5052
})).data;
5153
}
5254

53-
async requestWithCache(url, expiration=3600) {
55+
async requestWithCache(url) {
5456
// 类似于 Service Worker 缓存机制
5557
return this.asyncGetOrSet(
5658
url,
5759
async () => (await this.syncAxios(url)),
58-
expiration,
60+
this.expiration,
5961
);
6062
}
6163
}

config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
token: process.env.CODE_STATISTIC, // GitHub Access Token (Minimum permissions). Increase QPS of GitHub APIs.
3+
expiration: 3600, // expiration second
4+
requires: ["*"], // CODE STATISTIC can only be parsed for allowed users. ( * indicates that all users are allowed )
5+
}

index.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
const express = require('express');
2-
const cache = new (require('./cache').ApiCache)(process.env.CODE_STATISTIC || "");
2+
const cache = new (require('./cache').ApiCache)();
3+
const conf = require('./config');
34
const app = express();
45

6+
7+
const isAvailableUser = (username) => {
8+
return ( !! username ) && ( conf.requires.includes("*") || conf.requires.includes(username));
9+
}
10+
511
async function getLanguage(user, repo) {
612
return await cache.requestWithCache(`/repos/${user}/${repo}/languages`);
713
}
@@ -22,17 +28,26 @@ async function langStatistics(queue) {
2228
return res;
2329
}
2430

25-
app.get('/user/:user/', async function (req, res) {
26-
const username = req.params.user;
31+
app.get('/:user/', async function (req, res) {
32+
const username = req.params['user'];
33+
if ( ! isAvailableUser(username) ) {
34+
res.send('permission denied');
35+
return;
36+
}
2737
const response = await cache.requestWithCache(`/users/${username}/repos`);
2838
const result = await langStatistics(Object.values(response).map(async (resp) => {
2939
return await getLanguage(username, resp['name']);
3040
}));
3141
res.send(result);
3242
})
3343

34-
app.get('/repo/:user/:repo/', function (req, res) {
35-
res.send(getLanguage(req.params['user'], req.params['repo']));
44+
app.get('/:user/:repo/', async function (req, res) {
45+
const username = req.params['user'], repo = req.params['repo'];
46+
if ( ! isAvailableUser(username) ) {
47+
res.send('permission denied');
48+
return;
49+
}
50+
res.send(await getLanguage(username, repo));
3651
})
3752

3853
app.listen(3000);

0 commit comments

Comments
 (0)