From d517c9b75b4d4214b857d511a2450794b732b961 Mon Sep 17 00:00:00 2001 From: Stephan Yelle Date: Wed, 14 Jan 2026 02:09:54 -0500 Subject: [PATCH] Add src/router.ps1 --- src/router.ps1 | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/router.ps1 diff --git a/src/router.ps1 b/src/router.ps1 new file mode 100644 index 0000000..1e82597 --- /dev/null +++ b/src/router.ps1 @@ -0,0 +1,79 @@ +function Invoke-TasksCompleted { + param($Context) + Write-LogHybrid "All UI-selected tasks processed" Info UI -LogToEvent + Send-Text $Context "Tasks completion acknowledged." +} + +function Dispatch-Request { + param($Context) + + $path = $Context.Request.Url.AbsolutePath.TrimStart('/') + + if ($path -eq 'quit') { + Write-LogHybrid "Shutdown requested" "Info" "Server" -LogToEvent + Send-Text $Context "Server shutting down." + $Global:Listener.Stop() + return + } + + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'tasksCompleted') { + Invoke-TasksCompleted $Context + return + } + + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'getpw') { + Invoke-FetchSites $Context + return + } + + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'renameComputer') { + Invoke-RenameComputer $Context + return + } + + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'getprinters') { + Invoke-GetPrinters $Context + return + } + + if ($Context.Request.HttpMethod -eq 'POST' -and $path -eq 'installprinters') { + Invoke-InstallPrinters $Context + return + } + + if ($path -in @('', 'onboard', 'offboard', 'devices')) { + $page = if ($path -eq '') { 'onboard' } else { $path } + $html = Get-UIHtml -Page $page + Send-HTML $Context $html + return + } + + $task = $Global:SamyTasks | Where-Object Name -EQ $path | Select-Object -First 1 + if ($task) { + + $fn = Get-TaskHandlerName -Task $task + if ([string]::IsNullOrWhiteSpace($fn)) { + $Context.Response.StatusCode = 500 + Send-Text $Context "Task '$($task.Label)' is missing a handler (HandlerFn/Handler/Fn/Name)." + return + } + + $cmd = Get-Command -Name $fn -ErrorAction SilentlyContinue + if (-not $cmd) { + $Context.Response.StatusCode = 500 + Send-Text $Context "Handler not found: $fn" + return + } + + if ($cmd.Parameters.ContainsKey('Context')) { + & $fn -Context $Context + } + else { + & $fn + } + return + } + + $Context.Response.StatusCode = 404 + Send-Text $Context '404 - Not Found' +}