Scrivere funzioni HTTP
Nelle funzioni Cloud Run, utilizzi le funzioni HTTP quando vuoi richiamare una funzione tramite una richiesta HTTP(S). Per consentire la semantica HTTP, le firme delle funzioni HTTP accettano argomenti specifici di HTTP.
Implementazione
L'esempio seguente mostra un file di origine della funzione HTTP di base per ogni runtime. Consulta Struttura della directory di origine per informazioni su dove trovare il codice sorgente.
Node.js
const functions = require('@google-cloud/functions-framework');
// Register an HTTP function with the Functions Framework
functions.http('myHttpFunction', (req, res) => {
// Your code here
// Send an HTTP response
res.send('OK');
});
In Node.js, registri una funzione di gestione HTTP con il Framework di Functions per Node.js. La funzione del gestore HTTP deve essere una funzione middleware Express che accetta gli argomenti richiesta e risposta e invia una risposta HTTP.
Cloud Run Functions analizza automaticamente il corpo della richiesta in base all'intestazione Content-Type
della richiesta utilizzando body-parser
, in modo da poter accedere agli oggetti req.body
e req.rawBody
nel gestore HTTP.
Il
punto di ingresso della funzione
è il nome con cui il gestore viene registrato con Functions Framework.
In questo esempio, il punto di ingresso è myHttpFunction
.
Python
import functions_framework
# Register an HTTP function with the Functions Framework
@functions_framework.http
def my_http_function(request):
# Your code here
# Return an HTTP response
return 'OK'
In Python, registri una funzione di gestione HTTP con il Framework di Functions per Python. La funzione del gestore HTTP deve accettare un oggetto richiesta Flask come argomento e restituire un valore che Flask può convertire in un oggetto risposta HTTP.
L'entry point della funzione è il nome della funzione di gestione registrata con Functions Framework.
In questo esempio, il punto di ingresso è my_http_function
.
Vai
package myhttpfunction
import (
"fmt"
"net/http"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)
func init() {
// Register an HTTP function with the Functions Framework
functions.HTTP("MyHTTPFunction", myHTTPFunction)
}
// Function myHTTPFunction is an HTTP handler
func myHTTPFunction(w http.ResponseWriter, r *http.Request) {
// Your code here
// Send an HTTP response
fmt.Fprintln(w, "OK")
}
In Go, registri una funzione di gestione HTTP con il Framework di Functions per Go nella funzione init()
. La funzione del gestore HTTP deve utilizzare l'interfaccia standard
http.HandlerFunc
per inviare una risposta HTTP.
Il
punto di ingresso della funzione
è il nome con cui il gestore viene registrato con Functions Framework.
In questo esempio, il punto di ingresso è MyHTTPFunction
.
La funzione del gestore HTTP deve implementare l'interfaccia standard
http.HandlerFunc
. Accetta un'interfaccia http.ResponseWriter che la funzione utilizza per creare una risposta alla richiesta e un puntatore a una struct http.Request contenente i dettagli della richiesta HTTP in entrata.
Java
package myhttpfunction;
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
// Define a class that implements the HttpFunction interface
public class MyHttpFunction implements HttpFunction {
// Implement the service() method to handle HTTP requests
@Override
public void service(HttpRequest request, HttpResponse response) throws Exception {
// Your code here
// Send an HTTP response
response.getWriter().write("OK");
}
}
In Java, utilizzi l'API Functions Framework Java per implementare una classe di gestione HTTP con l'interfaccia HttpFunction
. Il metodo service()
deve inviare una risposta HTTP.
Il
punto di ingresso della funzione
è il nome completo della classe del gestore HTTP, incluso il nome del pacchetto. In questo esempio, il punto di ingresso è myhttpfunction.MyHttpFunction
.
Il metodo service
riceve un oggetto HttpRequest
che descrive la richiesta HTTP in entrata e un oggetto HttpResponse
che la funzione compila con un messaggio di risposta.
C#
using Google.Cloud.Functions.Framework;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
namespace MyProject
{
// Define a class that implements the IHttpFunction interface
public class MyHttpFunction : IHttpFunction
{
// Implement the HandleAsync() method to handle HTTP requests
public async Task HandleAsync(HttpContext context)
{
// Your code here
// Send an HTTP response
await context.Response.WriteAsync("OK");
}
}
}
Nei runtime .NET, utilizzi il
Framework di Functions per .NET
per implementare una classe di gestore HTTP con l'interfaccia
IHttpFunction
. Il metodo HandleAsync()
accetta un oggetto HttpContext
ASP.NET standard come argomento e deve inviare una risposta HTTP.
Il
punto di ingresso della funzione
è il nome completo della classe del gestore HTTP, incluso lo spazio dei nomi.
In questo esempio, il punto di ingresso è MyProject.MyHttpFunction
.
Ruby
require "functions_framework"
# Register an HTTP function with the Functions Framework
FunctionsFramework.http "my_http_function" do |request|
# Your code here
# Return an HTTP response
"OK"
end
In Ruby, registri una funzione di gestione HTTP con il Framework di Functions per Ruby. La funzione del gestore HTTP deve accettare un oggetto richiesta Rack come argomento e restituire un valore che può essere utilizzato come risposta HTTP.
Il
punto di ingresso della funzione
è il nome con cui il gestore viene registrato con Functions Framework.
In questo esempio, il punto di ingresso è my_http_function
.
PHP
<?php
use Google\CloudFunctions\FunctionsFramework;
use Psr\Http\Message\ServerRequestInterface;
// Register an HTTP function with the Functions Framework
FunctionsFramework::http('myHttpFunction', 'myHttpHandler');
// Define your HTTP handler
function myHttpHandler(ServerRequestInterface $request): string
{
// Your code here
// Return an HTTP response
return 'OK';
}
In PHP, registri una funzione di gestione HTTP con il Framework di Functions per PHP.
La funzione del gestore HTTP deve accettare un argomento che implementa l'interfaccia PSR-7
ServerRequestInterface
e deve restituire una risposta HTTP come stringa o oggetto che implementa l'interfaccia PSR-7
ResponseInterface
.
Il
punto di ingresso della funzione
è il nome con cui il gestore viene registrato con Functions Framework.
In questo esempio, il punto di ingresso è myHttpFunction
.
Richieste e risposte HTTP
Le funzioni HTTP accettano i metodi di richiesta HTTP elencati nella pagina Trigger HTTP. Il gestore HTTP può esaminare il metodo della richiesta ed eseguire azioni diverse in base al metodo.
La funzione deve inviare una risposta HTTP. Se la funzione crea attività in background (ad esempio con thread, future, oggetti JavaScript Promise
, callback o processi di sistema), devi terminare o risolvere queste attività prima di inviare una risposta HTTP. Le attività non terminate prima dell'invio della risposta HTTP potrebbero non essere completate e potrebbero causare un comportamento indefinito.
Per ulteriori informazioni sulle funzioni HTTP e sulle opzioni associate, consulta la sezione Trigger HTTP.
Gestione di CORS
La condivisione delle risorse tra origini (CORS) è un modo per consentire alle applicazioni in esecuzione su un dominio di accedere alle risorse di un altro dominio. Ad esempio, potrebbe essere necessario consentire al tuo dominio di effettuare richieste al dominio delle funzioni Cloud Run per accedere alla tua funzione.
Se CORS non è configurato correttamente, potresti visualizzare errori come i seguenti:
XMLHttpRequest cannot load https://YOUR_FUNCTION_URL. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://YOUR_DOMAIN' is therefore not allowed access.
Per consentire le richieste multiorigine alla tua funzione, imposta l'intestazione
Access-Control-Allow-Origin
in modo appropriato nella risposta HTTP. Per le richieste tra origini preflight, devi rispondere alla richiesta preflight OPTIONS
con un codice di risposta 204
e intestazioni aggiuntive.
Node.js
Python
Go
Java
C#
using Google.Cloud.Functions.Framework; using Microsoft.AspNetCore.Http; using System.Net; using System.Threading.Tasks; namespace Cors; // For more information about CORS and CORS preflight requests, see // https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request. public class Function : IHttpFunction { public async Task HandleAsync(HttpContext context) { HttpRequest request = context.Request; HttpResponse response = context.Response; // Set CORS headers // Allows GETs from any origin with the Content-Type // header and caches preflight response for 3600s response.Headers.Append("Access-Control-Allow-Origin", "*"); if (HttpMethods.IsOptions(request.Method)) { response.Headers.Append("Access-Control-Allow-Methods", "GET"); response.Headers.Append("Access-Control-Allow-Headers", "Content-Type"); response.Headers.Append("Access-Control-Max-Age", "3600"); response.StatusCode = (int) HttpStatusCode.NoContent; return; } await response.WriteAsync("CORS headers set successfully!", context.RequestAborted); } }
Ruby
PHP
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use GuzzleHttp\Psr7\Response; function corsEnabledFunction(ServerRequestInterface $request): ResponseInterface { // Set CORS headers for preflight requests // Allows GETs from any origin with the Content-Type header // and caches preflight response for 3600s $headers = ['Access-Control-Allow-Origin' => '*']; if ($request->getMethod() === 'OPTIONS') { // Send response to OPTIONS requests $headers = array_merge($headers, [ 'Access-Control-Allow-Methods' => 'GET', 'Access-Control-Allow-Headers' => 'Content-Type', 'Access-Control-Max-Age' => '3600' ]); return new Response(204, $headers, ''); } else { return new Response(200, $headers, 'Hello World!'); } }
Limitazioni di CORS
Per le richieste multiorigine preflight, vengono inviate richieste preflight OPTIONS
senza un'intestazione Authorization
, pertanto verranno rifiutate in tutte le funzioni HTTP
che richiedono l'autenticazione. Poiché le richieste preflight non vanno a buon fine,
anche le richieste principali non andranno a buon fine. Per ovviare a questo limite, utilizza una delle seguenti opzioni:
- Consenti chiamate non autenticate della tua funzione.
- Ospita la tua app web e le funzioni Cloud Run sullo stesso dominio per evitare CORS. Puoi farlo integrando Firebase Hosting con Cloud Run Functions.
Passaggi successivi
- Scopri di più sui trigger HTTP.
- Scopri come eseguire il deployment di una funzione Cloud Run.