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" ;
1+ import { getLogger } from "log4js" ;
2+ import { isAuthenticated , isExistRepo } from "./utils" ;
3+ import { analyseRepo , analyseUser } from "./stats" ;
4+ import { port } from "./config" ;
85
96
10- function renderError ( res : any , dark : boolean = false ) : void {
11- return res . render ( 'error' , { dark : dark } ) ;
12- }
7+ const express = require ( 'express' ) ;
8+ const logger = getLogger ( "Backend" ) ;
9+ logger . level = "debug" ;
1310
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- }
2311
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- }
3312export function createServer ( ) : void {
3413 const app = express ( ) ;
3514
@@ -45,7 +24,17 @@ export function createServer(): void {
4524 const dark : boolean = req . query [ 'theme' ] === 'dark' ,
4625 username : string = req . params [ 'user' ] ;
4726
48- try { await renderUser ( res , username , dark ) } catch { renderError ( res , dark ) }
27+ try {
28+ if ( ! await isAuthenticated ( username ) ) {
29+ res . render ( 'error' , { dark : dark } ) ;
30+ } else {
31+ const resp = await analyseUser ( username ) ;
32+ resp [ 'dark' ] = dark ;
33+ res . render ( 'user' , resp ) ;
34+ }
35+ } catch {
36+ res . render ( 'error' , { dark : dark } ) ;
37+ }
4938 } ) ;
5039
5140 app . get ( '/repo/:user/:repo/' , async function ( req : any , res : any ) {
@@ -55,9 +44,19 @@ export function createServer(): void {
5544 username = req . params [ 'user' ] ,
5645 repo = req . params [ 'repo' ] ;
5746
58- try { await renderRepo ( res , username , repo , dark ) } catch { renderError ( res , dark ) }
47+ try {
48+ if ( ! await isExistRepo ( username , repo ) ) {
49+ res . render ( 'error' , { dark : dark } ) ;
50+ } else {
51+ const resp = await analyseRepo ( username , repo ) ;
52+ resp [ 'dark' ] = dark ;
53+ res . render ( 'repo' , resp ) ;
54+ }
55+ } catch {
56+ res . render ( 'error' , { dark : dark } ) ;
57+ }
5958 } ) ;
6059
61- app . listen ( conf . port , ( ) =>
62- logger . info ( `Starting deployment server at http://127.0.0.1:${ conf . port } /.` ) ) ;
60+ app . listen ( port , ( ) =>
61+ logger . info ( `Starting deployment server at http://127.0.0.1:${ port } /.` ) ) ;
6362}
0 commit comments