您正在开发一个Azure Function App。您使用 Azure Function App 主机不支持的语言开发代码。代码语言支持 HTTP 原语。 您必须将代码部署到生产 Azure Function App 环境。 您需要配置应用程序以进行部署。 您应该使用哪些配置值?
请告知是否应该 代码 -> 自定义处理程序 -> 自定义 或者 Docker 容器 -> power shell 核心 -> 7.0
尝试了 Docker 容器 -> power shell core -> 7.0 但没有成功
为了使用自定义处理程序,请参阅此 Github 示例:-
示例 Http Golang 自定义处理程序:-
参考这里。
GoCustomHandler.go:-
package main
import (
"fmt"
"net/http"
"log"
"os"
"strings"
)
func httpTriggerHandler(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Forwarded-For") != "" {
w.Write([]byte(strings.Split(r.Header.Get("X-Forwarded-For"),":")[0]))
} else if r.Header.Get("Host") != "" {
w.Write([]byte(strings.Split(r.Header.Get("Host"),":")[0]))
} else if r.RemoteAddr != "" {
w.Write([]byte(strings.Split(r.RemoteAddr,":")[0]))
}
}
func main() {
httpInvokerPort, exists := os.LookupEnv("FUNCTIONS_HTTPWORKER_PORT")
if exists {
fmt.Println("FUNCTIONS_HTTPWORKER_PORT: " + httpInvokerPort)
}
mux := http.NewServeMux()
mux.HandleFunc("/httptrigger", httpTriggerHandler)
log.Println("Go server Listening...on httpInvokerPort:", httpInvokerPort)
log.Fatal(http.ListenAndServe(":"+httpInvokerPort, mux))
}
使用以下命令构建此代码:-
go build GoCustomHandler.go
将创建GoCustomHandler.exe
,然后将其添加到host.json
中,如下所示:-
{
"version": "2.0",
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[1.*, 2.0.0)"
},
"customHandler": {
"description": {
"defaultExecutablePath": "GoCustomHandler.exe",
"workingDirectory": "",
"arguments": []
},
"enableForwardingHttpRequest": true
}
}
正如您已经尝试使用
Powershell core
,直接创建一个 Http 触发器并针对 Http Primitive 用例进行调整,如下所示:-
run.ps1:-
# Azure Function PowerShell Script for HTTP Trigger
param($req, $TriggerMetadata)
# HTTP trigger function processed a request.
Write-Host "PowerShell HTTP trigger function processed a request."
# Interact with query parameters or the body of the request.
$name = $req.Query.Name
if (-not $name) {
if ($req.Body) {
$reqBody = $req.Body | ConvertFrom-Json
$name = $reqBody.Name
}
}
# Prepare the response body
if ($name) {
$body = "Hello, $name. This HTTP triggered function executed successfully."
} else {
$body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
}
# Create a response object using HTTP primitives
$response = [System.Net.Http.HttpResponseMessage]::new([System.Net.HttpStatusCode]::OK)
$responseContent = [System.Net.Http.StringContent]::new($body)
$response.Content = $responseContent
# Return the response
$response
输出:-
function.json:-
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["get", "post"]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}