From 60fd32e4d5a25de60eb21c5994bdb9004a415ff2 Mon Sep 17 00:00:00 2001 From: Aetherinox Date: Sun, 22 Jun 2025 21:24:08 -0700 Subject: [PATCH] feat: add new query param `silent` --- tvapp2/index.js | 55 ++++++++++++++++++++++++++++++++++++------- tvapp2/www/index.html | 9 ++++--- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/tvapp2/index.js b/tvapp2/index.js index 78c49218..60804a0e 100755 --- a/tvapp2/index.js +++ b/tvapp2/index.js @@ -188,6 +188,28 @@ getos( ( e, json ) => return serverOs; }); +/* + helper > str2bool +*/ + +function str2bool( str ) +{ + if ( typeof str === 'string' ) + { + const lower = str.toLowerCase(); + if ([ + '1', 'true', 'yes', 'y', 't' + ].includes( lower ) ) + str = true; + if ([ + '0', 'false', 'no', 'n', 'f' + ].includes( lower ) ) + str = false; + return str; + } + else return Boolean( str ); +} + /* Define > Logs @@ -1479,13 +1501,14 @@ async function serveHealthCheck( req, res ) try { const paramUrl = new URL( req.url, `http://${ req.headers.host }` ).searchParams.get( 'api' ); - const paramQuery = new URL( req.url, `http://${ req.headers.host }` ).searchParams.get( 'query' ); + const paramSilent = new URL( req.url, `http://${ req.headers.host }` ).searchParams.get( 'silent' ); + if ( !paramUrl ) { - if ( paramQuery !== 'uptime' ) + if ( str2bool( paramSilent ) !== true ) { Log.debug( `/api`, chalk.yellow( `[health]` ), chalk.white( `⚙️` ), - chalk.blueBright( `` ), chalk.gray( `No API key passed to health check` ) ); + chalk.blueBright( `` ), chalk.gray( `No api-key passed to health check` ) ); } } @@ -1509,7 +1532,7 @@ async function serveHealthCheck( req, res ) 'Content-Type': 'application/json' }); - if ( paramQuery !== 'uptime' ) + if ( str2bool( paramSilent ) !== true ) { Log.ok( `/api`, chalk.yellow( `[health]` ), chalk.white( `✅` ), chalk.greenBright( `` ), chalk.gray( `Response` ), @@ -2062,8 +2085,8 @@ const server = http.createServer( ( req, resp ) => loadFile channel?url=https%3A%2F%2Ftvpass.org%2Fchannel%2Fabc-wabc-new-york-ny%2F */ - const paramQuery = new URL( req.url, `http://${ req.headers.host }` ).searchParams.get( 'query' ); - if ( paramQuery !== 'uptime' ) + const paramSilent = new URL( req.url, `http://${ req.headers.host }` ).searchParams.get( 'silent' ); + if ( str2bool( paramSilent ) !== true ) { Log.debug( `http`, chalk.yellow( `[requests]` ), chalk.white( `⚙️` ), chalk.blueBright( `` ), chalk.gray( `Request started` ), @@ -2251,11 +2274,27 @@ const server = http.createServer( ( req, resp ) => return; } + /* + Endpoint > Health Check + + paramQuery specifies what type of query is triggered + options: + uptime + healthcheck + sync + + paramSilent specifies if logs should be silenced. useful for docker-compose.yml healthcheck so that console + is not spammed every 30 seconds. + */ + if ( subdomainHealth.some( ( urlKeyword ) => loadFile.startsWith( urlKeyword ) ) && method === 'GET' ) { - const paramQuery = new URL( req.url, `http://${ req.headers.host }` ).searchParams.get( 'query' ); + const paramSilent = new URL( req.url, `http://${ req.headers.host }` ).searchParams.get( 'silent' ); - if ( paramQuery !== 'uptime' ) + // do not show log if query is `uptime`, since uptime runs every 1 second. + // do not show logs if query has striggered `silent?=true` in url + + if ( str2bool( paramSilent ) !== true ) { Log.info( `http`, chalk.yellow( `[requests]` ), chalk.white( `ℹ️` ), chalk.blueBright( `` ), chalk.gray( `Requesting to access health api` ), diff --git a/tvapp2/www/index.html b/tvapp2/www/index.html index efee76a6..3f23e9e8 100644 --- a/tvapp2/www/index.html +++ b/tvapp2/www/index.html @@ -345,7 +345,8 @@ url: 'api/health', type: 'GET', data: { - query: 'healthcheck' + query: 'healthcheck', + silent: false }, beforeSend: function( data ) { @@ -403,7 +404,8 @@ url: 'api/health', type: 'GET', data: { - query: 'uptime' + query: 'uptime', + silent: true }, success: function( data ) { @@ -449,7 +451,8 @@ url: 'api/restart', type: 'GET', data: { - query: 'sync' + query: 'sync', + silent: false }, beforeSend: function( data ) {