|
1 | 1 | const express = require('express'); |
| 2 | +const engine = require('ejs-locals'); |
2 | 3 | const cache = new (require('./cache').ApiCache)(); |
3 | 4 | const logger = (require("log4js")).getLogger("Backend"); |
4 | 5 | const utils = require('./utils'); |
5 | 6 | const conf = require('./config'); |
6 | 7 | const app = express(); |
7 | 8 |
|
8 | 9 | logger.level = "debug"; |
| 10 | +app.set('views',__dirname + '/views'); |
9 | 11 | app.set("view engine","ejs"); |
10 | 12 |
|
11 | 13 |
|
@@ -33,50 +35,58 @@ async function langStatistics(queue) { |
33 | 35 | return res; |
34 | 36 | } |
35 | 37 |
|
| 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 | + |
36 | 66 | 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'); |
39 | 68 | }) |
| 69 | + |
40 | 70 | app.get('/user/:user/', async function (req, res) { |
41 | 71 | const username = req.params['user']; |
42 | 72 | if ( ! isAvailableUser(username) ) { |
43 | 73 | res.send('permission denied'); |
44 | 74 | return; |
45 | 75 | } |
46 | | - const response = await cache.requestWithCache(`/users/${username}`); |
47 | 76 | 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)); |
57 | 78 | }); |
58 | 79 |
|
59 | 80 | app.get('/repo/:user/:repo/', async function (req, res) { |
60 | 81 | const username = req.params['user'], repo = req.params['repo']; |
61 | | - if ( ! isAvailableUser(username) ) { |
| 82 | + if ( (! isAvailableUser(username)) || (! repo)) { |
62 | 83 | res.send('permission denied'); |
63 | 84 | return; |
64 | 85 | } |
65 | | - const info = await cache.requestWithCache(`/repos/${username}/${repo}`); |
66 | 86 | 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)); |
76 | 88 | }); |
77 | 89 |
|
78 | 90 |
|
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}/.`)); |
0 commit comments