From 856ff30429803b08a2f9dcf16131aed7bb9de93e Mon Sep 17 00:00:00 2001 From: mdaneri Date: Sun, 29 Dec 2024 09:51:50 -0800 Subject: [PATCH] add Enable Disable events --- src/Private/Console.ps1 | 86 +++++++++++++++++++++++++++++++++++++++++ src/Private/Context.ps1 | 7 ---- src/Private/Events.ps1 | 2 +- src/Private/Server.ps1 | 56 +++++++++++++++++++++++++-- src/Public/Core.ps1 | 40 +++++++------------ src/Public/Events.ps1 | 10 ++--- 6 files changed, 158 insertions(+), 43 deletions(-) diff --git a/src/Private/Console.ps1 b/src/Private/Console.ps1 index 734cef219..c3a5e623a 100644 --- a/src/Private/Console.ps1 +++ b/src/Private/Console.ps1 @@ -1066,3 +1066,89 @@ function Write-PodeConsoleHeader { Write-PodeHost ']' -ForegroundColor $headerColor -Force:$Force -NoNewLine:$NoNewLine } } + +<# +.SYNOPSIS +Sets override configurations for the Pode server console. + +.DESCRIPTION +This function updates the Pode server's console configuration by applying override settings based on the specified parameters. These configurations include disabling termination, console input, enabling quiet mode, and more. + +.PARAMETER DisableTermination +Sets an override to disable termination of the Pode server from the console. + +.PARAMETER DisableConsoleInput +Sets an override to disable input from the console for the Pode server. + +.PARAMETER Quiet +Sets an override to enable quiet mode, suppressing console output. + +.PARAMETER ClearHost +Sets an override to clear the host on startup. + +.PARAMETER HideOpenAPI +Sets an override to hide the OpenAPI documentation display. + +.PARAMETER HideEndpoints +Sets an override to hide the endpoints list display. + +.PARAMETER ShowHelp +Sets an override to show help information in the console. + +.PARAMETER Daemon +Sets an override to enable daemon mode, which combines quiet mode, disabled console input, and disabled termination. + +.NOTES +This function is used to dynamically apply override settings for the Pode server console configuration. +#> +function Set-PodeConsoleOverrideConfiguration { + param ( + [switch]$DisableTermination, + [switch]$DisableConsoleInput, + [switch]$Quiet, + [switch]$ClearHost, + [switch]$HideOpenAPI, + [switch]$HideEndpoints, + [switch]$ShowHelp, + [switch]$Daemon + ) + + # Apply override settings + if ($DisableTermination.IsPresent) { + $PodeContext.Server.Console.DisableTermination = $true + } + if ($DisableConsoleInput.IsPresent) { + $PodeContext.Server.Console.DisableConsoleInput = $true + } + if ($Quiet.IsPresent) { + $PodeContext.Server.Console.Quiet = $true + } + if ($ClearHost.IsPresent) { + $PodeContext.Server.Console.ClearHost = $true + } + if ($HideOpenAPI.IsPresent) { + $PodeContext.Server.Console.ShowOpenAPI = $false + } + if ($HideEndpoints.IsPresent) { + $PodeContext.Server.Console.ShowEndpoints = $false + } + if ($ShowHelp.IsPresent) { + $PodeContext.Server.Console.ShowHelp = $true + } + if ($Daemon.IsPresent) { + $PodeContext.Server.Console.Quiet = $true + $PodeContext.Server.Console.DisableConsoleInput = $true + $PodeContext.Server.Console.DisableTermination = $true + } + + # Apply IIS-specific overrides + if ($PodeContext.Server.IsIIS) { + $PodeContext.Server.Console.DisableTermination = $true + $PodeContext.Server.Console.DisableConsoleInput = $true + + # If running under Azure Web App, enforce quiet mode + if (!(Test-PodeIsEmpty $env:WEBSITE_IIS_SITE_NAME)) { + $PodeContext.Server.Console.Quiet = $true + } + } +} \ No newline at end of file diff --git a/src/Private/Context.ps1 b/src/Private/Context.ps1 index 6aeab5c2a..aaeab7741 100644 --- a/src/Private/Context.ps1 +++ b/src/Private/Context.ps1 @@ -254,13 +254,6 @@ function New-PodeContext { # is the server running under IIS? (also, disable termination) $ctx.Server.IsIIS = (!$isServerless -and (!(Test-PodeIsEmpty $env:ASPNETCORE_PORT)) -and (!(Test-PodeIsEmpty $env:ASPNETCORE_TOKEN))) if ($ctx.Server.IsIIS) { - $ctx.Server.Console.DisableTermination = $true - - # if under IIS and Azure Web App, force quiet - if (!(Test-PodeIsEmpty $env:WEBSITE_IIS_SITE_NAME)) { - $ctx.Server.Console.Quiet = $true - } - # set iis token/settings $ctx.Server.IIS = @{ Token = $env:ASPNETCORE_TOKEN diff --git a/src/Private/Events.ps1 b/src/Private/Events.ps1 index b77e90e25..5b9128d66 100644 --- a/src/Private/Events.ps1 +++ b/src/Private/Events.ps1 @@ -1,7 +1,7 @@ function Invoke-PodeEvent { param( [Parameter(Mandatory = $true)] - [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running','Suspend','Resume')] + [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running','Suspend','Resume','Enable','Disable')] [string] $Type ) diff --git a/src/Private/Server.ps1 b/src/Private/Server.ps1 index 9e8e93730..822f4fd21 100644 --- a/src/Private/Server.ps1 +++ b/src/Private/Server.ps1 @@ -1,3 +1,33 @@ +<# +.SYNOPSIS + Starts the internal Pode server, initializing configurations, middleware, routes, and runspaces. + +.DESCRIPTION + This function sets up and starts the internal Pode server. It initializes the server's configurations, routes, middleware, runspace pools, logging, and schedules. It also handles different server modes, such as normal, service, or serverless (Azure Functions, AWS Lambda). The function ensures all necessary components are ready and operational before triggering the server's start. + +.PARAMETER Request + Provides request data for serverless execution scenarios. + +.PARAMETER Browse + A switch to enable browsing capabilities for HTTP servers. + +.EXAMPLE + Start-PodeInternalServer + Starts the Pode server in the normal mode with all necessary components initialized. + +.EXAMPLE + Start-PodeInternalServer -Request $RequestData + Starts the Pode server in serverless mode, passing the required request data. + +.EXAMPLE + Start-PodeInternalServer -Browse + Starts the Pode HTTP server with browsing capabilities enabled. + +.NOTES + - This function is used to start the Pode server, either initially or after a restart. + - Handles specific setup for serverless types like Azure Functions and AWS Lambda. + - This is an internal function used within the Pode framework and is subject to change in future releases. +#> function Start-PodeInternalServer { param( [Parameter()] @@ -159,7 +189,21 @@ function Start-PodeInternalServer { } } +<# +.SYNOPSIS + Restarts the internal Pode server by clearing all configurations, contexts, and states, and reinitializing the server. +.DESCRIPTION + This function performs a comprehensive restart of the internal Pode server. It resets all contexts, clears caches, schedules, timers, middleware, and security configurations, and reinitializes the server state. It also reloads the server configuration if enabled and increments the server restart count. + +.EXAMPLE + Restart-PodeInternalServer + Restarts the Pode server, clearing all configurations and states before starting it again. +.NOTES + - This function is called internally to restart the Pode server gracefully. + - Handles cancellation tokens, clean-up processes, and reinitialization. + - This is an internal function used within the Pode framework and is subject to change in future releases. +#> function Restart-PodeInternalServer { if (!$PodeContext.Tokens.Restart.IsCancellationRequested) { @@ -316,8 +360,7 @@ function Restart-PodeInternalServer { - In other cases, the server will stay open. .NOTES - This function is primarily used internally by Pode to manage the server lifecycle. - It helps ensure the server remains active only when necessary based on its current state. + This is an internal function used within the Pode framework and is subject to change in future releases. #> function Test-PodeServerKeepOpen { # if we have any timers/schedules/fim - keep open @@ -516,8 +559,6 @@ function Resume-PodeServerInternal { } } - - <# .SYNOPSIS Enables new requests by removing the middleware that blocks requests when the Pode Watchdog service is active. @@ -536,6 +577,9 @@ function Enable-PodeServerInternal { return } + # Trigger the 'Enable' event for the server. + Invoke-PodeEvent -Type Enable + Remove-PodeMiddleware -Name $PodeContext.Server.AllowedActions.DisableSettings.MiddlewareName } @@ -555,6 +599,10 @@ function Disable-PodeServerInternal { if (!(Test-PodeServerState -State Running) -or (!( Test-PodeServerIsEnabled)) ) { return } + + # Trigger the 'Enable' event for the server. + Invoke-PodeEvent -Type Disable + # Add middleware to block new requests and respond with 503 Service Unavailable Add-PodeMiddleware -Name $PodeContext.Server.AllowedActions.DisableSettings.MiddlewareName -ScriptBlock { # Set HTTP response header for retrying after a certain time (RFC7231) diff --git a/src/Public/Core.ps1 b/src/Public/Core.ps1 index cbcc987cd..a37eaafca 100644 --- a/src/Public/Core.ps1 +++ b/src/Public/Core.ps1 @@ -266,34 +266,21 @@ function Start-PodeServer { # Create main context object $PodeContext = New-PodeContext @ContextParams - # Override the configuration - if ($DisableTermination.IsPresent) { - $PodeContext.Server.Console.DisableTermination = $true - } - if ($DisableConsoleInput.IsPresent) { - $PodeContext.Server.Console.DisableConsoleInput = $true - } - if ($Quiet.IsPresent) { - $PodeContext.Server.Console.Quiet = $true - } - if ($ClearHost.IsPresent) { - $PodeContext.Server.Console.ClearHost = $true - } - if ($ShowOpenAPI.IsPresent) { - $PodeContext.Server.Console.ShowOpenAPI = $false - } - if ($ShowEndpoints.IsPresent) { - $PodeContext.Server.Console.ShowEndpoints = $false - } - if ($ShowHelp.IsPresent) { - $PodeContext.Server.Console.ShowHelp = $true - } - if ($Daemon.IsPresent) { - $PodeContext.Server.Console.Quiet = $true - $PodeContext.Server.Console.DisableConsoleInput = $true - $PodeContext.Server.Console.DisableTermination = $true + # Define parameter values with comments explaining each one + $ConfigParameters = @{ + DisableTermination = $DisableTermination # Disable termination of the Pode server from the console + DisableConsoleInput = $DisableConsoleInput # Disable input from the console for the Pode server + Quiet = $Quiet # Enable quiet mode, suppressing console output + ClearHost = $ClearHost # Clear the host on startup + HideOpenAPI = $HideOpenAPI # Hide the OpenAPI documentation display + HideEndpoints = $HideEndpoints # Hide the endpoints list display + ShowHelp = $ShowHelp # Show help information in the console + Daemon = $Daemon # Enable daemon mode, combining multiple configurations } + # Call the function using splatting + Set-PodeConsoleOverrideConfiguration @ConfigParameters + # start the file monitor for interally restarting Start-PodeFileMonitor @@ -364,6 +351,7 @@ function Start-PodeServer { # clean the session $PodeContext = $null + $PodeLocale = $null } } } diff --git a/src/Public/Events.ps1 b/src/Public/Events.ps1 index b3f800a0d..183ff0439 100644 --- a/src/Public/Events.ps1 +++ b/src/Public/Events.ps1 @@ -24,7 +24,7 @@ function Register-PodeEvent { [CmdletBinding()] param( [Parameter(Mandatory = $true)] - [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running')] + [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running','Suspend','Resume','Enable','Disable')] [string] $Type, @@ -78,7 +78,7 @@ function Unregister-PodeEvent { [CmdletBinding()] param( [Parameter(Mandatory = $true)] - [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running')] + [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running','Suspend','Resume','Enable','Disable')] [string] $Type, @@ -116,7 +116,7 @@ function Test-PodeEvent { [CmdletBinding()] param( [Parameter(Mandatory = $true)] - [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running')] + [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running','Suspend','Resume','Enable','Disable')] [string] $Type, @@ -148,7 +148,7 @@ function Get-PodeEvent { [CmdletBinding()] param( [Parameter(Mandatory = $true)] - [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running')] + [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running','Suspend','Resume','Enable','Disable')] [string] $Type, @@ -177,7 +177,7 @@ function Clear-PodeEvent { [CmdletBinding()] param( [Parameter(Mandatory = $true)] - [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running')] + [ValidateSet('Start', 'Terminate', 'Restart', 'Browser', 'Crash', 'Stop', 'Running','Suspend','Resume','Enable','Disable')] [string] $Type )