-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPodeServer.ps1
275 lines (230 loc) · 10.7 KB
/
PodeServer.ps1
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<#
.SYNOPSIS
Start Pode server
.DESCRIPTION
Test if it's running on Windows, then test if it's running with elevated Privileges, and start a new session if not.
.EXAMPLE
pwsh .\PodePSHTML\PodeServer.ps1
#>
[CmdletBinding()]
param ()
#region functions
function Initialize-FileWatcher {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]$Watch
)
begin{
$function = $($MyInvocation.MyCommand.Name)
Write-Verbose $('[', (Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff'), ']', '[ Begin ]', "$($function)", $Watch -Join ' ')
}
process{
Add-PodeFileWatcher -Name PodePSHTML -Path $Watch -ScriptBlock {
Write-Verbose "$($FileEvent.Name) -> $($FileEvent.Type) -> $($FileEvent.FullPath)"
$BinPath = Join-Path -Path $($PSScriptRoot) -ChildPath 'bin'
try{
" - Received: $($FileEvent.Name) at $($FileEvent.Timestamp)" | Out-Default
switch -Regex ($FileEvent.Type){
'Created|Changed' {
# Move-Item, New-Item
switch -Regex ($FileEvent.Name){
'index.txt' {
Start-Sleep -Seconds 3
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
. $(Join-Path $BinPath -ChildPath 'New-PshtmlIndexPage.ps1') -Title 'Index' -Request 'FileWatcher'
}
'pode.txt' {
Start-Sleep -Seconds 3
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
. $(Join-Path $BinPath -ChildPath 'New-PshtmlPodeServerPage.ps1') -Title 'Pode Server' -Request 'FileWatcher'
}
'asset.txt' {
Start-Sleep -Seconds 3
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
. $(Join-Path $BinPath -ChildPath 'New-PshtmlUpdateAssetPage.ps1') -Title 'Update Assets' -Request 'FileWatcher'
}
}
$index = Join-Path -Path $($PSScriptRoot) -ChildPath 'views/index.pode'
$content = Get-Content $index
$content -replace 'Created at\s\d{4}\-\d{2}\-\d{2}\s\d{2}\:\d{2}\:\d{2}', "Created at $(Get-Date -f 'yyyy-MM-dd HH:mm:ss')" | Set-Content -Path $index -Force -Confirm:$false
}
'Deleted' {
# Move-Item, Remove-Item
}
'Renamed' {
# Rename-Item
}
default {
" - $($FileEvent.Type): is not supported" | Out-Default
}
}
}catch{
Write-Warning "$($function): An error occured on line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
$Error.Clear()
}
} -Verbose
}
end{
Write-Verbose $('[', (Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff'), ']', '[ End ]', "$($MyInvocation.MyCommand.Name)" -Join ' ')
}
}
function Initialize-WebEndpoints {
[CmdletBinding()]
param()
begin{
$function = $($MyInvocation.MyCommand.Name)
Write-Verbose $('[', (Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff'), ']', '[ Begin ]', "$($function)", $Watch -Join ' ')
}
process{
# Index
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
Write-PodeViewResponse -Path 'Index.pode'
}
# Pode Server
Add-PodeRoute -Method Get -Path '/pode' -ScriptBlock {
Write-PodeViewResponse -Path 'Pode-Server.pode'
}
# Update Assets
Add-PodeRoute -Method Get -Path '/update' -ScriptBlock {
Write-PodeViewResponse -Path 'Update-Assets.pode'
}
# SQLite Data
Add-PodeRoute -Method Get -Path '/sqlite' -ScriptBlock {
Write-PodeViewResponse -Path 'SQLite-Data.pode'
}
# Pester Result
Add-PodeRoute -Method Get -Path '/pester' -ScriptBlock {
Write-PodeViewResponse -Path 'Pester-Result.pode'
}
# Mermaid Diagram
Add-PodeRoute -Method Get -Path '/mermaid' -ScriptBlock {
Write-PodeViewResponse -Path 'Mermaid-Diagram.pode'
}
# Help
Add-PodeRoute -Method Get -Path '/help' -ScriptBlock {
Write-PodeViewResponse -Path 'Help.pode'
}
}
end{
Write-Verbose $('[', (Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff'), ']', '[ End ]', "$($MyInvocation.MyCommand.Name)" -Join ' ')
}
}
function Initialize-ApiEndpoints {
[CmdletBinding()]
param()
begin{
$function = $($MyInvocation.MyCommand.Name)
Write-Verbose $('[', (Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff'), ']', '[ Begin ]', "$($function)", $Watch -Join ' ')
}
process{
$BinPath = Join-Path -Path $($PSScriptRoot) -ChildPath 'bin'
$PesterPath = Join-Path -Path $($BinPath).Replace('bin','upload') -ChildPath 'pstests.xml'
Add-PodeRoute -Method Post -Path '/api/index' -ArgumentList @($BinPath) -ScriptBlock {
param($BinPath)
if($CurrentOS -eq [OSType]::Windows){Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force}
$Response = . $(Join-Path $BinPath -ChildPath 'New-PshtmlIndexPage.ps1') -Title 'Index' -Request 'API'
Write-PodeJsonResponse -Value $Response
}
Add-PodeRoute -Method Post -Path '/api/pode' -ArgumentList @($BinPath) -ScriptBlock {
param($BinPath)
if($CurrentOS -eq [OSType]::Windows){Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force}
$Response = . $(Join-Path $BinPath -ChildPath 'New-PshtmlPodeServerPage.ps1') -Title 'Pode Server' -Request 'API'
Write-PodeJsonResponse -Value $Response
}
Add-PodeRoute -Method Post -Path '/api/asset' -ArgumentList @($BinPath) -ScriptBlock {
param($BinPath)
if($CurrentOS -eq [OSType]::Windows){Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force}
$Response = . $(Join-Path $BinPath -ChildPath 'New-PshtmlUpdateAssetPage.ps1') -Title 'Update Assets' -Request 'API'
Write-PodeJsonResponse -Value $Response
}
Add-PodeRoute -Method Post -Path '/api/sqlite' -ContentType 'application/text' -ArgumentList @($BinPath) -ScriptBlock {
param($BinPath)
if($CurrentOS -eq [OSType]::Windows){Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force}
$Response = . $(Join-Path $BinPath -ChildPath 'New-PshtmlSQLitePage.ps1') -Title 'SQLite Data' -Request 'API' -TsqlQuery $WebEvent.Data
Write-PodeJsonResponse -Value $Response
}
Add-PodeRoute -Method Post -Path '/api/pester' -ContentType 'application/json' -ArgumentList @($BinPath, $PesterPath) -ScriptBlock {
param($BinPath, $PesterPath)
if($CurrentOS -eq [OSType]::Windows){Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force}
Import-Module Pester
if($WebEvent.Data -is [system.array]){
$data = $WebEvent.Data
}else{
$data = @('example.ch')
}
# In a container it's possible to pass variables
$ContainerSplat = @{
Path = $(Join-Path $BinPath -ChildPath 'Invoke-PesterResult.Tests.ps1')
Data = @{ Destination = $data}
}
$container = New-PesterContainer @ContainerSplat
# Exclude Tests with the Tag NotRun
$PesterData = Invoke-Pester -Container $container -PassThru -Output None -ExcludeTagFilter NotRun
$Response = . $(Join-Path $BinPath -ChildPath 'New-PshtmlPesterPage.ps1') -Title 'Pester Result' -Request 'API' -PesterData $PesterData
if([String]::IsNullOrEmpty($Response)){
Write-PodeJsonResponse -Value 'Could not read pester results' -StatusCode 400
}else{
Write-PodeJsonResponse -Value $Response
}
}
Add-PodeRoute -Method Post -Path '/api/mermaid' -ContentType 'application/text' -ArgumentList @($BinPath) -ScriptBlock {
param($BinPath)
if($CurrentOS -eq [OSType]::Windows){Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force}
$Response = . $(Join-Path $BinPath -ChildPath 'New-PshtmlMermaidPage.ps1') -Title 'Mermaid Diagram' -Request 'API' -TsqlQuery $WebEvent.Data
Write-PodeJsonResponse -Value $Response
}
Add-PodeRoute -Method Post -Path '/api/help' -ArgumentList @($BinPath) -ScriptBlock {
param($BinPath)
if($CurrentOS -eq [OSType]::Windows){Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force}
$Response = . $(Join-Path $BinPath -ChildPath 'New-PshtmlHelpPage.ps1') -Title 'Help' -Request 'API'
Write-PodeJsonResponse -Value $Response
}
}
end{
Write-Verbose $('[', (Get-Date -f 'yyyy-MM-dd HH:mm:ss.fff'), ']', '[ End ]', "$($MyInvocation.MyCommand.Name)" -Join ' ')
}
}
#endregion
#region Main
enum OSType {
Linux
Mac
Windows
}
if($PSVersionTable.PSVersion.Major -lt 6){
$CurrentOS = [OSType]::Windows
}else{
if($IsMacOS) {$CurrentOS = [OSType]::Mac}
if($IsLinux) {$CurrentOS = [OSType]::Linux}
if($IsWindows){$CurrentOS = [OSType]::Windows}
}
#endregion
#region Pode server
if($CurrentOS -eq [OSType]::Windows){
$Address = 'localhost'
}else{
$Address = '*'
}
# We'll use 2 threads to handle API requests
Start-PodeServer -Thread 2 {
Write-Host "Press Ctrl. + C to terminate the Pode server" -ForegroundColor Yellow
if($CurrentOS -eq [OSType]::Mac){
Write-Host "Re-builds of pages not supportet on $($CurrentOS), because mySQLite support only Windows and Linux" -ForegroundColor Red
}
# Enables Error Logging
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
# Add listener to Port 8080 for Protocol http
Add-PodeEndpoint -Address $Address -Port 8080 -Protocol Http
# Set the engine to use and render .pode files
Set-PodeViewEngine -Type Pode
# Add File Watcher
$WatcherPath = Join-Path -Path $($PSScriptRoot) -ChildPath 'upload'
Initialize-FileWatcher -Watch $WatcherPath
# Set Pode endpoints for the web pages
Initialize-WebEndpoints
# Set Pode endpoints for the api
Initialize-ApiEndpoints
} -Verbose
#endregion