Skip to content

Commit

Permalink
Merge pull request #530 from Badgerati/Issue-525
Browse files Browse the repository at this point in the history
Adds new Quiet mode, fixes DisableTermination, and auto-detects Azure Web Apps
  • Loading branch information
Badgerati committed Apr 10, 2020
2 parents f2d649b + 7cd798e commit fe0c6cf
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 70 deletions.
8 changes: 8 additions & 0 deletions docs/Hosting/IIS.md
Expand Up @@ -112,6 +112,14 @@ If the required header is missing, then Pode responds with a 401. The retrieved
!!! note
If the authenticated user is a Local User, then the following properties will be empty: FQDN, Email, and DistinguishedName

## Azure Web Apps

To host your Pode server under IIS using Azure Web Apps, ensure the OS type is Windows and the framework is .NET Core 2.1/3.0.

Your web.config's `processPath` will also need to reference `powershell.exe` not `pwsh.exe`.

Pode can auto-detect if you're using an Azure Web App, but if you're having issues trying setting the `-DisableTermination` and `-Quiet` switches on your [`Start-PodeServer`](../../Functions/Core/Start-PodeServer).

## Useful Links

* [Host ASP.NET Core on Windows with IIS \| Microsoft Docs](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-3.1)
1 change: 1 addition & 0 deletions src/Pode.psd1
Expand Up @@ -104,6 +104,7 @@
'Test-IsPSCore',
'Test-IsEmpty',
'Out-PodeHost',
'Write-PodeHost',
'Test-PodeIsIIS',

# routes
Expand Down
22 changes: 19 additions & 3 deletions src/Private/Context.ps1
Expand Up @@ -28,7 +28,13 @@ function New-PodeContext

[Parameter()]
[string]
$ServerType
$ServerType,

[switch]
$DisableTermination,

[switch]
$Quiet
)

# set a random server name if one not supplied
Expand Down Expand Up @@ -58,12 +64,14 @@ function New-PodeContext
Add-Member -MemberType NoteProperty -Name Server -Value @{} -PassThru |
Add-Member -MemberType NoteProperty -Name Metrics -Value @{} -PassThru

# set the server name, logic and root
# set the server name, logic and root, and other basic properties
$ctx.Server.Name = $Name
$ctx.Server.Logic = $ScriptBlock
$ctx.Server.LogicPath = $FilePath
$ctx.Server.Interval = $Interval
$ctx.Server.PodeModulePath = (Get-PodeModulePath)
$ctx.Server.DisableTermination = $DisableTermination.IsPresent
$ctx.Server.Quiet = $Quiet.IsPresent

# basic logging setup
$ctx.Server.Logging = @{
Expand Down Expand Up @@ -122,14 +130,22 @@ function New-PodeContext
$ctx.Server.Type = 'SERVICE'
}

# if it's serverless, also disable termination
if ($isServerless) {
$ctx.Server.IsServerless = $isServerless
$ctx.Server.DisableTermination = $true
}

# is the server running under IIS? (also, force the server type to pode)
# is the server running under IIS? (also, force the server type to pode, and disable termination)
$ctx.Server.IsIIS = (!$isServerless -and (!(Test-IsEmpty $env:ASPNETCORE_PORT)) -and (!(Test-IsEmpty $env:ASPNETCORE_TOKEN)))
if ($ctx.Server.IsIIS) {
$ctx.Server.Type = 'PODE'
$ctx.Server.DisableTermination = $true

# if IIS under and Azure Web App, force quiet
if (!(Test-IsEmpty $env:WEBSITE_IIS_SITE_NAME)) {
$ctx.Server.Quiet = $true
}
}

# set the IP address details
Expand Down
9 changes: 6 additions & 3 deletions src/Private/FileMonitor.ps1
Expand Up @@ -68,10 +68,12 @@ function Start-PodeFileMonitor
Register-ObjectEvent -InputObject $timer -EventName 'Elapsed' -SourceIdentifier (Get-PodeFileMonitorTimerName) -Action {
# if enabled, show the files that triggered the restart
if ($Event.MessageData.FileSettings.ShowFiles) {
Write-Host 'The following files have changed:' -ForegroundColor Magenta
if (!$Event.MessageData.Quiet) {
Write-Host 'The following files have changed:' -ForegroundColor Magenta

foreach ($file in $Event.MessageData.FileSettings.Files) {
Write-Host "> $($file)" -ForegroundColor Magenta
foreach ($file in $Event.MessageData.FileSettings.Files) {
Write-Host "> $($file)" -ForegroundColor Magenta
}
}

$Event.MessageData.FileSettings.Files = @()
Expand All @@ -83,6 +85,7 @@ function Start-PodeFileMonitor
} -MessageData @{
Tokens = $PodeContext.Tokens
FileSettings = $PodeContext.Server.FileMonitor
Quiet = $PodeContext.Server.Quiet
} -SupportEvent
}

Expand Down
2 changes: 1 addition & 1 deletion src/Private/Gui.ps1
Expand Up @@ -12,7 +12,7 @@ function Start-PodeGuiRunspace
if ($null -eq $PodeContext.Server.Gui.Endpoint)
{
if (($PodeContext.Server.Endpoints | Measure-Object).Count -gt 1) {
Write-Host "Multiple endpoints defined, only the first will be used for the GUI" -ForegroundColor Yellow
Write-PodeHost "Multiple endpoints defined, only the first will be used for the GUI" -ForegroundColor Yellow
}
}

Expand Down
55 changes: 9 additions & 46 deletions src/Private/Helpers.ps1
Expand Up @@ -141,8 +141,8 @@ function Test-IsAdminUser
return $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
catch [exception] {
Write-Host 'Error checking user administrator priviledges' -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
Write-PodeHost 'Error checking user administrator priviledges' -ForegroundColor Red
Write-PodeHost $_.Exception.Message -ForegroundColor Red
return $false
}
}
Expand Down Expand Up @@ -217,7 +217,7 @@ function Set-PodeCertificate

# only bind if windows at the moment
if (!(Test-IsWindows)) {
Write-Host "Certificates are currently only supported on Windows" -ForegroundColor Yellow
Write-PodeHost "Certificates are currently only supported on Windows" -ForegroundColor Yellow
return
}

Expand All @@ -227,7 +227,7 @@ function Set-PodeCertificate
}

if ($sslPortInUse) {
Write-Host "$($addrport) already has a certificate bound" -ForegroundColor Green
Write-PodeHost "$($addrport) already has a certificate bound" -ForegroundColor Green
return
}

Expand All @@ -246,13 +246,13 @@ function Set-PodeCertificate
{
# generate a self-signed cert
if ($SelfSigned) {
Write-Host "Generating self-signed certificate for $($addrport)..." -NoNewline -ForegroundColor Cyan
Write-PodeHost "Generating self-signed certificate for $($addrport)..." -NoNewline -ForegroundColor Cyan
$cert = (New-PodeSelfSignedCertificate)
}

# ensure a given cert exists for binding
else {
Write-Host "Binding $($Certificate) to $($addrport)..." -NoNewline -ForegroundColor Cyan
Write-PodeHost "Binding $($Certificate) to $($addrport)..." -NoNewline -ForegroundColor Cyan
$cert = (Get-PodeCertificate -Certificate $Certificate)
}
}
Expand All @@ -271,7 +271,7 @@ function Set-PodeCertificate
}
}

Write-Host " Done" -ForegroundColor Green
Write-PodeHost " Done" -ForegroundColor Green
}

function Get-PodeHostIPRegex
Expand Down Expand Up @@ -719,7 +719,7 @@ function Test-PodeTerminationPressed
$Key = $null
)

if ($PodeContext.DisableTermination) {
if ($PodeContext.Server.DisableTermination) {
return $false
}

Expand All @@ -746,43 +746,6 @@ function Test-PodeRestartPressed
(($Key.Modifiers -band [ConsoleModifiers]::Control) -or ((Test-IsUnix) -and ($Key.Modifiers -band [ConsoleModifiers]::Shift))))
}

function Start-PodeTerminationListener
{
Add-PodeRunspace -Type 'Main' {
# default variables
$options = "AllowCtrlC,IncludeKeyUp,NoEcho"
$ctrlState = "LeftCtrlPressed"
$char = 'c'
$cancel = $false

# are we on ps-core?
$onCore = ($PSVersionTable.PSEdition -ieq 'core')

while ($true) {
if ($Console.UI.RawUI.KeyAvailable) {
$key = $Console.UI.RawUI.ReadKey($options)

if ([char]$key.VirtualKeyCode -ieq $char) {
if ($onCore) {
$cancel = ($key.Character -ine $char)
}
else {
$cancel = (($key.ControlKeyState -band $ctrlState) -ieq $ctrlState)
}
}

if ($cancel) {
Write-Host 'Terminating...' -NoNewline
$PodeContext.Tokens.Cancellation.Cancel()
break
}
}

Start-Sleep -Milliseconds 10
}
}
}

function Close-PodeServer
{
param (
Expand All @@ -809,7 +772,7 @@ function Close-PodeServer
Remove-PodePSDrives

if ($ShowDoneMessage -and ![string]::IsNullOrWhiteSpace($PodeContext.Server.Type) -and !$PodeContext.Server.IsServerless) {
Write-Host " Done" -ForegroundColor Green
Write-PodeHost " Done" -ForegroundColor Green
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/Private/Logging.ps1
Expand Up @@ -2,8 +2,13 @@ function Get-PodeLoggingTerminalMethod
{
return {
param($item, $options)

if ($PodeContext.Server.Quiet) {
return
}

$item = ($item | Protect-PodeLogItem)
$item.ToString() | Out-Default
$item.ToString() | Out-PodeHost
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Private/Server.ps1
Expand Up @@ -91,9 +91,9 @@ function Start-PodeInternalServer

# state what endpoints are being listened on
if ($endpoints.Length -gt 0) {
Write-Host "Listening on the following $($endpoints.Length) endpoint(s) [$($PodeContext.Threads.Web) thread(s)]:" -ForegroundColor Yellow
Write-PodeHost "Listening on the following $($endpoints.Length) endpoint(s) [$($PodeContext.Threads.Web) thread(s)]:" -ForegroundColor Yellow
$endpoints | ForEach-Object {
Write-Host "`t- $($_)" -ForegroundColor Yellow
Write-PodeHost "`t- $($_)" -ForegroundColor Yellow
}
}
}
Expand All @@ -107,7 +107,7 @@ function Restart-PodeInternalServer
try
{
# inform restart
Write-Host 'Restarting server...' -NoNewline -ForegroundColor Cyan
Write-PodeHost 'Restarting server...' -NoNewline -ForegroundColor Cyan

# cancel the session token
$PodeContext.Tokens.Cancellation.Cancel()
Expand Down Expand Up @@ -180,7 +180,7 @@ function Restart-PodeInternalServer
# reload the configuration
$PodeContext.Server.Configuration = Open-PodeConfiguration -Context $PodeContext

Write-Host " Done" -ForegroundColor Green
Write-PodeHost " Done" -ForegroundColor Green

# restart the server
$PodeContext.Metrics.Server.RestartCount++
Expand Down
8 changes: 4 additions & 4 deletions src/Private/Serverless.ps1
Expand Up @@ -84,8 +84,8 @@ function Start-PodeAzFuncServer
}
}
catch {
$_ | Write-PodeErrorLog
Set-PodeResponseStatus -Code 500 -Exception $_
Write-Host $Error[0]
}
finally {
Update-PodeServerRequestMetrics -WebEvent $WebEvent
Expand All @@ -99,7 +99,7 @@ function Start-PodeAzFuncServer
Push-OutputBinding -Name Response -Value $response
}
catch {
Write-Host $Error[0]
$_ | Write-PodeErrorLog
throw $_.Exception
}
}
Expand Down Expand Up @@ -185,8 +185,8 @@ function Start-PodeAwsLambdaServer
}
}
catch {
$_ | Write-PodeErrorLog
Set-PodeResponseStatus -Code 500 -Exception $_
Write-Host $Error[0]
}
finally {
Update-PodeServerRequestMetrics -WebEvent $WebEvent
Expand All @@ -208,7 +208,7 @@ function Start-PodeAwsLambdaServer
} | ConvertTo-Json -Depth 10 -Compress)
}
catch {
Write-Host $Error[0]
$_ | Write-PodeErrorLog
throw $_.Exception
}
}
2 changes: 1 addition & 1 deletion src/Private/ServiceServer.ps1
Expand Up @@ -6,7 +6,7 @@ function Start-PodeServiceServer
}

# state we're running
Write-Host "Server looping every $($PodeContext.Server.Interval)secs" -ForegroundColor Yellow
Write-PodeHost "Server looping every $($PodeContext.Server.Interval)secs" -ForegroundColor Yellow

# script for the looping server
$serverScript = {
Expand Down
2 changes: 1 addition & 1 deletion src/Private/Sockets.ps1
Expand Up @@ -129,7 +129,7 @@ function Close-PodeSocketListener
$PodeContext.Server[$Type].Listeners = @()
}
catch {
$_.Exception | Out-Default
$_.Exception | Out-PodeHost
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/Public/Core.ps1
Expand Up @@ -33,6 +33,9 @@ The server type, to define how Pode should run and deal with incoming Requests.
.PARAMETER DisableTermination
Disables the ability to terminate the Server.
.PARAMETER Quiet
Disables any output from the Server.
.PARAMETER Browse
Open the web Server's default endpoint in your default browser.
Expand Down Expand Up @@ -87,6 +90,9 @@ function Start-PodeServer
[switch]
$DisableTermination,

[switch]
$Quiet,

[switch]
$Browse,

Expand Down Expand Up @@ -127,10 +133,12 @@ function Start-PodeServer
-Threads $Threads `
-Interval $Interval `
-ServerRoot (Protect-PodeValue -Value $RootPath -Default $MyInvocation.PSScriptRoot) `
-ServerType $Type
-ServerType $Type `
-DisableTermination:$DisableTermination `
-Quiet:$Quiet

# set it so ctrl-c can terminate, unless serverless
if (!$PodeContext.Server.IsServerless -and !$PodeContext.Server.IsIIS) {
# set it so ctrl-c can terminate, unless serverless/iis, or disabled
if (!$PodeContext.Server.DisableTermination) {
[Console]::TreatControlCAsInput = $true
}

Expand Down Expand Up @@ -158,7 +166,7 @@ function Start-PodeServer
}
}

Write-Host 'Terminating...' -NoNewline -ForegroundColor Yellow
Write-PodeHost 'Terminating...' -NoNewline -ForegroundColor Yellow
$PodeContext.Tokens.Cancellation.Cancel()
}
catch {
Expand Down

0 comments on commit fe0c6cf

Please sign in to comment.