From fb71f9d545035155e644d06de04a0787126e1d58 Mon Sep 17 00:00:00 2001 From: Matthew Kelly Date: Mon, 25 May 2026 14:21:48 +0100 Subject: [PATCH] #24: Adds simple Pode server examples for Web/API, SMTP, and TCP --- Servers/Pode.SMTP.Server.ps1 | 42 +++++++++++++++++++++++++++ Servers/Pode.TCP.Server.ps1 | 56 ++++++++++++++++++++++++++++++++++++ Servers/Pode.Web.Server.ps1 | 46 +++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 Servers/Pode.SMTP.Server.ps1 create mode 100644 Servers/Pode.TCP.Server.ps1 create mode 100644 Servers/Pode.Web.Server.ps1 diff --git a/Servers/Pode.SMTP.Server.ps1 b/Servers/Pode.SMTP.Server.ps1 new file mode 100644 index 0000000..9dfa859 --- /dev/null +++ b/Servers/Pode.SMTP.Server.ps1 @@ -0,0 +1,42 @@ +#Requires -Modules @{ ModuleName = 'Pode'; ModuleVersion = '2.13.3' } + +<# +.SYNOPSIS + A simple SMTP server built with Pode. + +.DESCRIPTION + A simple SMTP server built with Pode. It has one Handler that listens for incoming emails and writes + the details to the console. + +.PARAMETER Port + The port to listen on. (Default: Random between 4200 and 42000) + +.LINK + https://badgerati.github.io/Pode/Servers/SMTP/ + +.EXAMPLE + .\Pode.SMTP.Server.ps1 -Port 8025 + +.NOTES + 1. You can test the SMTP server by sending an email to localhost on the specified port. + Send-MailMessage -From 'sender@example.com' -To 'recipient@example.com' -Subject 'Test' -Body 'This is a test email.' -SmtpServer 'localhost' -Port +#> +param( + [Parameter()] + [int] + $Port = (Get-Random -Minimum 4200 -Maximum 42000) +) + +# Starts the Pode server, running on 2 threads (ie: runspaces) +Start-PodeServer -Threads 2 -ScriptBlock { + # Add an SMTP endpoint, listening on localhost and the specified port + Add-PodeEndpoint -Address 'localhost' -Port $Port -Protocol Smtp + + # Add a handler for incoming SMTP events that writes the email details to the console. + Add-PodeHandler -Type Smtp -Name 'SmtpHandler' -ScriptBlock { + "From: $($SmtpEvent.Email.From)" | Out-PodeHost + "To: $($SmtpEvent.Email.To)" | Out-PodeHost + "Subject: $($SmtpEvent.Email.Subject)" | Out-PodeHost + "Body: $($SmtpEvent.Email.Body)" | Out-PodeHost + } +} \ No newline at end of file diff --git a/Servers/Pode.TCP.Server.ps1 b/Servers/Pode.TCP.Server.ps1 new file mode 100644 index 0000000..43dfc0d --- /dev/null +++ b/Servers/Pode.TCP.Server.ps1 @@ -0,0 +1,56 @@ +#Requires -Modules @{ ModuleName = 'Pode'; ModuleVersion = '2.13.3' } + +<# +.SYNOPSIS + A simple TCP server built with Pode. + +.DESCRIPTION + A simple TCP server built with Pode. It has three Verbs that listen for incoming messages and respond accordingly. + +.PARAMETER Port + The port to listen on. (Default: Random between 4200 and 42000) + +.LINK + https://badgerati.github.io/Pode/Servers/TCP/ + +.EXAMPLE + .\Pode.TCP.Server.ps1 -Port 9000 + +.NOTES + 1. You can test the TCP server by connecting to localhost on the specified port via telnet. + + $> telnet localhost + S> Welcome! + C> HELLO + S> Hi! + C> DATE + S> 6/10/2024 2:30:00 PM + C> BYE + S> Goodbye! +#> +param( + [Parameter()] + [int] + $Port = (Get-Random -Minimum 4200 -Maximum 42000) +) + +# Starts the Pode server, running on 2 threads (ie: runspaces) +Start-PodeServer -Threads 2 -ScriptBlock { + # Add a TCP endpoint, listening on localhost and the specified port + Add-PodeEndpoint -Address 'localhost' -Port $Port -Protocol Tcp -CRLFMessageEnd -Acknowledge 'Welcome!' + + # Adds a verb for HELLO that responds with "Hi!" + Add-PodeVerb -Verb 'HELLO' -ScriptBlock { + Write-PodeTcpClient -Message 'Hi!' + } + + # Adds a verb for DATE that responds with the current date and time. + Add-PodeVerb -Verb 'DATE' -ScriptBlock { + Write-PodeTcpClient -Message (Get-Date).ToString() + } + + # Adds a verb for BYE that responds with "Goodbye!" and closes the connection. + Add-PodeVerb -Verb 'BYE' -ScriptBlock { + Write-PodeTcpClient -Message 'Goodbye!' -CloseConnection + } +} \ No newline at end of file diff --git a/Servers/Pode.Web.Server.ps1 b/Servers/Pode.Web.Server.ps1 new file mode 100644 index 0000000..5a77791 --- /dev/null +++ b/Servers/Pode.Web.Server.ps1 @@ -0,0 +1,46 @@ +#Requires -Modules @{ ModuleName = 'Pode'; ModuleVersion = '2.13.3' } + +<# +.SYNOPSIS + A simple web server built with Pode. + +.DESCRIPTION + A simple web server built with Pode. It has two Routes: + + 1. A Route for pinging and returning the current date and time. + 2. A Page for listing the current processes on the server. + +.PARAMETER Port + The port to listen on. (Default: Random between 4200 and 42000) + +.LINK + https://badgerati.github.io/Pode/Getting-Started/FirstApp/ + +.EXAMPLE + .\Pode.Web.Server.ps1 -Port 8080 + +.NOTES + 1. You can test the /ping route by navigating to http://localhost:/ping. + 2. You can test the /processes page by navigating to http://localhost:/processes. +#> +param( + [Parameter()] + [int] + $Port = (Get-Random -Minimum 4200 -Maximum 42000) +) + +# Starts the Pode server, running on 2 threads (ie: runspaces) +Start-PodeServer -Threads 2 -ScriptBlock { + # Add an HTTP endpoint, listening on localhost and the specified port + Add-PodeEndpoint -Address 'localhost' -Port $Port -Protocol Http + + # Add a simple GET route for /ping that returns the current date and time as JSON + Add-PodeRoute -Method Get -Path '/ping' -ScriptBlock { + Write-PodeJsonResponse -Value @{ Pong = Get-Date } + } + + # Add a page for /processes that lists the current processes on the server. + Add-PodePage -Name 'processes' -ScriptBlock { + Get-Process | Select-Object Name, Id, CPU, StartTime + } +} \ No newline at end of file