11const express = require ( 'express' ) ;
2- const engine = require ( 'ejs-locals' ) ;
3- const cache = new ( require ( './cache' ) . ApiCache ) ( ) ;
42const logger = ( require ( "log4js" ) ) . getLogger ( "Backend" ) ;
5- const utils = require ( './utils ' ) ;
3+ const stats = require ( './stats ' ) ;
64const conf = require ( './config' ) ;
75const app = express ( ) ;
86
@@ -15,54 +13,6 @@ const isAvailableUser = (username) => {
1513 return ( ! ! username ) && ( conf . requires . includes ( "*" ) || conf . requires . includes ( username ) ) ;
1614}
1715
18- async function getLanguage ( user , repo ) {
19- return await cache . requestWithCache ( `/repos/${ user } /${ repo } /languages` ) ;
20- }
21-
22- async function langStatistics ( queue ) {
23- const res = { } ;
24- for ( const idx in queue ) {
25- const task = queue [ idx ] ;
26- if ( task instanceof Promise ) {
27- const task_response = await task ;
28- for ( const lang in task_response ) {
29- lang in res ?
30- res [ lang ] += task_response [ lang ] :
31- res [ lang ] = task_response [ lang ] ;
32- }
33- }
34- }
35- return res ;
36- }
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-
6616app . get ( '/' , function ( req , res ) {
6717 res . render ( 'index' ) ;
6818} )
@@ -74,7 +24,7 @@ app.get('/user/:user/', async function (req, res) {
7424 return ;
7525 }
7626 res . type ( 'svg' ) ;
77- res . render ( 'user' , await getAccount ( username ) ) ;
27+ res . render ( 'user' , await stats . getAccount ( username ) ) ;
7828} ) ;
7929
8030app . get ( '/repo/:user/:repo/' , async function ( req , res ) {
@@ -84,9 +34,9 @@ app.get('/repo/:user/:repo/', async function (req, res) {
8434 return ;
8535 }
8636 res . type ( 'svg' ) ;
87- res . send ( await getRepository ( username , repo ) ) ;
37+ res . send ( await stats . getRepository ( username , repo ) ) ;
8838} ) ;
8939
9040
91- app . listen ( conf . port , conf . host , ( ) =>
92- logger . info ( `Starting deployment server at http://${ conf . host } :${ conf . port } /.` ) ) ;
41+ app . listen ( conf . port , ( ) =>
42+ logger . info ( `Starting deployment server at http://127.0.0.1 :${ conf . port } /.` ) ) ;
0 commit comments