Skip to content

Commit

Permalink
Merge a583d12 into 2d29a1a
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Dec 11, 2019
2 parents 2d29a1a + a583d12 commit 1f57463
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
13 changes: 13 additions & 0 deletions examples/scripts/server.ps1
@@ -0,0 +1,13 @@
{
Add-PodeEndpoint -Address * -Port 8081 -Protocol Http
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
Set-PodeViewEngine -Type Pode

Add-PodeTimer -Name 'Hi' -Interval 4 -ScriptBlock {
'Hello from a file!' | Out-PodeHost
}

Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
Write-PodeViewResponse -Path 'simple' -Data @{ 'numbers' = @(1, 2, 3); }
}
}
8 changes: 8 additions & 0 deletions examples/server-from-file.ps1
@@ -0,0 +1,8 @@
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
Import-Module "$($path)/src/Pode.psm1" -Force -ErrorAction Stop

# or just:
# Import-Module Pode

# create a basic server
Start-PodeServer -FilePath './scripts/server.ps1' -CurrentPath
12 changes: 12 additions & 0 deletions src/Private/Context.ps1
@@ -1,21 +1,32 @@
function New-PodeContext
{
[CmdletBinding()]
param (
[Parameter()]
[scriptblock]
$ScriptBlock,

[Parameter()]
[string]
$FilePath,

[Parameter()]
[int]
$Threads = 1,

[Parameter()]
[int]
$Interval = 0,

[Parameter()]
[string]
$ServerRoot,

[Parameter()]
[string]
$Name = $null,

[Parameter()]
[string]
$ServerType
)
Expand Down Expand Up @@ -49,6 +60,7 @@ function New-PodeContext
# set the server name, logic and root
$ctx.Server.Name = $Name
$ctx.Server.Logic = $ScriptBlock
$ctx.Server.LogicPath = $FilePath
$ctx.Server.Interval = $Interval
$ctx.Server.PodeModulePath = (Get-PodeModulePath)

Expand Down
13 changes: 11 additions & 2 deletions src/Private/Server.ps1
Expand Up @@ -13,9 +13,18 @@ function Start-PodeInternalServer
# setup temp drives for internal dirs
Add-PodePSInbuiltDrives

# create the runspace state, execute the server logic, and start the runspaces
# create the shared runspace state
New-PodeRunspaceState
Invoke-PodeScriptBlock -ScriptBlock $PodeContext.Server.Logic -NoNewClosure

# get the server's script and invoke it - to set up routes, timers, middleware, etc
$_script = $PodeContext.Server.Logic
if (Test-PodePath -Path $PodeContext.Server.LogicPath -NoStatus) {
$_script = Convert-PodeFileToScriptBlock -FilePath $PodeContext.Server.LogicPath
}

Invoke-PodeScriptBlock -ScriptBlock $_script -NoNewClosure

# start the runspace pools for web, schedules, etc
New-PodeRunspacePools

# create timer/schedules for auto-restarting
Expand Down
39 changes: 35 additions & 4 deletions src/Public/Core.ps1
Expand Up @@ -8,6 +8,10 @@ Starts a Pode Server with the supplied ScriptBlock.
.PARAMETER ScriptBlock
The main logic for the Server.
.PARAMETER FilePath
A literal, or relative, path to a file containing a ScriptBlock for the Server's logic.
The directory of this file will be used as the Server's root path - unless a specific -RootPath is supplied.
.PARAMETER Interval
For 'Service' type Servers, will invoke the ScriptBlock every X seconds.
Expand All @@ -32,6 +36,9 @@ Disables the ability to terminate the Server.
.PARAMETER Browse
Open the web Server's default endpoint in your defualt browser.
.PARAMETER CurrentPath
Sets the Server's root path to be the current working path - for -FilePath only.
.EXAMPLE
Start-PodeServer { /* logic */ }
Expand All @@ -43,12 +50,16 @@ Start-PodeServer -Request $LambdaInput -Type 'AwsLambda' { /* logic */ }
#>
function Start-PodeServer
{
[CmdletBinding()]
[CmdletBinding(DefaultParameterSetName='Script')]
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0, ParameterSetName='Script')]
[scriptblock]
$ScriptBlock,

[Parameter(Mandatory=$true, ParameterSetName='File')]
[string]
$FilePath,

[Parameter()]
[int]
$Interval = 0,
Expand Down Expand Up @@ -77,21 +88,41 @@ function Start-PodeServer
$DisableTermination,

[switch]
$Browse
$Browse,

[switch]
$CurrentPath
)

# ensure the session is clean
$PodeContext = $null
$ShowDoneMessage = $true

try {
# if we have a filepath, resolve it - and extract a root path from it
if ($PSCmdlet.ParameterSetName -ieq 'file') {
$FilePath = Get-PodeRelativePath -Path $FilePath -Resolve -TestPath

# if not already supplied, set root path
if ([string]::IsNullOrWhiteSpace($RootPath)) {
if ($CurrentPath) {
$RootPath = $PWD.Path
}
else {
$RootPath = Split-Path -Parent -Path $FilePath
}
}
}

# configure the server's root path
if (!(Test-IsEmpty $RootPath)) {
$RootPath = Get-PodeRelativePath -Path $RootPath -RootPath $MyInvocation.PSScriptRoot -JoinRoot -Resolve -TestPath
}

# create main context object
$PodeContext = New-PodeContext -ScriptBlock $ScriptBlock `
$PodeContext = New-PodeContext `
-ScriptBlock $ScriptBlock `
-FilePath $FilePath `
-Threads $Threads `
-Interval $Interval `
-ServerRoot (Protect-PodeValue -Value $RootPath -Default $MyInvocation.PSScriptRoot) `
Expand Down

0 comments on commit 1f57463

Please sign in to comment.