Add src/router.ps1
This commit is contained in:
79
src/router.ps1
Normal file
79
src/router.ps1
Normal file
@@ -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'
|
||||
}
|
||||
Reference in New Issue
Block a user