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

Commit 91a4207

Browse files
committed
0.11
1 parent e72090d commit 91a4207

4 files changed

Lines changed: 42 additions & 27 deletions

File tree

index.js

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const express = require('express');
2+
const engine = require('ejs-locals');
23
const cache = new (require('./cache').ApiCache)();
34
const logger = (require("log4js")).getLogger("Backend");
45
const utils = require('./utils');
56
const conf = require('./config');
67
const app = express();
78

89
logger.level = "debug";
10+
app.set('views',__dirname + '/views');
911
app.set("view engine","ejs");
1012

1113

@@ -33,50 +35,58 @@ async function langStatistics(queue) {
3335
return res;
3436
}
3537

38+
async function getAccount(username) {
39+
const response = await cache.requestWithCache(`/users/${username}`);
40+
return {
41+
username: username,
42+
followers: response['followers'],
43+
repos: response['public_repos'],
44+
langs: await langStatistics(
45+
Object.values(await cache.requestWithCache(`/users/${username}/repos`)
46+
).map(async (resp) => {
47+
return await getLanguage(username, resp['name']);
48+
})),
49+
};
50+
}
51+
52+
async function getRepository(username, repo) {
53+
// get releases (700ms): (await cache.requestWithCache(`/repos/${username}/${repo}/releases`)).length
54+
const info = await cache.requestWithCache(`/repos/${username}/${repo}`);
55+
return {
56+
repo: `${username} / ${repo}`,
57+
size: utils.storeConvert(info['size'], 1),
58+
forks: info['forks'],
59+
stars: info['stargazers_count'],
60+
watchers: info['watchers_count'],
61+
license: info['license']['spdx_id'],
62+
langs: await getLanguage(username, repo),
63+
};
64+
}
65+
3666
app.get('/', function(req, res) {
37-
res.send("<p>User Code Analysis: <strong>/user/:user/</strong> (e.g. /user/zmh-program/)</p>" +
38-
"<p>Repo Code Statistic: <strong>/repo/:user/:repo/</strong> (e.g. /repo/zmh-program/admin-pages/)</p>")
67+
res.render('index');
3968
})
69+
4070
app.get('/user/:user/', async function (req, res) {
4171
const username = req.params['user'];
4272
if ( ! isAvailableUser(username) ) {
4373
res.send('permission denied');
4474
return;
4575
}
46-
const response = await cache.requestWithCache(`/users/${username}`);
4776
res.type('svg');
48-
res.send({
49-
followers: response['followers'],
50-
repos: response['public_repos'],
51-
langs: await langStatistics(
52-
Object.values(await cache.requestWithCache(`/users/${username}/repos`)
53-
).map(async (resp) => {
54-
return await getLanguage(username, resp['name']);
55-
})),
56-
});
77+
res.render('user', await getAccount(username));
5778
});
5879

5980
app.get('/repo/:user/:repo/', async function (req, res) {
6081
const username = req.params['user'], repo = req.params['repo'];
61-
if ( ! isAvailableUser(username) ) {
82+
if ( (! isAvailableUser(username)) || (! repo)) {
6283
res.send('permission denied');
6384
return;
6485
}
65-
const info = await cache.requestWithCache(`/repos/${username}/${repo}`);
6686
res.type('svg');
67-
res.send({
68-
size: utils.storeConvert(info['size'], 1),
69-
forks: info['forks'],
70-
stars: info['stargazers_count'],
71-
watchers: info['watchers_count'],
72-
license: info['license']['spdx_id'],
73-
langs: await getLanguage(username, repo),
74-
// releases: (await cache.requestWithCache(`/repos/${username}/${repo}/releases`)).length, - 700ms
75-
});
87+
res.send(await getRepository(username, repo));
7688
});
7789

7890

79-
80-
logger.info(`Starting deployment server at http://${conf.host}:${conf.port}/.`)
81-
82-
app.listen(conf.port, conf.host);
91+
app.listen(conf.port, conf.host, () =>
92+
logger.info(`Starting deployment server at http://${conf.host}:${conf.port}/.`));

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"dependencies": {
2323
"axios": "^1.3.2",
2424
"ejs": "^3.1.8",
25+
"ejs-locals": "^1.0.2",
2526
"express": "^4.18.2",
2627
"log4js": "^6.7.1"
2728
}

stats.js

Whitespace-only changes.

views/index.ejs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div>
2+
User Code Analysis: <strong>/user/:user/</strong> (e.g. <a href="/user/zmh-program/">/user/zmh-program/</a>) <br>
3+
Repo Code Statistic: <strong>/repo/:user/:repo/</strong> (e.g. <a href="/repo/zmh-program/admin-pages/">/repo/zmh-program/admin-pages/</a>)
4+
</div>

0 commit comments

Comments
 (0)