This repository has been archived by the owner on Sep 22, 2024. It is now read-only.
forked from PowerShell/Polaris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolaris.psm1
869 lines (781 loc) · 24.9 KB
/
Polaris.psm1
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
if ($PSVersionTable.PSEdition -eq "Core")
{
Add-Type -Path "$PSScriptRoot/PolarisCore/bin/Debug/netstandard2.0/Polaris.dll"
}
else
{
Add-Type -Path "$PSScriptRoot/PolarisCore/bin/Debug/net451/Polaris.dll"
}
$script:Polaris = $null
# Handles the removal of the module
$ExecutionContext.SessionState.Module.OnRemove =
{
If ( $script:Polaris )
{
Stop-Polaris -ErrorAction SilentlyContinue
Clear-Polaris
}
}.GetNewClosure()
<#
.SYNOPSIS
Add web route.
.DESCRIPTION
Create web route for server to serve.
.PARAMETER Path
Path (path/route/endpoint) of the web route to to be serviced.
.PARAMETER Method
HTTP verb/method to be serviced.
Valid values are GET, POST, PUT, and DELETE
.PARAMETER ScriptBlock
ScriptBlock that will be triggered when web route is called.
.PARAMETER ScriptPath
Full path and name of script that will be triggered when web route is called.
.PARAMETER Force
Use -Force to overwrite any existing web route for the same path and method.
.EXAMPLE
New-PolarisRoute -Path "helloworld" -Method "GET" -ScriptBlock { $response.Send( 'Hello World' ) }
To view results:
Start-Polaris
Start-Process http://localhost:8080/helloworld
.EXAMPLE
New-PolarisRoute -Path "helloworld" -Method "GET" -ScriptPath D:\Scripts\Example.ps1
To view results, assuming default port:
Start-Polaris
Start-Process http://localhost:8080/helloworld
#>
function New-PolarisRoute
{
[CmdletBinding()]
param(
[Parameter( Mandatory = $True, Position = 0 )]
[string]
$Path,
[Parameter( Mandatory = $True, Position = 1 )]
[ValidateSet( 'GET', 'POST', 'PUT', 'DELETE' )]
[string]
$Method,
[Parameter( Mandatory = $True, Position = 2, ParameterSetName = 'ScriptBlock' )]
[scriptblock]
$ScriptBlock,
[Parameter( Mandatory = $True, ParameterSetName = 'ScriptPath' )]
[string]
$ScriptPath,
[switch]
$Force )
$ExistingWebRoute = Get-PolarisRoute -Path $Path -Method $Method
if ( $ExistingWebRoute -and $Force )
{
Remove-PolarisRoute -Path $Path -Method $Method
$ExistingWebRoute = Get-PolarisRoute -Path $Path -Method $Method
}
if ( $ExistingWebRoute )
{
$PSCmdlet.WriteError( (
New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
[System.Exception]'WebRoute already exists.'
$Null
[System.Management.Automation.ErrorCategory]::ResourceExists
"$Path,$Method" ) ) )
}
else
{
CreateNewPolarisIfNeeded
if ( -not $Path.StartsWith( '/' ) )
{
$Path = '/' + $Path
}
switch ( $PSCmdlet.ParameterSetName )
{
'ScriptBlock'
{
$script:Polaris.AddRoute( $Path, $Method, [string]$ScriptBlock )
}
'ScriptPath'
{
if ( Test-Path -Path $ScriptPath )
{
$Script = Get-Content -Path $ScriptPath -Raw
$script:Polaris.AddRoute( $Path, $Method, $Script )
}
else
{
$PSCmdlet.WriteError( (
New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
[System.Exception]'ScriptPath not found.'
$Null
[System.Management.Automation.ErrorCategory]::ObjectNotFound
$ScriptPath ) ) )
}
}
}
}
}
<#
.SYNOPSIS
Removes the web route.
.DESCRIPTION
Removes the web route(s) matching the specified path(s) and method(s).
.PARAMETER Path
Path(s) of the route(s) to remove.
Accepts multiple values and wildcards.
Accepts pipeline input.
Accepts pipeline input by property name.
Defaults to all paths (*).
.PARAMETER Method
Method(s) of the route(s) to remove.
Accepts pipeline input by property name.
Accepts multiple values and wildcards.
Defaults to all methods (*).
.EXAMPLE
Remove-PolarisRoute
Removes all existing web routes.
.EXAMPLE
Remove-PolarisRoute -Path 'helloworld' -Method GET
Removes the web route for method GET for path helloworld.
.EXAMPLE
Remove-PolarisRoute -Path 'sub1/sub2/*'
Removes all web routes for all methods for paths starting with sub1/sub2/.
.EXAMPLE
Remove-PolarisRoute -Path 'sub1/sub2/*' -Method GET, POST
Removes all web routes for GET and POST methods for paths starting with sub1/sub2/.
.EXAMPLE
Get-PolarisRoute -Method Delete | Remove-Method
Removes all web routes for DELETE methods for all paths.
#>
function Remove-PolarisRoute
{
[CmdletBinding()]
param (
[Parameter( ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True )]
[string[]]
$Path = '*',
[Parameter( ValueFromPipelineByPropertyName = $True )]
[string[]]
$Method = '*' )
process
{
if ( $script:Polaris )
{
$WebRoutes = Get-PolarisRoute -Path $Path -Method $Method
ForEach ( $Route in $WebRoutes )
{
$script:Polaris.RemoveRoute( $Route.Path, $Route.Method )
}
}
}
}
<#
.SYNOPSIS
Get web routes.
.DESCRIPTION
Get web routes filtered by Path and Method, as specified.
.PARAMETER Path
Path of the route(s) to get.
Accepts pipeline input.
Accepts pipeline input by property name.
Accepts multiple values and wildcards.
Defaults to all paths (*).
.PARAMETER Method
Method(s) of the route(s) to get.
Accepts pipeline input by property name.
Accepts multiple values and wildcards.
Defaults to all methods (*).
.EXAMPLE
Get-PolarisRoute
Gets all web routes.
.EXAMPLE
Get-PolarisRoute -Path 'helloworld' -Method 'GET'
Gets the web route for method GET for path helloworld.
.EXAMPLE
Get-PolarisRoute -Path 'sub1/sub2/*'
Gets all web routes for all methods for paths starting with sub1/sub2/.
.EXAMPLE
Get-PolarisRoute -Path 'sub1/sub2/*' -Method GET, POST
Gets all web routes for GET and POST methods for paths starting with sub1/sub2/.
.EXAMPLE
Get-PolarisRoute -Method Delete
Gets all web routes for DELETE methods for all paths.
#>
function Get-PolarisRoute
{
[CmdletBinding()]
param(
[Parameter( ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True )]
[string[]]
$Path = '*',
[Parameter( ValueFromPipelineByPropertyName = $True )]
[string[]]
$Method = '*' )
process
{
if ( $script:Polaris )
{
$WebRoutes = [System.Collections.ArrayList]@()
ForEach ( $Route in $script:Polaris.ScriptBlockRoutes.GetEnumerator() )
{
ForEach ( $RouteMethod in $Route.Value.GetEnumerator() )
{
$Null = $WebRoutes.Add( [pscustomobject]@{ Path = $Route.Key; Method = $RouteMethod.Key; Scriptblock = $RouteMethod.Value } )
}
}
$Filter = [scriptblock]::Create( (
'( ' +
( $Path.ForEach({ "`$_.Path -like `"$($_.TrimStart('/'))`"" } ) -join ' -or ' ) +
' ) -and ( ' +
( $Method.ForEach({ "`$_.Method -like `"$($_.TrimStart('/'))`"" } ) -join ' -or ' ) +
' )' ) )
$WebRoutes = $WebRoutes.Where( $Filter )
return $WebRoutes
}
}
}
<#
.SYNOPSIS
Creates web routes to recursively serve folder contents
.DESCRIPTION
Creates web routes to recursively serve folder contents. Perfect for static websites.
.PARAMETER RoutePath
Root route that the folder path will be served to.
Defaults to "/".
.PARAMETER FolderPath
Full path and name of the folder to serve.
.PARAMETER Force
Use -Force to overwrite existing web route(s) for the same paths.
.EXAMPLE
New-PolarisStaticRoute -RoutePath 'public' -Path D:\FolderShares\public
Creates web routes for GET method for each file recursively within D:\FolderShares\public
at relative path /public, for example, http://localhost:8080/public/File1.html
.EXAMPLE
Get-PolarisRoute -Path 'public/*' -Method GET | Remove-PolarisRoute
New-PolarisStaticRoute -RoutePath 'public' -Path D:\FolderShares\public
Updates website web routes. (Deletes all existing web routes and creates new web routes
for all existing folder content.)
.NOTES
Folders are not browsable. New files are not added dynamically.
#>
function New-PolarisStaticRoute
{
[CmdletBinding()]
param(
[string]
$RoutePath = "/",
[Parameter( Mandatory = $True )]
[string]
$FolderPath,
[switch]
$Force )
$ErrorAction = $PSBoundParameters["ErrorAction"]
If ( -not $ErrorAction )
{
$ErrorAction = $ErrorActionPreference
}
CreateNewPolarisIfNeeded
if ( -not ( Test-Path -Path $FolderPath ) )
{
ThrowError -ExceptionName FileNotFoundException -ExceptionMessage "Folder does not exist at path $FolderPath"
}
$allPaths = Get-ChildItem -Path $FolderPath -Recurse -File | ForEach-Object { $_.FullName }
$resolvedPath = ( Resolve-Path -Path $FolderPath ).Path
$RoutePath = $RoutePath.TrimEnd( '/' )
ForEach ( $Path in $AllPaths )
{
$StaticPath = "$RoutePath$( $Path.Substring( $ResolvedPath.Length ).Replace( '\' , '/' ) )"
If ( $PSVersionTable.PSEdition -eq "Core" )
{
$ByteParam = '-AsByteStream'
}
Else
{
$ByteParam = '-Encoding Byte'
}
$ScriptBlock = [ScriptBlock]::Create( @"
`$bytes = Get-Content -LiteralPath "$Path" $ByteParam -ReadCount 0
`$response.SetContentType( ( [PolarisCore.PolarisResponse]::GetContentType( "$Path" ) ) )
`$response.ByteResponse = `$bytes
"@ )
New-PolarisRoute -Path $StaticPath -Method GET -ScriptBlock $ScriptBlock -Force:$Force -ErrorAction:$ErrorAction
}
}
<#
.SYNOPSIS
Add new route middleware.
.DESCRIPTION
Creates new route middleware. Route middleware scripts are used to
manipulate request and response objects and run before web route scripts.
.PARAMETER Name
Name of the middleware.
.PARAMETER ScriptBlock
ScriptBlock to run when middleware is triggered.
.PARAMETER ScriptPath
Full path and name to script to run when middleware is triggered.
.PARAMETER Force
Use -Force to overwrite any existing middleware with the same name.
.EXAMPLE
$JsonBodyParserMiddlerware =
{
if ($Request.BodyString -ne $null) {
$Request.Body = $Request.BodyString | ConvertFrom-Json
}
}
New-PolarisRouteMiddleware -Name JsonBodyParser -ScriptBlock $JsonBodyParserMiddleware
#>
function New-PolarisRouteMiddleware
{
[CmdletBinding()]
param(
[Parameter( Mandatory = $True, Position = 0 )]
[string]
$Name,
[Parameter( Mandatory = $True, Position = 1, ParameterSetName = 'ScriptBlock' )]
[scriptblock]
$ScriptBlock,
[Parameter( Mandatory = $True, ParameterSetName = 'ScriptPath' )]
[string]
$ScriptPath,
[switch]
$Force )
$ExistingMiddleWare = Get-PolarisRouteMiddleware -Name $Name
if ( $ExistingMiddleWare -and $Force )
{
Remove-PolarisRouteMiddleware -Name $Name
$ExistingMiddleWare = Get-PolarisRouteMiddleware -Name $Name
}
if ( $ExistingMiddleWare )
{
$PSCmdlet.WriteError( (
New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
[System.Exception]'RouteMiddleware already exists.'
$Null
[System.Management.Automation.ErrorCategory]::ResourceExists
"$Name" ) ) )
}
else
{
CreateNewPolarisIfNeeded
switch ( $PSCmdlet.ParameterSetName )
{
'ScriptBlock'
{
$script:Polaris.AddMiddleware( $Name, [string]$ScriptBlock )
}
'ScriptPath'
{
if ( Test-Path -Path $ScriptPath )
{
$Script = Get-Content -Path $ScriptPath -Raw
$script:Polaris.AddMiddleware( $Name, $Script )
}
else
{
$PSCmdlet.WriteError( (
New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList @(
[System.Exception]'ScriptPath not found.'
$Null
[System.Management.Automation.ErrorCategory]::ObjectNotFound
$ScriptPath ) ) )
}
}
}
}
}
<#
.SYNOPSIS
Remove route middleware.
.DESCRIPTION
Remove route middleware matching the specified name(s).
.PARAMETER Name
Name of the middleware to remove.
Accepts multiple values and wildcards.
Accepts pipeline input.
Accepts pipeline input by property name.
Defaults to all names (*).
.EXAMPLE
Remove-PolarisRouteMiddleware -Name JsonBodyParser
.EXAMPLE
Remove-PolarisRouteMiddleware
Removes all route middleware.
.EXAMPLE
Remove-PolarisRouteMiddleware -Name ParamCheck*, ParamVerify*
Removes any route middleware with names starting with ParamCheck or ParamVerify.
.EXAMPLE
Get-PolarisRouteMiddleware | Remove-PolarisRouteMiddleware
Removes all route middleware.
#>
function Remove-PolarisRouteMiddleware
{
[CmdletBinding()]
param(
[Parameter( ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True )]
[string[]]
$Name = '*' )
process
{
if ( $script:Polaris )
{
$Middleware = Get-PolarisRouteMiddleware -Name $Name
ForEach ( $Ware in $MiddleWare )
{
$script:Polaris.RemoveMiddleware( $Ware.Name )
}
}
}
}
<#
.SYNOPSIS
Get route middleware.
.DESCRIPTION
Get route middleware matching the specified name(s).
.PARAMETER Name
Name of the middleware to get.
Accepts pipeline input.
Accepts pipeline input by property name.
Accepts multiple values and wildcards.
Defaults to all names (*).
.EXAMPLE
Get-PolarisRouteMiddleware
.EXAMPLE
Get-PolarisRouteMiddleware -Name JsonBodyParser
.EXAMPLE
Get-PolarisRouteMiddleware -Name ParamCheck*, ParamVerify*
#>
function Get-PolarisRouteMiddleware
{
[CmdletBinding()]
param(
[Parameter( ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True )]
[string[]]
$Name = '*' )
process
{
if ( $script:Polaris )
{
$Filter = [scriptblock]::Create( ( $Name.ForEach({ "`$_.Name -like `"$_`"" }) -join ' -or ' ) )
return $script:Polaris.RouteMiddleware.Where( $Filter )
}
}
}
<#
.SYNOPSIS
Start Polaris web server.
.DESCRIPTION
Start Polaris web server.
.PARAMETER Port
Port number to listen on.
Defaults to 8080.
.PARAMETER MinRunspaces
Minimum number of PowerShell runspaces for web server to use.
Defaults to 1.
.PARAMETER MaxRunspaces
Maximum number of PowerShell runspaces for web server to use.
Defaults to 1.
.PARAMETER UseJsonBodyParserMiddleware
When present, JSONBodyParser middleware will be created, if needed.
.EXAMPLE
Start-Polaris
.EXAMPLE
Start-Polaris -Port 8081 -MinRunspaces 2 -MaxRunspaces 10 -UseJsonBodyParserMiddleware
#>
function Start-Polaris {
[CmdletBinding()]
param(
[Int32]
$Port = 8080,
[Int32]
$MinRunspaces = 1,
[Int32]
$MaxRunspaces = 1,
[switch]
$UseJsonBodyParserMiddleware = $False )
CreateNewPolarisIfNeeded
if ( $UseJsonBodyParserMiddleware )
{
New-PolarisRouteMiddleware -Name JsonBodyParser -ScriptBlock $JsonBodyParserMiddlerware
}
$script:Polaris.Start( $Port, $MinRunspaces, $MaxRunspaces )
return $script:Polaris
}
<#
.SYNOPSIS
Stop Polaris web server.
.DESCRIPTION
Stop Polaris web server.
.PARAMETER ServerContext
Polaris instance to stop.
Defaults to the global instance.
.EXAMPLE
Stop-Polaris
.EXAMPLE
Stop-Polaris -ServerContext $app
#>
function Stop-Polaris
{
[CmdletBinding()]
param(
[PolarisCore.Polaris]
$ServerContext = $script:Polaris )
if ( $ServerContext )
{
$ServerContext.Stop()
}
}
##############################
# HELPERS
##############################
<#
.SYNOPSIS
Add web route with method GET
.DESCRIPTION
Wrapper for New-PolarisRoute -Method GET
.PARAMETER Path
Path (path/route/endpoint) of the web route to to be serviced.
.PARAMETER ScriptBlock
ScriptBlock that will be triggered when web route is called.
.PARAMETER ScriptPath
Full path and name of script that will be triggered when web route is called.
.PARAMETER Force
Use -Force to overwrite any existing web route for the same path and method.
.EXAMPLE
New-PolarisGetRoute -Path "helloworld" -ScriptBlock { $response.Send( 'Hello World' ) }
To view results:
Start-Polaris
Start-Process http://localhost:8080/helloworld
.EXAMPLE
New-PolarisGetRoute -Path "helloworld" -ScriptPath D:\Scripts\Example.ps1
To view results, assuming default port:
Start-Polaris
Start-Process http://localhost:8080/helloworld
#>
function New-PolarisGetRoute
{
[CmdletBinding()]
param(
[Parameter( Mandatory = $True, Position = 0 )]
[string]
$Path,
[Parameter( Mandatory = $True, Position = 1, ParameterSetName = 'ScriptBlock' )]
[scriptblock]
$ScriptBlock,
[Parameter( Mandatory = $True, ParameterSetName = 'ScriptPath' )]
[string]
$ScriptPath,
[switch]
$Force )
switch ( $PSCmdlet.ParameterSetName )
{
'ScriptBlock' { New-PolarisRoute -Path $Path -Method "GET" -ScriptBlock $ScriptBlock -Force:$Force }
'ScriptPath' { New-PolarisRoute -Path $Path -Method "GET" -ScriptPath $ScriptPath -Force:$Force }
}
}
<#
.SYNOPSIS
Add web route with method POST
.DESCRIPTION
Wrapper for New-PolarisRoute -Method POST
.PARAMETER Path
Path (path/route/endpoint) of the web route to to be serviced.
.PARAMETER ScriptBlock
ScriptBlock that will be triggered when web route is called.
.PARAMETER ScriptPath
Full path and name of script that will be triggered when web route is called.
.PARAMETER Force
Use -Force to overwrite any existing web route for the same path and method.
.EXAMPLE
New-PolarisGetRoute -Path "helloworld" -ScriptBlock { $response.Send( 'Hello World' ) }
To view results, assuming default port:
Start-Polaris
Invoke-WebRequest -Uri http://localhost:8080/helloworld -Method POST
.EXAMPLE
New-PolarisGetRoute -Path "helloworld" -ScriptPath D:\Scripts\Example.ps1
To view results, assuming default port:
Start-Polaris
Invoke-WebRequest -Uri http://localhost:8080/helloworld -Method POST
#>
function New-PolarisPostRoute
{
[CmdletBinding()]
param(
[Parameter( Mandatory = $True, Position = 0 )]
[string]
$Path,
[Parameter( Mandatory = $True, Position = 1, ParameterSetName = 'ScriptBlock' )]
[scriptblock]
$ScriptBlock,
[Parameter( Mandatory = $True, ParameterSetName = 'ScriptPath' )]
[string]
$ScriptPath,
[switch]
$Force )
switch ( $PSCmdlet.ParameterSetName )
{
'ScriptBlock' { New-PolarisRoute -Path $Path -Method "POST" -ScriptBlock $ScriptBlock -Force:$Force }
'ScriptPath' { New-PolarisRoute -Path $Path -Method "POST" -ScriptPath $ScriptPath -Force:$Force }
}
}
<#
.SYNOPSIS
Add web route with method PUT
.DESCRIPTION
Wrapper for New-PolarisRoute -Method PUT
.PARAMETER Path
Path (path/route/endpoint) of the web route to to be serviced.
.PARAMETER ScriptBlock
ScriptBlock that will be triggered when web route is called.
.PARAMETER ScriptPath
Full path and name of script that will be triggered when web route is called.
.PARAMETER Force
Use -Force to overwrite any existing web route for the same path and method.
.EXAMPLE
New-PolarisGetRoute -Path "helloworld" -ScriptBlock { $response.Send( 'Hello World' ) }
To view results, assuming default port:
Start-Polaris
Invoke-WebRequest -Uri http://localhost:8080/helloworld -Method PUT
.EXAMPLE
New-PolarisGetRoute -Path "helloworld" -ScriptPath D:\Scripts\Example.ps1
To view results, assuming default port:
Start-Polaris
Invoke-WebRequest -Uri http://localhost:8080/helloworld -Method PUT
#>
function New-PolarisPutRoute {
[CmdletBinding()]
param(
[Parameter( Mandatory = $True, Position = 0 )]
[string]
$Path,
[Parameter( Mandatory = $True, Position = 1, ParameterSetName = 'ScriptBlock' )]
[scriptblock]
$ScriptBlock,
[Parameter( Mandatory = $True, ParameterSetName = 'ScriptPath' )]
[string]
$ScriptPath,
[switch]
$Force )
switch ( $PSCmdlet.ParameterSetName )
{
'ScriptBlock' { New-PolarisRoute -Path $Path -Method "PUT" -ScriptBlock $ScriptBlock -Force:$Force }
'ScriptPath' { New-PolarisRoute -Path $Path -Method "PUT" -ScriptPath $ScriptPath -Force:$Force }
}
}
<#
.SYNOPSIS
Add web route with method DELETE
.DESCRIPTION
Wrapper for New-PolarisRoute -Method DELETE
.PARAMETER Path
Path (path/route/endpoint) of the web route to to be serviced.
.PARAMETER ScriptBlock
ScriptBlock that will be triggered when web route is called.
.PARAMETER ScriptPath
Full path and name of script that will be triggered when web route is called.
.PARAMETER Force
Use -Force to overwrite any existing web route for the same path and method.
.EXAMPLE
New-PolarisGetRoute -Path "helloworld" -ScriptBlock { $response.Send( 'Hello World' ) }
To view results, assuming default port:
Start-Polaris
Invoke-WebRequest -Uri http://localhost:8080/helloworld -Method DELETE
.EXAMPLE
New-PolarisGetRoute -Path "helloworld" -ScriptPath D:\Scripts\Example.ps1
To view results, assuming default port:
Start-Polaris
Invoke-WebRequest -Uri http://localhost:8080/helloworld -Method DELETE
#>
function New-PolarisDeleteRoute {
[CmdletBinding()]
param(
[Parameter( Mandatory = $True, Position = 0 )]
[string]
$Path,
[Parameter( Mandatory = $True, Position = 1, ParameterSetName = 'ScriptBlock' )]
[scriptblock]
$ScriptBlock,
[Parameter( Mandatory = $True, ParameterSetName = 'ScriptPath' )]
[string]
$ScriptPath,
[switch]
$Force )
switch ( $PSCmdlet.ParameterSetName )
{
'ScriptBlock' { New-PolarisRoute -Path $Path -Method "DELETE" -ScriptBlock $ScriptBlock -Force:$Force }
'ScriptPath' { New-PolarisRoute -Path $Path -Method "DELETE" -ScriptPath $ScriptPath -Force:$Force }
}
}
<#
.SYNOPSIS
Helper function that turns on the Json Body parsing middleware.
.DESCRIPTION
Helper function that turns on the Json Body parsing middleware.
.EXAMPLE
Use-PolarisJsonBodyParserMiddleware
#>
function Use-PolarisJsonBodyParserMiddleware {
[CmdletBinding()]
param()
New-PolarisRouteMiddleware -Name JsonBodyParser -ScriptBlock $JsonBodyParserMiddlerware -Force
}
<#
.SYNOPSIS
Returns the internal instance of Polaris
.DESCRIPTION
Returns the instance of the Polaris .NET Standard object
.EXAMPLE
Get-Polaris
.NOTES
Should only be used for testing
#>
function Get-Polaris {
return $script:Polaris
}
<#
.SYNOPSIS
Clears the internal instance of Polaris
.DESCRIPTION
Clears the internal Polaris .NET Standard object. The instance will be reinstantiated in other module calls.
.EXAMPLE
Clear-Polaris
.NOTES
Should only be used for testing
#>
function Clear-Polaris {
if ($script:Polaris) {
Remove-Variable -Name Polaris -Scope script
}
}
##############################
# INTERNAL
##############################
function CreateNewPolarisIfNeeded ()
{
if ( -not $script:Polaris )
{
$script:Polaris = New-Object -TypeName PolarisCore.Polaris -ArgumentList @(
[Action[string]]{ param($str) Write-Verbose "$str" } )
}
}
$JsonBodyParserMiddlerware =
{
if ( $Request.BodyString -ne $Null )
{
$Request.Body = $Request.BodyString | ConvertFrom-Json
}
}
Export-ModuleMember -Function `
Get-Polaris,
Clear-Polaris,
New-PolarisRoute,
Remove-PolarisRoute,
Get-PolarisRoute,
New-PolarisGetRoute,
New-PolarisPostRoute,
New-PolarisPutRoute,
New-PolarisDeleteRoute,
New-PolarisStaticRoute,
New-PolarisRouteMiddleware,
Remove-PolarisRouteMiddleware,
Get-PolarisRouteMiddleware,
Use-PolarisJsonBodyParserMiddleware,
Start-Polaris,
Stop-Polaris