Skip to content

Commit

Permalink
add Enable Disable events
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Dec 29, 2024
1 parent 67100c7 commit 856ff30
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 43 deletions.
86 changes: 86 additions & 0 deletions src/Private/Console.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
7 changes: 0 additions & 7 deletions src/Private/Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Private/Events.ps1
Original file line number Diff line number Diff line change
@@ -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
)
Expand Down
56 changes: 52 additions & 4 deletions src/Private/Server.ps1
Original file line number Diff line number Diff line change
@@ -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()]
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
}

Expand All @@ -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)
Expand Down
40 changes: 14 additions & 26 deletions src/Public/Core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -364,6 +351,7 @@ function Start-PodeServer {

# clean the session
$PodeContext = $null
$PodeLocale = $null
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Public/Events.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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,

Expand Down Expand Up @@ -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
)
Expand Down

0 comments on commit 856ff30

Please sign in to comment.