Skip to content

Commit 21f39f0

Browse files
author
James Brundage
committed
feat: Start-Htmx ( Fixes #7 )
Adding -LifeSpan (support for ephemeral servers)
1 parent 0f06217 commit 21f39f0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Commands/Start-Htmx.ps1

+16
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ function Start-Htmx {
107107
[ScriptBlock]
108108
$Handler,
109109

110+
# The lifespan of the server. After this time, the server will stop.
111+
[Parameter(ValueFromPipelineByPropertyName)]
112+
[timespan]
113+
$LifeSpan,
114+
110115
# The name of the palette to use. This will include the [4bitcss](https://4bitcss.com) stylesheet.
111116
[Alias('Palette','ColorScheme','ColorPalette')]
112117
[ArgumentCompleter({
@@ -156,6 +161,7 @@ function Start-Htmx {
156161
$ExecutionContext.SessionState.PSVariable.Set($keyValuePair.Key, $keyValuePair.Value)
157162
}
158163
# Start the listener
164+
$ServerStartTime = [DateTime]::Now
159165
$httpListener.Start()
160166
# If we have routes, we will cache all of their possible parameters now
161167
if ($route.Count) {
@@ -179,6 +185,16 @@ function Start-Htmx {
179185
# While the server is listening
180186
while ($httpListener.IsListening) {
181187
try {
188+
# If the server has a lifespan, we will stop it after the lifespan has passed.
189+
if (
190+
$LifeSpan -is [timespan] -and
191+
$lifeSpan.TotalMilliseconds -and
192+
[DateTime]::Now -gt $ServerStartTime.Add($LifeSpan)
193+
) {
194+
$httpListener.Stop()
195+
break
196+
}
197+
182198
# Try to get a the next context
183199
$contextAsync = $httpListener.GetContextAsync()
184200
$context = $contextAsync.Result

0 commit comments

Comments
 (0)