From 0de30003f3b0bc6924bda379d402ea2022255c36 Mon Sep 17 00:00:00 2001 From: komayama Date: Wed, 10 Nov 2021 09:00:33 +0900 Subject: [PATCH] add SimpleHttpTrigger for Perl custom handler --- Perl/.funcignore | 4 ++++ Perl/.gitignore | 14 ++++++++++++++ Perl/README.md | 20 ++++++++++++++++++++ Perl/SimpleHttpTrigger/function.json | 19 +++++++++++++++++++ Perl/host.json | 22 ++++++++++++++++++++++ Perl/local.settings-example.json | 8 ++++++++ Perl/src/main.pl | 23 +++++++++++++++++++++++ 7 files changed, 110 insertions(+) create mode 100644 Perl/.funcignore create mode 100644 Perl/.gitignore create mode 100644 Perl/README.md create mode 100644 Perl/SimpleHttpTrigger/function.json create mode 100644 Perl/host.json create mode 100644 Perl/local.settings-example.json create mode 100644 Perl/src/main.pl diff --git a/Perl/.funcignore b/Perl/.funcignore new file mode 100644 index 0000000..414df2f --- /dev/null +++ b/Perl/.funcignore @@ -0,0 +1,4 @@ +.git* +.vscode +local.settings.json +test \ No newline at end of file diff --git a/Perl/.gitignore b/Perl/.gitignore new file mode 100644 index 0000000..2445bb3 --- /dev/null +++ b/Perl/.gitignore @@ -0,0 +1,14 @@ + +# Azure Functions artifacts +bin +obj +appsettings.json +local.settings.json + +# Azurite artifacts +__blobstorage__ +__queuestorage__ +__azurite_db*__.json + +# Strawberry +Strawberry/* \ No newline at end of file diff --git a/Perl/README.md b/Perl/README.md new file mode 100644 index 0000000..1aab531 --- /dev/null +++ b/Perl/README.md @@ -0,0 +1,20 @@ +# Azure Functions custom handler in Perl + +The samples available in this folder demonstrate how to implement a [custom handler](https://docs.microsoft.com/azure/azure-functions/functions-custom-handlers). + +Example functions featured in this repo include: + +| Name | Trigger | Input | Output | +|------|---------|-------|--------| +| [SimpleHttpTrigger](../../../tree/master/Perl/SimpleHttpTrigger) | HTTP | n/a | n/a | + +## Configuration + +The *local.settings-example.json* is provided to show what values the app is expecting to read from environment variables. Make a copy of *local.settings-example.json* and rename it *local.settings.json* and replace any values that begin with "**YOUR_**" with your values. + +## Pre-reqs + +- Perl (Strawberry) : https://strawberryperl.com/ + - Put the Strawberry packages which is included in perl.exe to under `Strawberry` folder after download. + - Windows **ONLY**. +- Azure Functions Core Tools diff --git a/Perl/SimpleHttpTrigger/function.json b/Perl/SimpleHttpTrigger/function.json new file mode 100644 index 0000000..7eb1f8f --- /dev/null +++ b/Perl/SimpleHttpTrigger/function.json @@ -0,0 +1,19 @@ +{ + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get", + "post" + ] + }, + { + "type": "http", + "direction": "out", + "name": "res" + } + ] +} diff --git a/Perl/host.json b/Perl/host.json new file mode 100644 index 0000000..455e3f9 --- /dev/null +++ b/Perl/host.json @@ -0,0 +1,22 @@ +{ + "version": "2.0", + "logging": { + "applicationInsights": { + "samplingSettings": { + "isEnabled": true, + "excludedTypes": "Request" + } + } + }, + "extensionBundle": { + "id": "Microsoft.Azure.Functions.ExtensionBundle", + "version": "[1.*, 2.0.0)" + }, + "customHandler": { + "description": { + "defaultExecutablePath": "Strawberry/perl/bin/perl.exe", + "arguments": ["src/main.pl"] + }, + "enableForwardingHttpRequest": true + } +} diff --git a/Perl/local.settings-example.json b/Perl/local.settings-example.json new file mode 100644 index 0000000..c798d95 --- /dev/null +++ b/Perl/local.settings-example.json @@ -0,0 +1,8 @@ +{ + "IsEncrypted": false, + "Values": { + "AzureWebJobsStorage": "YOUR_STORAGE_CONNECTION_STRING", + "FUNCTIONS_WORKER_RUNTIME": "custom" + } + } + \ No newline at end of file diff --git a/Perl/src/main.pl b/Perl/src/main.pl new file mode 100644 index 0000000..f70766a --- /dev/null +++ b/Perl/src/main.pl @@ -0,0 +1,23 @@ +#!usr/bin/env perl +use HTTP::Daemon; +use HTTP::Status; +use Env; + +my $port = $ENV{"FUNCTIONS_CUSTOMHANDLER_PORT"}; +my $function_name = "SimpleHttpTrigger"; +my $d = HTTP::Daemon->new(LocalAddr => "127.0.0.1", LocalPort => $port ) || die; +print "Please contact me at: url, ">\n"; + +while (my $c = $d->accept) { + while (my $r = $c->get_request) { + if ($r->method eq "GET" and $r->uri->path eq "/api/" . $function_name) { + print "Perl is running on Azure Functions"; + $c->send_response( HTTP::Response->new(200, "OK", undef, "Perl is running on Azure Functions\n") ); + } + else { + $c->send_error(RC_FORBIDDEN) + } + } + $c->close; + undef($c); +} \ No newline at end of file