-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathWeb-PagesUsing.ps1
More file actions
138 lines (104 loc) · 4.22 KB
/
Web-PagesUsing.ps1
File metadata and controls
138 lines (104 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<#
.SYNOPSIS
A sample PowerShell script to set up a Pode server with various routes, middleware, and custom functions.
.DESCRIPTION
This script sets up a Pode server listening on port 8081. It demonstrates how to handle GET requests,
use middleware, export and use custom functions, and set up timers. The script includes examples of
using `$using:` scope for variables in script blocks and middleware.
.EXAMPLE
To run the sample: ./Web-PagesUsing.ps1
# Root route
Invoke-RestMethod -Uri http://localhost:8081/ -Method Get
# Random route
Invoke-RestMethod -Uri http://localhost:8081/random -Method Get
# Inner function route
Invoke-RestMethod -Uri http://localhost:8081/inner-func -Method Get
# Outer function route
Invoke-RestMethod -Uri http://localhost:8081/outer-func -Method Get
# Greetings route
Invoke-RestMethod -Uri http://localhost:8081/greetings -Method Get
# Sub-greetings route
Invoke-RestMethod -Uri http://localhost:8081/sub-greetings -Method Get
# Timer route
Invoke-RestMethod -Uri http://localhost:8081/timer -Method Get
.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Web-PagesUsing.ps1
.NOTES
Author: Pode Team
License: MIT License
#>
try {
# Determine the script path and Pode module path
$ScriptPath = (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
$podePath = Split-Path -Parent -Path $ScriptPath
# Import the Pode module from the source path if it exists, otherwise from installed modules
if (Test-Path -Path "$($podePath)/src/Pode.psm1" -PathType Leaf) {
Import-Module "$($podePath)/src/Pode.psm1" -Force -ErrorAction Stop
}
else {
Import-Module -Name 'Pode' -MaximumVersion 2.99 -ErrorAction Stop
}
}
catch { throw }
# or just:
# Import-Module Pode
$outerfoo = 'outer-bar'
$outer_ken = 'Hello, there'
function Write-MyOuterResponse {
Write-PodeJsonResponse -Value @{ Message = 'From an outer function' }
}
# create a server, and start listening on port 8081
Start-PodeServer -Threads 2 {
# listen on localhost:8081
Add-PodeEndpoint -Address localhost -Port 8081 -Protocol Http
# log requests to the terminal
New-PodeLoggingMethod -Terminal -Batch 10 -BatchTimeout 10 | Enable-PodeRequestLogging
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
# set view engine to pode renderer
Set-PodeViewEngine -Type Pode
# load file funcs
Use-PodeScript -Path ./modules/Imported-Funcs.ps1
$innerfoo = 'inner-bar'
$inner_ken = 'General Kenobi'
function Write-MyInnerResponse {
Write-PodeJsonResponse -Value @{ Message = 'From an inner function' }
}
Export-PodeFunction -Name 'Write-MyOuterResponse', 'Write-MyInnerResponse'
New-PodeMiddleware -ScriptBlock {
"M1: $($using:outer_ken) ... $($using:inner_ken)" | Out-Default
return $true
} | Add-PodeMiddleware -Name 'TestUsingMiddleware1'
Add-PodeMiddleware -Name 'TestUsingMiddleware2' -ScriptBlock {
"M2: $($using:outer_ken) ... $($using:inner_ken)" | Out-Default
return $true
}
# GET request for web page on "localhost:8081/"
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
$using:innerfoo | Out-Default
$using:outerfoo | Out-Default
$using:innerfoo | Out-Default
$WebEvent.Method | Out-Default
Write-PodeViewResponse -Path 'simple' -Data @{ 'numbers' = @(1, 2, 3); }
}
Add-PodeRoute -Method Get -Path '/random' -ScriptBlock {
Write-PodeJsonResponse -Value @{ Message = "$($using:outer_ken) ... $($using:inner_ken)" }
}
Add-PodeRoute -Method Get -Path '/inner-func' -ScriptBlock {
Write-MyInnerResponse
}
Add-PodeRoute -Method Get -Path '/outer-func' -ScriptBlock {
Write-MyOuterResponse
}
Add-PodeRoute -Method Get -Path '/greetings' -ScriptBlock {
Write-MyGreeting
}
Add-PodeRoute -Method Get -Path '/sub-greetings' -ScriptBlock {
Write-MySubGreeting
}
Add-PodeTimer -Name 'empty' -Interval 60 -ScriptBlock {}
Add-PodeRoute -Method Get -Path '/timer' -ScriptBlock {
Add-PodeTimer -Name 'inner_timer' -Interval 5 -ScriptBlock {
$using:innerfoo | Out-PodeHost
}
}
}