Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Servers/Pode.SMTP.Server.ps1
Original file line number Diff line number Diff line change
@@ -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 <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
}
}
56 changes: 56 additions & 0 deletions Servers/Pode.TCP.Server.ps1
Original file line number Diff line number Diff line change
@@ -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 <port>
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
}
}
46 changes: 46 additions & 0 deletions Servers/Pode.Web.Server.ps1
Original file line number Diff line number Diff line change
@@ -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:<port>/ping.
2. You can test the /processes page by navigating to http://localhost:<port>/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
}
}
Loading