Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add powershell-http template #1

Merged
merged 5 commits into from Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
@@ -1,2 +1,9 @@
# powershell-http-template
OpenFaaS HTTP template for PowerShell

## Using this template
```
$ faas template pull https://github.com/openfaas-incubator/powershell-http-template
$ faas new --lang powershell-http <fn-name>
```

22 changes: 22 additions & 0 deletions template/powershell-http/Dockerfile
@@ -0,0 +1,22 @@
FROM microsoft/powershell:ubuntu-xenial

RUN apt-get update \
&& apt-get install -y curl \
&& curl -sSLf https://github.com/openfaas-incubator/of-watchdog/releases/download/0.4.5/of-watchdog > /usr/bin/fwatchdog \
&& chmod +x /usr/bin/fwatchdog \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

COPY server.ps1 server.ps1

COPY function function

HEALTHCHECK --interval=1s CMD [ -e /tmp/.lock ] || exit 1

ENV fprocess="pwsh ./server.ps1"
ENV cgi_headers="true"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:8081"

EXPOSE 8080
CMD ["fwatchdog"]
13 changes: 13 additions & 0 deletions template/powershell-http/function/handler.ps1
@@ -0,0 +1,13 @@
function Handler {
Param(
[Parameter(Mandatory=$true)]
[FunctionContext]$fnContext,
[Parameter(Mandatory=$true)]
[FunctionResponse]$fnResponse
)

$output = "Hello! Your input was: " + $fnContext.Body

$fnResponse.Body = $output

}
60 changes: 60 additions & 0 deletions template/powershell-http/server.ps1
@@ -0,0 +1,60 @@
Add-Type -AssemblyName System.Web

Class FunctionContext{
[string] $Method;
[string] $Request;
[string] $Body;
[System.Net.WebHeaderCollection] $Headers;
[System.Collections.Specialized.NameValueCollection] $Query;
[string] $Path;
}

Class FunctionResponse{
[string] $Body;
[System.Net.WebHeaderCollection] $Headers;
[int] $Status;
}

$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://*:8081/")
$listener.Start()

. "./function/handler.ps1"

do {
$context = $listener.GetContext()
$response = $context.Response
$Content = ""

$reqStream = $context.Request.InputStream
$reqEncoding = $context.Request.ContentEncoding

$reader = [System.IO.StreamReader]::new($reqStream, $reqEncoding)

$fnContext = [FunctionContext]::new()
$fnContext.Body = $reader.ReadToEnd()
$fnContext.Headers = $context.Request.Headers
$fnContext.Method = $context.Request.HttpMethod
$fnContext.Query = $context.Request.QueryString
$fnContext.Path = $context.Request.Url.LocalPath
$fnContext.Request = $context.Request

$fnResponse = [FunctionResponse]::new()
$fnResponse.Headers = [System.Net.WebHeaderCollection]::new()
try {
Handler -fnContext $fnContext -fnResponse $fnResponse

$Content = [System.Text.Encoding]::UTF8.GetBytes($fnResponse.Body)
$response.StatusCode = $(If ($fnResponse.Status) {$fnResponse.Status} Else {200}) #default to 200 response if not set
}
catch {
$responseBody = $(If ($_.Exception.Message) {$_.Exception.Message} Else {$fnResponse.Body})
$Content = [System.Text.Encoding]::UTF8.GetBytes($responseBody)
$response.StatusCode = $(If ($fnResponse.Status) {$fnResponse.Status} Else {500}) #default to 500 response for exceptions
}

$response.Headers = $fnResponse.Headers
$response.ContentLength64 = $Content.Length
$response.OutputStream.Write($Content, 0, $Content.Length)
$response.Close()
} while ($listener.IsListening)
2 changes: 2 additions & 0 deletions template/powershell-http/template.yml
@@ -0,0 +1,2 @@
language: powershell-http
fprocess: pwsh server.ps1