Skip to content

Commit 76f77e5

Browse files
burtonralexellis
authored andcommitted
Add powershell-http template
This is an initial powershell-http template Create an HTTP server in powershell and call the function/handler.ps1 script with the incoming request body Errors are returned with a 500 status code and the exception message Signed-off-by: Burton Rheutan <[email protected]>
1 parent 4c898b5 commit 76f77e5

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

template/powershell-http/Dockerfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM microsoft/powershell:ubuntu16.04
2+
3+
RUN curl -sSLf https://github.com/openfaas-incubator/of-watchdog/releases/download/0.4.2/of-watchdog > /usr/bin/fwatchdog \
4+
&& chmod +x /usr/bin/fwatchdog
5+
WORKDIR /root/
6+
7+
COPY server.ps1 server.ps1
8+
9+
COPY function function
10+
11+
HEALTHCHECK --interval=1s CMD [ -e /tmp/.lock ] || exit 1
12+
13+
ENV fprocess="pwsh ./server.ps1"
14+
ENV cgi_headers="true"
15+
ENV mode="http"
16+
ENV upstream_url="http://127.0.0.1:8081"
17+
18+
EXPOSE 8080
19+
CMD ["fwatchdog"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Rename input for better readability
2+
$funcInput = $args
3+
$output = "Hello! Your input was: " + $funcInput
4+
5+
# Must provide an output variable for the main process to return
6+
$output

template/powershell-http/server.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Add-Type -AssemblyName System.Web
2+
3+
$listener = New-Object System.Net.HttpListener
4+
$listener.Prefixes.Add("http://*:8081/")
5+
$listener.Start()
6+
7+
do {
8+
$context = $listener.GetContext()
9+
$response = $context.Response
10+
$Content = ""
11+
12+
$reqStream = $context.Request.InputStream
13+
$reqEncoding = $context.Request.ContentEncoding
14+
15+
$reader = [System.IO.StreamReader]::new($reqStream, $reqEncoding)
16+
17+
$reqBody = $reader.ReadToEnd()
18+
19+
try {
20+
$funcResult = . .\function\handler.ps1 $reqBody
21+
22+
$Content = [System.Text.Encoding]::UTF8.GetBytes($funcResult)
23+
$response.StatusCode = 200
24+
}
25+
catch {
26+
$Content = [System.Text.Encoding]::UTF8.GetBytes("$_.Exception.Message")
27+
$response.StatusCode = 500
28+
}
29+
30+
$response.ContentLength64 = $Content.Length
31+
$response.OutputStream.Write($Content, 0, $Content.Length)
32+
$response.Close()
33+
} while ($listener.IsListening)

template/powershell-http/template.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language: powershell-http
2+
fprocess: pwsh server.ps1

0 commit comments

Comments
 (0)