@@ -27,21 +27,28 @@ const app = express()
2727const port = process . env . PORT || 3000
2828
2929app . use ( express . static ( 'src/public' ) )
30- app . use ( express . json ( ) )
3130app . use ( cors ( { methods : [ 'GET' , 'HEAD' , 'PUT' , 'PATCH' , 'POST' , 'DELETE' , 'PURGE' ] } ) )
3231
3332app . set ( 'views' , 'src/views' )
3433app . set ( 'view engine' , 'pug' )
3534app . disable ( 'view cache' )
35+ app . disable ( 'x-powered-by' )
3636
3737console . log ( `APP_VERSION: ${ process . env . APP_VERSION } ` )
3838console . log ( `GITHUB_TOKEN: ${ process . env . GITHUB_TOKEN ? 'Loaded' : 'MISSING' } ` )
3939console . log ( `VT_API_KEY: ${ process . env . VT_API_KEY ? 'Loaded' : 'MISSING' } ` )
4040
41- app . listen ( port , ( ) => {
41+ const server = app . listen ( port , ( ) => {
4242 console . log ( `Listening on PORT: ${ port } ` )
4343} )
4444
45+ process . on ( 'SIGTERM' , ( ) => {
46+ console . log ( 'SIGTERM signal received: closing HTTP server' )
47+ server . close ( ( ) => {
48+ console . log ( 'HTTP server closed' )
49+ } )
50+ } )
51+
4552app . get ( '/app-health-check' , ( req , res ) => {
4653 res . sendStatus ( 200 )
4754} )
@@ -93,11 +100,11 @@ app.all('/vt/:type/:hash', async (req, res, next) => {
93100 if ( req . method === 'PURGE' ) {
94101 console . log ( 'PURGE:' , req . originalUrl )
95102 if ( ! [ 'id' , 'sha' ] . includes ( req . params . type ) ) return next ( )
96- let hash = req . params . hash
97- if ( req . params . hash === 'sha' ) {
98- hash = hash . includes ( ':' ) ? hash . split ( ':' ) [ 1 ] : hash
99- }
100- const key = `/vt/${ req . params . type === 'id' ? 'id' : 'sha' } /${ hash } `
103+ const hash = req . params . hash . includes ( ':' )
104+ ? req . params . hash . split ( ':' ) [ 1 ]
105+ : req . params . hash
106+ console . log ( 'hash:' , hash )
107+ const key = `/vt/id /${ hash } `
101108 return purgeKey ( res , key )
102109 }
103110 next ( )
@@ -109,13 +116,14 @@ app.get(
109116 console . log ( req . originalUrl )
110117 // console.log('req.params.type:', req.params.type)
111118 if ( ! [ 'id' , 'sha' ] . includes ( req . params . type ) ) return res . sendStatus ( 404 )
112-
113119 if ( ! process . env . VT_API_KEY ) throw new Error ( 'Missing VT API Key' )
114- let hash = req . params . hash
115- if ( req . params . type === 'sha' ) {
116- hash = hash . includes ( ':' ) ? hash . split ( ':' ) [ 1 ] : hash
117- }
118- const stats = await getVTStats ( hash , req . params . type === 'id' )
120+
121+ const hash = req . params . hash . includes ( ':' )
122+ ? req . params . hash . split ( ':' ) [ 1 ]
123+ : req . params . hash
124+ console . log ( 'hash:' , hash )
125+
126+ const stats = await getVTStats ( hash )
119127 // console.log('stats:', stats)
120128 const message = `${ stats . malicious } /${ stats . suspicious } /${ stats . undetected } `
121129 console . log ( 'message:' , message )
@@ -277,6 +285,20 @@ app.get(
277285 } )
278286)
279287
288+ // Handler 404
289+ app . use ( ( req , res ) => {
290+ // res.status(404).send("Sorry can't find that!")
291+ const data = {
292+ message : '404 - URL Not Found' ,
293+ color : 'red' ,
294+ style : req . query . style || 'flat' ,
295+ }
296+ console . log ( 'data:' , data )
297+ // noinspection JSCheckFunctionSignatures
298+ const badge = makeBadge ( data )
299+ sendBadge ( res , badge , 404 )
300+ } )
301+
280302if ( Sentry ) Sentry . setupExpressErrorHandler ( app )
281303
282304function errorBadgeHandler ( handler ) {
@@ -298,12 +320,32 @@ function errorBadgeHandler(handler) {
298320 }
299321}
300322
323+ /**
324+ * Set SVG Headers
325+ * @param {express.Response } res
326+ */
327+ function setHeaders ( res ) {
328+ res . setHeader ( 'Content-Type' , 'image/svg+xml' )
329+ res . setHeader ( 'Cache-Control' , 'public, max-age=3600' )
330+ }
331+
332+ /**
333+ * Send Badge
334+ * @param {express.Response } res
335+ * @param {String } badge
336+ * @param {Number } [status]
337+ */
338+ function sendBadge ( res , badge , status = 200 ) {
339+ setHeaders ( res )
340+ res . status ( status ) . send ( badge )
341+ }
342+
301343/**
302344 * Get Badge
303345 * @param {String } message Badge Message
304346 * @param {Object } [query] req.query Object
305347 * @param {Object } [options] Badge Options
306- * @param {Response } [res] To also sendBadge
348+ * @param {express. Response } [res] To sendBadge
307349 * @return {String }
308350 */
309351function getBadge ( message , query = { } , options = { } , res = null ) {
@@ -327,17 +369,6 @@ function getBadge(message, query = {}, options = {}, res = null) {
327369 return badge
328370}
329371
330- /**
331- * Send Badge
332- * @param {Response } res
333- * @param {String } badge
334- */
335- function sendBadge ( res , badge ) {
336- res . setHeader ( 'Content-Type' , 'image/svg+xml' )
337- res . setHeader ( 'Cache-Control' , 'public, max-age=3600' )
338- res . send ( badge )
339- }
340-
341372/**
342373 * Get Logo String
343374 * @param {Object } query
@@ -380,7 +411,7 @@ function getLogo(query, opts, color = '#fff') {
380411
381412/**
382413 * Purge Key Response
383- * @param {Response } res
414+ * @param {express. Response } res
384415 * @param {String } key
385416 * @return {Promise<void> }
386417 */
@@ -420,7 +451,7 @@ function getUptime() {
420451
421452/**
422453 * Get Ranged Color w/ Options
423- * @param {Request } req
454+ * @param {express. Request } req
424455 * @param {Number } index
425456 * @param {Object } [options]
426457 * @return {String }
0 commit comments