11const express = require ( 'express' ) ;
22const app = express ( ) ;
3+ const path = require ( 'path' ) ;
34const GitHub = require ( 'github-api' ) ;
45const Promise = require ( 'bluebird' ) ;
56const JSONStream = require ( 'JSONStream' ) ;
67const moment = require ( 'moment' ) ;
7- const morgan = require ( 'morgan' ) ;
8+ const requestLog = require ( './request-log' ) ;
9+ const logger = require ( './logger' ) ;
810const retry = require ( 'bluebird-retry' ) ;
911const _ = require ( 'lodash' ) ;
1012
@@ -24,7 +26,7 @@ const getMessage = (err) => {
2426} ;
2527
2628const catchGitHub401or404 = ( err ) => {
27- console . log ( `GITHUB ERROR: ${ err . message } ` ) ;
29+ logger . info ( `GITHUB ERROR: ${ err . message } ` ) ;
2830 const errorCode = getErrorCode ( err . response || { } ) ;
2931 if ( errorCode === 401 || errorCode === 404 ) {
3032 err . status = errorCode ;
@@ -34,7 +36,7 @@ const catchGitHub401or404 = (err) => {
3436} ;
3537
3638const processGitHubResponse = ( r ) => {
37- console . log ( `${ r . config . method } ${ r . status } ${ r . config . url } | ${ r . statusText } ` ) ;
39+ logger . info ( `${ r . config . method } ${ r . status } ${ r . config . url } | ${ r . statusText } ` ) ;
3840 if ( r . status === 204 ) {
3941 return [ ] ;
4042 }
@@ -90,7 +92,7 @@ const writeRepoStatsToStream = async (github, repo, stream) => {
9092const writeStatsToStream = async ( repos , github , stream ) => {
9193 await Promise . each ( repos , ( repo ) => writeRepoStatsToStream ( github , repo , stream ) )
9294 . catch ( err => {
93- console . log ( `ERROR STREAMING: ${ err . message } ` ) ;
95+ logger . info ( `ERROR STREAMING: ${ err . message } ` ) ;
9496 return stream . write ( {
9597 __streamError : {
9698 message : getMessage ( err ) ,
@@ -110,23 +112,29 @@ const getAuthToken = (req) => {
110112
111113
112114const streamStats = async ( github , repos , out ) => {
113- console . log ( `Count of repos to process is ${ repos . length } ` ) ;
115+ logger . info ( `Count of repos to process is ${ repos . length } ` ) ;
114116 out . type ( 'json' ) ;
115117 const stream = JSONStream . stringify ( ) ;
116118 stream . pipe ( out ) ;
117119 await writeStatsToStream ( repos , github , stream ) ;
118120 stream . end ( ) ;
119121} ;
120122
121- morgan . token ( 'path' , ( req ) => req . path ) ;
123+ app . use ( requestLog ( ) ) ;
122124
123- app . use ( morgan ( ':method :status :path :response-time ms' ) ) ;
125+ app . get ( '/favicon.ico' , function ( req , res ) {
126+ res . sendFile ( path . resolve ( './favicon.ico' ) ) ;
127+ } ) ;
124128
125129app . get ( '/vizydrop-status-ping' , ( req , res ) => {
126130 res . json ( { ok :true } ) ;
127131} ) ;
128132
129133app . get ( '/' , async ( req , res , next ) => {
134+ if ( ! getAuthToken ( req ) ) {
135+ res . sendStatus ( 401 ) ;
136+ return ;
137+ }
130138 try {
131139 const github = await initGitHub ( req ) ;
132140 const repos = await getRepositoriesForLoggedUser ( github ) ;
@@ -181,5 +189,5 @@ app.use((err, req, res, next) => res.status(getErrorCode(err)).send({
181189} ) ) ;
182190
183191const port = process . env . PORT || 7770 ;
184- const server = app . listen ( port , ( ) => console . log ( `github-data-link is listening on port ${ port } !` ) ) ;
192+ const server = app . listen ( port , ( ) => logger . info ( `github-data-link is listening on port ${ port } !` ) ) ;
185193module . exports = ( ) => server ;
0 commit comments