-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpodex.ps1
More file actions
109 lines (97 loc) · 4.58 KB
/
podex.ps1
File metadata and controls
109 lines (97 loc) · 4.58 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
Import-Module -Name 'PSSQLite' -MaximumVersion 1.99.99 -Force
Import-Module -Name 'Pode' -MaximumVersion 2.99.99 -Force
function Write-FormattedLog {
param([string]$tag, [string]$log, [switch]$save)
switch ($tag) {
'debug' { $icon = '🐞' }
'database' { $icon = '💾' }
'api' { $icon = '🔗' }
'informational' { $icon = 'ℹ️' }
'verbose' { $icon = '🔍' }
'warning' { $icon = '⚠️' }
'error' { $icon = '❌' }
default { $icon = '✅' }
}
$timestamp = Get-Date -Format 'yyyyMMddHHmmss'
$prefix = '{0} {1} {2} ' -f $timestamp, $tag.PadRight(11), $icon
Write-PodeHost $prefix -NoNewLine
$maxLineLength = [int]($Host.UI.RawUI.WindowSize.Width - $prefix.Length - 1)
$currentPosition = 0
while ($currentPosition -lt $log.Length) {
$endPosition = [math]::Min(($log.Length - $currentPosition), $maxLineLength)
$line = $log.Substring($currentPosition, $endPosition)
if ($currentPosition -ne 0) {
Write-PodeHost "$(' ' * $($prefix.Length))$($line)"
} else {
Write-PodeHost "$($line)"
}
$currentPosition += $line.Length
}
if ($save) {
$log | Out-File -FilePath "./$($WebEvent.Request.Url.AbsolutePath)/$($WebEvent.Method).json" -Force
}
}
function Remove-UnsafeCharacter {
param([string]$inputString)
$inputString = $inputString -replace "'", "''"
$inputString = $inputString -replace '"', '\"'
$inputString = $inputString -replace ';', '\;'
$inputString = $inputString -replace '--', '\-\-'
$inputString = $inputString -replace '/\*', '/\*'
$inputString = $inputString -replace '\*/', '\*/'
$inputString = $inputString -replace '\\', '\\\\'
return $inputString
}
# Start-PodeServer -Name 'Podex' -ConfigFile '.\podex.psd1' -Threads 5 -ScriptBlock {
Start-PodeServer -Name 'Podex' -Threads 5 -ScriptBlock {
# get config
$cfg = (Get-PodeConfig)
Set-PodeViewEngine -Type Pode
# setup logging
New-PodeLoggingMethod -File -Path './logs' -Name 'requests' | Enable-PodeRequestLogging
if ($cfg.Podex.Debug) {
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
} else {
New-PodeLoggingMethod -File -Path './logs' -Name 'errors' | Enable-PodeErrorLogging
}
# create appropriate endpoint
if ($($cfg.PodeCfg.HttpsEnabled) -and $($cfg.PodeCfg.CertThumbprint) -ne '') {
Add-PodeEndpoint -Address $($cfg.PodeCfg.HttpUrl) -Port $($cfg.PodeCfg.HttpPort) -Protocol Https -CertificateThumbprint $($cfg.PodeCfg.CertThumbprint) -CertificateStoreLocation LocalMachine
} else {
Add-PodeEndpoint -Address $($cfg.PodeCfg.HttpUrl) -Port $($cfg.PodeCfg.HttpPort) -Protocol Http
}
# static routes
Add-PodeStaticRoute -Path '/public' -Source './public'
# front-end routes
Add-PodeRoute -Path '/' -Method Get, Post -ScriptBlock { Write-PodeViewResponse -Path 'layouts/main' -Data @{ PageName = 'Home'; Title = 'Podex - PowerShell/Pode + htmx Framework for Building Web Applications'; Components = @('about'); } }
Add-PodeRoute -Path '/crudmgr' -Method Get, Post -ScriptBlock { Write-PodeViewResponse -Path 'layouts/main' -Data @{ PageName = 'CRUDMgr'; Title = 'Podex - CRUD Management Demo'; Components = @('crudmgr'); } }
# htmx routes (html only)
Add-PodeRoute -Path '/htmx/hello' -Method Get -FilePath './htmx/hello.ps1'
Add-PodeRoute -Path '/htmx/crudmgr-new' -Method Get -ScriptBlock { Write-PodeViewResponse -Path 'layouts/bare' -Data @{ Components = @('crud-new'); } }
# file-based api routes (json or html)
foreach ($file in (Get-ChildItem -Path './api' -Filter *.ps1 -Recurse -File)) {
$method = (Get-Culture).TextInfo.ToTitleCase($file.Name) -replace '\.ps1$', ''
$relativePath = $file.FullName -replace [regex]::Escape($PWD.Path + '\'), '' -replace '\\', '/'
$apiPath = '/' + ($relativePath -replace '\.ps1$', '')
if ($method -in @('Get', 'Post', 'Put', 'Delete')) {
$apiPath = $apiPath -replace "/$($method)", ''
} elseif ($cfg.Podex.Debug -and $relativePath -match '/debug/') {
$apiPath = $apiPath -replace '/debug', ''
$method = 'Get'
} else {
$method = 'Get'
}
Add-PodeRoute -Path $apiPath -Method $method -FilePath $file.FullName
}
# show routes
if ($cfg.Podex.Debug) {
foreach ($route in (Get-PodeRoute | Sort-Object -Unique -Property Path, Method)) {
# Write-FormattedLog -tag 'routes' -log "$($route.Path.PadRight(30)) -> $($route.Method.PadRight(10)) -> $($logic)"
Write-FormattedLog -tag 'routes' -log "$($route.Path.PadRight(30)) -> $($route.Method.PadRight(10))"
}
}
# api docs
Enable-PodeOpenApi -RouteFilter '/api/*' -Path '/docs/openapi'
Add-PodeOAInfo -Title 'Podex - OpenAPI 3.0' -Version 0.0.1 -Description 'Podex API'
Enable-PodeOAViewer -Type Swagger -Path '/docs/swagger' -DarkMode -Title 'Podex API' -OpenApiUrl '/docs/openapi'
}