Servizio Hello World
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Crea una semplice applicazione Hello World, pacchettizzala in un'immagine container, carica l'immagine container in Artifact Registry, quindi esegui il deployment dell'immagine container in Cloud Run.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],[],[],[],null,["# Hello World service\n\nCreate a simple Hello World application, package it into a container image, upload the container image to Artifact Registry, and then deploy the container image to Cloud Run.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Quickstart: Build and deploy a .NET web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-dotnet-service)\n- [Quickstart: Build and deploy a C++ web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-c-plus-plus-service)\n- [Quickstart: Build and deploy a Go web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-go-service)\n- [Quickstart: Build and deploy a Java Spring Boot web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-java-service)\n- [Quickstart: Build and deploy a Node.js web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-nodejs-service)\n- [Quickstart: Build and deploy a PHP web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-php-service)\n- [Quickstart: Build and deploy a Python (Flask) web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-python-service)\n- [Quickstart: Build and deploy a Ruby web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-ruby-service)\n\nCode sample\n-----------\n\n### C++\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n #include \u003cgoogle/cloud/functions/framework.h\u003e\n #include \u003ccstdlib\u003e\n\n namespace gcf = ::google::cloud::functions;\n\n auto hello_world_http() {\n return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {\n std::string greeting = \"Hello \";\n auto const* target = std::getenv(\"TARGET\");\n greeting += target == nullptr ? \"World\" : target;\n greeting += \"\\n\";\n\n return gcf::HttpResponse{}\n .set_header(\"Content-Type\", \"text/plain\")\n .set_payload(greeting);\n });\n }\n\n int main(int argc, char* argv[]) {\n return gcf::Run(argc, argv, hello_world_http());\n }\n\n### C#\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n var builder = WebApplication.CreateBuilder(args);\n\n var port = Environment.GetEnvironmentVariable(\"PORT\") ?? \"8080\";\n var url = $\"http://0.0.0.0:{port}\";\n var target = Environment.GetEnvironmentVariable(\"TARGET\") ?? \"World\";\n\n var app = builder.Build();\n\n app.MapGet(\"/\", () =\u003e $\"Hello {target}!\");\n\n app.Run(url);\n\n### Go\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Sample run-helloworld is a minimal Cloud Run service.\n package main\n\n import (\n \t\"fmt\"\n \t\"log\"\n \t\"net/http\"\n \t\"os\"\n )\n\n func main() {\n \tlog.Print(\"starting server...\")\n \thttp.HandleFunc(\"/\", handler)\n\n \t// Determine port for HTTP service.\n \tport := os.Getenv(\"PORT\")\n \tif port == \"\" {\n \t\tport = \"8080\"\n \t\tlog.Printf(\"defaulting to port %s\", port)\n \t}\n\n \t// Start HTTP server.\n \tlog.Printf(\"listening on port %s\", port)\n \tif err := http.ListenAndServe(\":\"+port, nil); err != nil {\n \t\tlog.Fatal(err)\n \t}\n }\n\n func handler(w http.ResponseWriter, r *http.Request) {\n \tname := os.Getenv(\"NAME\")\n \tif name == \"\" {\n \t\tname = \"World\"\n \t}\n \tfmt.Fprintf(w, \"Hello %s!\\n\", name)\n }\n\n### Java\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n package com.example.helloworld;\n\n import org.springframework.beans.factory.annotation.Value;\n import org.springframework.boot.SpringApplication;\n import org.springframework.boot.autoconfigure.SpringBootApplication;\n import org.springframework.web.bind.annotation.GetMapping;\n import org.springframework.web.bind.annotation.RestController;\n\n @SpringBootApplication\n public class HelloworldApplication {\n\n @Value(\"${NAME:World}\")\n String name;\n\n @RestController\n class HelloworldController {\n @GetMapping(\"/\")\n String hello() {\n return \"Hello \" + name + \"!\";\n }\n }\n\n public static void main(String[] args) {\n SpringApplication.run(HelloworldApplication.class, args);\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import express from 'express';\n const app = express();\n\n app.get('/', (req, res) =\u003e {\n const name = process.env.NAME || 'World';\n res.send(`Hello ${name}!`);\n });\n\n const port = parseInt(process.env.PORT) || 8080;\n app.listen(port, () =\u003e {\n console.log(`helloworld: listening on port ${port}`);\n });\n\n### PHP\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n \u003c?php\n\n $name = getenv('NAME', true) ?: 'World';\n echo sprintf('Hello %s!', $name);\n\n### Python\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import os\n\n from flask import Flask\n\n app = Flask(__name__)\n\n\n @app.route(\"/\")\n def hello_world():\n \"\"\"Example Hello World route.\"\"\"\n name = os.environ.get(\"NAME\", \"World\")\n return f\"Hello {name}!\"\n\n\n if __name__ == \"__main__\":\n app.run(debug=True, host=\"0.0.0.0\", port=int(os.environ.get(\"PORT\", 8080)))\n\n### Ruby\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"sinatra\"\n\n set :bind, \"0.0.0.0\"\n port = ENV[\"PORT\"] || \"8080\"\n set :port, port\n\n get \"/\" do\n name = ENV[\"NAME\"] || \"World\"\n \"Hello #{name}!\"\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=cloudrun)."]]