
14個文本轉圖像AI API
該應用程序由三個主要組件組成:
該圖顯示了用戶與 Web 應用交互時數據在這些組件之間的流動方式。該應用利用了無服務器計算的優勢,例如可擴展性、可靠性和成本效益。
以下是使用 Terraform 設置開發環境的基本流程概述:
terraform init
在您的終端中運行。這將下載必要的提供程序插件。terraform plan
以檢查將進行哪些更改。然后,運行terraform apply
以創建定義的基礎設施。請記住用實際值替換占位符,并根據您的要求調整代碼。
Azure Functions 是一種無服務器解決方案,可讓您編寫更少的代碼、維護更少的基礎架構并節省成本。以下是如何使用 Terraform 添加帳戶和開發環境來設置我們的基本基礎架構。
# Terraform code for setting up Azure Functions
# Define the provider
provider "azurerm" {
features {}
}
# Create a resource group
resource "azurerm_resource_group" "rg" {
name = "example-resources"
location = "West Europe"
}
# Create a storage account
resource "azurerm_storage_account" "sa" {
name = "examplestorageacc"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}
# Create an app service plan
resource "azurerm_app_service_plan" "asp" {
name = "example-appserviceplan"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku {
tier = "Standard"
size = "S1"
}
}
# Create a function app
resource "azurerm_function_app" "fa" {
name = "example-functionapp"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
app_service_plan_id = azurerm_app_service_plan.asp.id
storage_connection_string = azurerm_storage_account.sa.primary_connection_string
}
讓我們深入研究上述 Terraform 代碼及其創建的資源。
azurerm_resource_group
用于定義資源組。azurerm_storage_account
用于定義存儲帳戶。azurerm_app_service_plan
用于定義應用服務計劃。azurerm_function_app
用于定義函數應用程序。在提供的代碼中,您可以將占位符替換為實際值,以便在 Azure 環境中創建這些資源。例如,您可以將“example-resources”替換為資源組的名稱,將“West Europe”替換為要使用的 Azure 區域,將“examplestorageacc”替換為存儲帳戶的名稱,等等。
要創建基礎設施,我們只需執行:terraform apply
。
輸出應如下所示。
現在我們有了工具,讓我們開始構建無服務器 Web 應用程序。
如果您使用 RapidAPI,則需要執行以下手動流程。找到要在 RapidAPI 上使用的 API 后,您通常會使用注冊 RapidAPI 時獲得的API 密鑰來驗證您對 API 的請求。
下面是一個示例 Python 代碼片段,展示了如何使用 API 密鑰在 RapidAPI 上請求 API:
import requests
url = "https://weatherapi-com.p.rapidapi.com/current.json"
querystring = {"q": "53.1,-0.13"}
headers = {
"X-RapidAPI-Key": "your-api-key",
"X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com"
}
response = requests.get(url, headers=headers, params=querystring)
print(response.json())
在此代碼片段中,將“ api – name . p . rapidapi . com ”替換為您要使用的 API 的主機,“api-endpoint”替換為 API 的端點,“your-api-key”替換為您實際的 API 密鑰。這將向 API 發送 GET 請求并打印響應。
代碼片段的示例輸出。
注意:請確保您訂閱了在 RapidAPI 上使用的 API
這是用于創建 Azure 函數應用和 HTTP 觸發器函數的 Terraform 代碼。這對于創建 Azure 函數應用是必需的。
# Define the provider
provider "azurerm" {
features {}
}
# Create a resource group
resource "azurerm_resource_group" "rg" {
name = "example-resources"
location = "West Europe"
}
# Create a storage account
resource "azurerm_storage_account" "sa" {
name = "examplestorageacc"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}
# Create an app service plan
resource "azurerm_app_service_plan" "asp" {
name = "example-appserviceplan"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku {
tier = "Standard"
size = "S1"
}
}
# Create a function app
resource "azurerm_function_app" "fa" {
name = "example-functionapp"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
app_service_plan_id = azurerm_app_service_plan.asp.id
storage_connection_string = azurerm_storage_account.sa.primary_connection_string
}
以下是代碼中的組件:
這是一個 C# Azure 函數的簡單示例,它從 RapidAPI 調用 OpenWeatherMap API。此函數將由 HTTP 請求觸發,并返回指定城市的當前天氣。
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string city = req.Query["city"];
using (var client = new HttpClient())
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri($"https://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY"),
Headers =
{
{ "x-rapidapi-key", "YOUR_RAPIDAPI_KEY" },
{ "x-rapidapi-host", "community-open-weather-map.p.rapidapi.com" },
},
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(body);
return new OkObjectResult($"Temperature in {city}: {data.main.temp}°C");
}
}
}
在此代碼中:
請記住將“YOUR_API_KEY”替換為您在 OpenWeatherMap 中獲得的實際 API 密鑰,將“YOUR_RAPIDAPI_KEY”替換為您的 RapidAPI 密鑰。另請注意,出于安全原因,您通常不會在代碼中直接公開 API 密鑰。在實際情況下,您會將這些密鑰安全地存儲在 Azure Key Vault 中或使用托管標識。
如果您已經擁有 Azure 函數應用,那就太好了!您可以跳過創建步驟,直接將代碼部署到現有函數應用。以下是使用 Azure CLI 執行此操作的方法:
az login
從 CLI 登錄到您的 Azure 帳戶。func azure functionapp publish <FunctionAppName>
命令將本地項目部署到現有函數應用程序。 替換為現有函數應用程序的名稱。以下是示例腳本:
# Login to Azure
az login
# Navigate to your local project directory
cd path-to-your-local-function-app
# Deploy the function app
func azure functionapp publish your-existing-function-app-name
用您的實際值替換“path-to-your-local-function-app”和“your-existing-function-app-name”。
成功上傳的示例輸出。
部署函數應用后,可以通過向函數的 URL 發出 HTTP 請求來測試它。URL 通常采用以下格式:https://<Your Function App>.azurewebsites.net/api/<Your Function Name>?code=<your access code>&name=<Enter a name here>
。
與傳統的 Web 開發相比,無服務器計算是一種不同的模式。了解其優點和局限性將有助于您充分利用它。例如,無服務器函數是無狀態的,并且具有最大執行時間,這會影響您設計應用程序的方式。以下是一些其他需要考慮的提示:
使用 Azure Functions、Terraform 和 RapidAPI 構建無服務器 Web 應用是一種強大而有效的應用程序開發方式。通過遵循這些提示和最佳實踐,您可以確保您的應用程序安全、可擴展且可維護。
文章來源:How to Build a Serverless Web App with Azure Functions and RapidAPI