forked from Badgerati/Pode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb-static-auth.ps1
51 lines (39 loc) · 1.75 KB
/
web-static-auth.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
$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 server, and start listening on port 8085
Start-PodeServer -Threads 2 {
# listen on localhost:8085
Add-PodeEndpoint -Address * -Port 8085 -Protocol Http
New-PodeLoggingMethod -Terminal | Enable-PodeRequestLogging
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
# setup basic auth (base64> username:password in header)
New-PodeAuthScheme -Basic -Realm 'Pode Static Page' | Add-PodeAuth -Name 'Validate' -Sessionless -ScriptBlock {
param($username, $password)
# here you'd check a real user storage, this is just for example
if ($username -eq 'morty' -and $password -eq 'pickle') {
return @{
User = @{
ID ='M0R7Y302'
Name = 'Morty'
Type = 'Human'
}
}
}
return @{ Message = 'Invalid details supplied' }
}
# set view engine to pode renderer
Set-PodeViewEngine -Type Pode
# STATIC asset folder route
Add-PodeStaticRoute -Path '/assets' -Source './assets' -Defaults @('index.html') -Authentication 'Validate'
Add-PodeStaticRoute -Path '/assets/download' -Source './assets' -DownloadOnly
# GET request for web page on "localhost:8085/"
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
Write-PodeViewResponse -Path 'web-static' -Data @{ 'numbers' = @(1, 2, 3); }
}
# GET request to download a file from static route
Add-PodeRoute -Method Get -Path '/download' -ScriptBlock {
Set-PodeResponseAttachment -Path '/assets/images/Fry.png'
}
}