UniversalAutomation.Dashboard.psm1

if ($Env:UADebug -eq $true) {
    $AssetId = [UniversalDashboard.Services.AssetService]::Instance.RegisterAsset("http://localhost:10002/ua.index.bundle.js")
    [UniversalDashboard.Services.AssetService]::Instance.RegisterAsset("http://localhost:10002/ua.vendors~index.bundle.js")
    [UniversalDashboard.Services.AssetService]::Instance.RegisterAsset("http://localhost:10002/editor.worker.js")
    [UniversalDashboard.Services.AssetService]::Instance.RegisterFramework('UniversalAutomation', "http://localhost:10002/ua.index.bundle.js")
} else {
    $IndexJs = Get-ChildItem "$PSScriptRoot\index.*.bundle.js"
    $JsFiles = Get-ChildItem "$PSScriptRoot\*.js"
    $Maps = Get-ChildItem "$PSScriptRoot\*.map"
    
    $AssetId = [UniversalDashboard.Services.AssetService]::Instance.RegisterAsset($IndexJs.FullName)
    [UniversalDashboard.Services.AssetService]::Instance.RegisterFramework('UniversalAutomation', $IndexJs.FullName)

    foreach($item in $JsFiles)
    {
        [UniversalDashboard.Services.AssetService]::Instance.RegisterAsset($item.FullName) | Out-Null
    }
    
    foreach($item in $Maps)
    {
        [UniversalDashboard.Services.AssetService]::Instance.RegisterAsset($item.FullName) | Out-Null
    }
}

function Start-UADashboard {
    param(
        [string]$ComputerName,
        [int]$Port,
        [UniversalDashboard.Models.LoginPage]$LoginPage,
        [Switch]$Wait
    )

    $GetEndpoint = New-UDEndpoint -Url 'universal-automation/.*' -Method Get -EvaluateUrlAsRegex -Endpoint {
        $UAToken = $Request.Headers["UAToken"]
        $Headers = @{}

        if ($UAToken) 
        {
            $Headers["Authorization"] = "Bearer $UAToken" 
        }

        $Url = $Request.Path.ToString().Replace('/api/universal-automation', '')

        try 
        {
            (Invoke-WebRequest "$($ComputerName)$Url" -Headers $Headers -UseBasicParsing).Content 
        }
        catch {
            Write-UDLog $_.Exception.Message
            [Microsoft.AspNetCore.Mvc.StatusCodeResult]::new(401)
        }
    } -Public

    $PostEndpoint = New-UDEndpoint -Url 'universal-automation/.*' -Method Post -EvaluateUrlAsRegex -Endpoint {

        $UAToken = $Request.Headers["UAToken"]
        $Headers = @{}

        if ($UAToken) 
        {
            $Headers["Authorization"] = "Bearer $UAToken" 
        }

        $Url = $Request.Path.ToString().Replace('/api/universal-automation', '')
        (Invoke-WebRequest "$($ComputerName)$Url" -Method Post -Body $Body -ContentType $Request.ContentType -Headers $Headers -UseBasicParsing).Content 
    } -Public

    $PutEndpoint = New-UDEndpoint -Url 'universal-automation/.*' -Method Put -EvaluateUrlAsRegex -Endpoint {

        $UAToken = $Request.Headers["UAToken"]
        $Headers = @{}

        if ($UAToken) 
        {
            $Headers["Authorization"] = "Bearer $UAToken" 
        }

        $Url = $Request.Path.ToString().Replace('/api/universal-automation', '')
        (Invoke-WebRequest "$($ComputerName)$Url" -Method Put -Body $Body -ContentType $Request.ContentType -Headers $Headers -UseBasicParsing).Content 
    } -Public

    $DeleteEndpoint = New-UDEndpoint -Url 'universal-automation/.*' -Method Delete -EvaluateUrlAsRegex -Endpoint {

        $UAToken = $Request.Headers["UAToken"]
        $Headers = @{}

        if ($UAToken) 
        {
            $Headers["Authorization"] = "Bearer $UAToken" 
        }

        $Url = $Request.Path.ToString().Replace('/api/universal-automation', '')
        (Invoke-WebRequest "$($ComputerName)$Url" -Method Delete -Headers $Headers -UseBasicParsing).Content
    } -Public

    $SessionEndpoint = New-UDEndpoint -Url 'sessionAppToken' -Method Get -Endpoint {
        $Session:AppToken
    } -Public

    $RoleEndpoint = New-UDEndpoint -Url 'sessionUserRole' -Method Get -Endpoint {
        $Session:UserRole
    } -Public

    $UserEndpoint = New-UDEndpoint -Url 'user' -Method Get -Endpoint {
        $User
    } -Public

    $Dashboard = New-UDDashboard -Content {} -LoginPage $LoginPage

    $Dashboard.FrameworkAssetId = [UniversalDashboard.Services.AssetService]::Instance.Frameworks['UniversalAutomation'];

    Start-UDDashboard -Port $Port -Endpoint @($GetEndpoint, $PostEndpoint, $PutEndpoint, $DeleteEndpoint, $SessionEndpoint, $RoleEndpoint, $UserEndpoint) -Dashboard $Dashboard -Wait:$Wait
}