Badgerati / Pode

Pode is a Cross-Platform PowerShell web framework for creating REST APIs, Web Sites, and TCP/SMTP servers
https://badgerati.github.io/Pode
MIT License
830 stars 92 forks source link

Hashtable on start #1327

Closed eltyBelgium closed 2 months ago

eltyBelgium commented 3 months ago

Describe the Bug

On call

Start-PodeServer{}

I get the following hashtable in cmd, while all my code is commented.

image

Steps To Reproduce

Steps to reproduce the behavior:

  1. Call file for starting Pode

Expected Behavior

Starting server

Platform

Badgerati commented 3 months ago

Hi @eltyBelgium,

Are you saying you're running a ps1 file with literally just Start-PodeServer {} as its content? The scriptblock has nothing in it?

If so, I've tested myself on Pode 2.10.1 and PS7.4.2 and I only get the Pode v2.10.1 (PID: 37040) line. I've scanned the code, and I can't see anywhere that hashtable would be being output, as it doesn't map to any objects/properties which Pode requires 🤔

eltyBelgium commented 3 months ago

Hi @Badgerati

The full file is, I think he saves the endpoints somewhere and it failso n that point. Just a wild guess here.

Import-Module -Name Pode -MaximumVersion 2.99.99
Import-Module "$env:Psuniverse/Functions/AT/Product/get-productcategories.ps1" -Force
Import-Module "$env:Psuniverse/Functions/VSZ/vsz-getwlans.ps1" -Force
Import-Module "$env:Psuniverse/Functions/VSZ/vsz-getzones.ps1" -Force
Import-Module "$env:Psuniverse/Functions/VSZ/vsz-getdomains.ps1" -Force
Import-Module "$env:Psuniverse/Functions/VSZ/vsz-getdpsks.ps1" -Force
Import-Module "$env:Psuniverse/Functions/VSZ/vsz-createdpsks.ps1" -Force

Start-PodeServer {
    New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging -Levels Error, Warning, Informational, Verbose, Debug 
    Add-PodeOAInfo -Title "Acom External Connector" -TermsOfService 'http://swagger.io/terms/' -LicenseName 'Apache 2.0' -LicenseUrl 'http://www.apache.org/licenses/LICENSE-2.0.html' -ContactName 'API Support' -ContactEmail 'info@acom.be' -ContactUrl 'http://acom.be/support'
    Enable-PodeOpenApi -Path '/docs/openapi'   -NoDefaultResponses
    Enable-PodeOAViewer -Type Swagger -Path '/docs/swagger'
    # attach to port 8080 for http
    Add-PodeEndpoint -Address * -Port 8070 -Protocol Http
    New-PodeAuthScheme -ApiKey | Add-PodeAuth -Name 'Auth' -Sessionless -ScriptBlock {
        param($key)

        # check if the key is valid, and get user
        if ($key -eq (Get-SecretFromVault -Name psu-api-key -AsPlainText 1)) {
            return @{
                User = @{
                    'ID'   = 'AcomID'
                    'Name' = 'API'
                    'Type' = 'BITCH'
                }
            }
        }
        return $null
    }
    Add-PodeAuthMiddleware -Name 'GlobalAuthValidation' -Authentication 'Auth' -Route '/api/*'
    Set-PodeRouteIfExistsPreference -Value Overwrite

    Add-PodeRouteGroup -Path '/api'  -Routes {
        Add-PodeRoute -Method Get -Path '/health' -ScriptBlock {
            Write-PodeJsonResponse -Value @{ 'status' = 'ok'; 'message' = 'API is healthy'; 'time' = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss') }
        }

        Add-PodeRouteGroup -Path '/network' -Routes {
            Add-PodeRoute -Method Get -Path '/domains' -ScriptBlock {
                $result = vsz-getdomains | ConvertTo-Json

                Write-PodeJsonResponse -Value $result
            }

            Add-PodeRoute -Method Get -Path '/zones' -ScriptBlock {
                $result = vsz-getzones | ConvertTo-Json

                Write-PodeJsonResponse -Value $result
            }

            Add-PodeRoute -Method Get -Path '/zones/:zoneId/wlans' -ScriptBlock {
                $result = vsz-getwlans $WebEvent.Parameters['zoneId']| ConvertTo-Json

                $specificWlan = $result | Where-Object { $_.ZoneID -eq $WebEvent.Query['wlandId'] }

                if($null -eq $specificWlan) {
                    Write-PodeJsonResponse -Value $result

                }else{
                    Write-PodeJsonResponse -Value $specificWlan
                }
            }

            Add-PodeRoute -Method Get -Path '/zones/:zoneId/dpsks' -ScriptBlock {
                $result = vsz-getdpsks $WebEvent.Parameters['zoneId']| ConvertTo-Json

                $specificWlan = $result | Where-Object { $_.ZoneID -eq $WebEvent.Query['dpsk'] }

                if($null -eq $specificWlan) {
                    Write-PodeJsonResponse -Value $result

                }else{
                    Write-PodeJsonResponse -Value $specificWlan
                }
            }

            Add-PodeRoute -Method Post -Path '/zones/:zoneId/dpsks' -ScriptBlock {

                try{
                    $result = vsz-createdpsks -zoneID $WebEvent.Parameters['zoneId'] -wlanID $WebEvent.Data.wlandID -username $WebEvent.Data.username -userrole $WebEvent.Data.userrole
                    Write-PodeTextResponse -Value $result -ContentType "application/json"
                }
                catch{
                    Write-PodeErrorLog -Exception $_.Exception
                    Write-PodeJsonResponse -Value @{"message" = $_.Exception.Message} -StatusCode 500
                }

            }

        }
    }

}
ArieHein commented 2 months ago

Based on the UserAgent, its either something poking your API from the outside and you can try to add a route on the root to see whose poking your api but this is not a return from Pode itself. Other option is you have something in your default PowerShell profile. Try to change your endpoint to either 127.0.0.1 or localhost and and not * and see if there is any differernt result. Also just use the simple example in the documentation for a ping pong pode server and route to make sure its not something inside your powershell scripts you load at the start that causes this

eltyBelgium commented 2 months ago

Found the problem, my collegue added a line with a module that we import and everytime we import it, it runs itself .... My bad for calling it to early...