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

Commit 1a732b1

Browse files
committed
2.0
1 parent 3b1bca0 commit 1a732b1

4 files changed

Lines changed: 71 additions & 46 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ https://github.com/zmh-program/code-statistic/blob/a3841c2eefcda8484f94f56724d8d
5757

5858
### 🍇 Run
5959
```shell
60-
node index
60+
ts-node index
6161
```

index.ts

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
import {isExistRepo} from "./utils";
1+
const server = require("./server");
22

3-
const logger = (require("log4js")).getLogger("Backend");
4-
const stats = require('./stats');
5-
const conf = require('./config');
6-
const utils = require('./utils');
7-
const app = require('express')();
8-
9-
logger.level = "debug";
10-
app.set('views',__dirname + '/views');
11-
app.set("view engine","ejs");
12-
13-
app.get('/', function(req: any, res: any) {
14-
res.render('index');
15-
})
16-
17-
app.get('/user/:user/', async function (req: any, res: any) {
18-
res.type('svg');
19-
20-
try {
21-
const username = req.params['user'];
22-
if (await utils.isAuthenticated(username)) throw new Error();
23-
res.render('user', await stats.getAccount(username, req.query['theme'] === 'dark'));
24-
} catch (e) {
25-
res.render('error', {dark: req.query['theme'] === 'dark'});
26-
}
27-
});
28-
29-
app.get('/repo/:user/:repo/', async function (req: any, res: any) {
30-
res.type('svg');
31-
try {
32-
const username = req.params['user'], repo = req.params['repo'];
33-
34-
if (await isExistRepo(username, repo)) throw new Error();
35-
res.render('repo', await stats.getRepository(username, repo, req.query['theme'] === 'dark'))
36-
} catch (e) {
37-
res.render('error', {dark: req.query['theme'] === 'dark'});
38-
}
39-
});
40-
41-
app.listen(conf.port, () =>
42-
logger.info(`Starting deployment server at http://127.0.0.1:${conf.port}/.`));
3+
server.createServer();

server.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const logger = (require("log4js")).getLogger("Backend");
2+
const stats = require('./stats');
3+
const conf = require('./config');
4+
const utils = require('./utils');
5+
const express = require('express');
6+
7+
logger.level = "debug";
8+
9+
10+
function renderError(res: any, dark: boolean = false): void {
11+
return res.render('error', {dark: dark});
12+
}
13+
14+
async function renderUser(res: any, username: string, dark: boolean = false): Promise<void> {
15+
if (! await utils.isAuthenticated(username)) {
16+
renderError(res, dark);
17+
} else {
18+
const resp = await stats.analyseUser(username);
19+
resp['dark'] = dark;
20+
res.render('user', resp);
21+
}
22+
}
23+
24+
async function renderRepo(res: any, username: string, repo: string, dark: boolean = false): Promise<void> {
25+
if (! await utils.isExistRepo(username, repo)) {
26+
renderError(res, dark);
27+
} else {
28+
const resp = await stats.analyseRepo(username, repo);
29+
resp['dark'] = dark;
30+
res.render('repo', resp);
31+
}
32+
}
33+
export function createServer(): void {
34+
const app = express();
35+
36+
app.set('views', __dirname + '/views');
37+
app.set("view engine", "ejs");
38+
39+
app.get('/', function (req: any, res: any) {
40+
res.render('index');
41+
});
42+
43+
app.get('/user/:user/', async function (req: any, res: any) {
44+
res.type('svg');
45+
46+
const dark: boolean = req.query['theme'] === 'dark',
47+
username: string = req.params['user'];
48+
49+
try { await renderUser(res, username, dark) } catch { renderError(res, dark) }
50+
});
51+
52+
app.get('/repo/:user/:repo/', async function (req: any, res: any) {
53+
res.type('svg');
54+
55+
const dark: boolean = req.query['theme'] === 'dark',
56+
username = req.params['user'],
57+
repo = req.params['repo'];
58+
59+
try { await renderRepo(res, username, repo, dark) } catch { renderError(res, dark) }
60+
});
61+
62+
app.listen(conf.port, () =>
63+
logger.info(`Starting deployment server at http://127.0.0.1:${conf.port}/.`));
64+
}

stats.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ function formatter(langs: object): object[] {
2828
}});
2929
}
3030

31-
export async function getAccount(username: string, dark: boolean = false) {
31+
async function _analyseUser(username: string) {
3232
const response = await utils.requestUser(username);
3333
const repos = await utils.listRepos(username);
3434
return {
35-
dark: dark,
3635
org: response['type'] !== 'User',
3736
location: response['location'], //@ts-ignore
3837
stars: utils.decConvert(utils.sum(repos.map(repo => repo['stargazers_count']))), //@ts-ignore
@@ -48,11 +47,11 @@ export async function getAccount(username: string, dark: boolean = false) {
4847
})),
4948
};
5049
}
50+
export const analyseUser = cache.wrap(_analyseUser);
5151

52-
export async function getRepository(username: string, repo: string, dark: boolean = false) {
52+
async function _analyseRepo(username: string, repo: string) {
5353
const res = await utils.requestRepo(username, repo);
5454
return {
55-
dark : dark,
5655
username: username,
5756
repo: repo,
5857
size: utils.storeConvert(res['size'], 1),
@@ -64,3 +63,4 @@ export async function getRepository(username: string, repo: string, dark: boolea
6463
langs: formatter(await utils.requestLanguage(username, repo)),
6564
};
6665
}
66+
export const analyseRepo = cache.wrap(_analyseRepo);

0 commit comments

Comments
 (0)