Create a web server that uses an event handler
Stay organized with collections
Save and categorize content based on your preferences.
Sets up and starts a web application on port 8080.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code sample demonstrates how to initialize and run a web application across multiple languages, including C#, Go, Java, Node.js, and Python.\u003c/p\u003e\n"],["\u003cp\u003eThe applications are configured to listen on port 8080 by default, or an alternative port specified by the \u003ccode\u003ePORT\u003c/code\u003e environment variable.\u003c/p\u003e\n"],["\u003cp\u003eTo use these code samples with Eventarc, you must configure Application Default Credentials (ADC) for authentication.\u003c/p\u003e\n"],["\u003cp\u003eDetailed information on debugging routing events and authentication setup for a local development environment can be found in the provided links.\u003c/p\u003e\n"]]],[],null,["# Create a web server that uses an event handler\n\nSets up and starts a web application on port 8080.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Tutorial: Debug routing events to Cloud Run](/eventarc/standard/docs/run/debugging-events-cloud-run)\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Eventarc, 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 public static void Main(string[] args)\n {\n CreateHostBuilder(args).Build().Run();\n }\n public static IHostBuilder CreateHostBuilder(string[] args)\n {\n var port = Environment.GetEnvironmentVariable(\"PORT\") ?? \"8080\";\n var url = $\"http://0.0.0.0:{port}\";\n\n return Host.CreateDefaultBuilder(args)\n .ConfigureWebHostDefaults(webBuilder =\u003e\n {\n webBuilder.UseStartup\u003cStartup\u003e().UseUrls(url);\n });\n }\n\n### Go\n\n\nTo authenticate to Eventarc, 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 func main() {\n \thttp.HandleFunc(\"/\", HelloEventsStorage)\n \t// Determine port for HTTP service.\n \tport := os.Getenv(\"PORT\")\n \tif port == \"\" {\n \t\tport = \"8080\"\n \t}\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### Java\n\n\nTo authenticate to Eventarc, 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 import org.springframework.boot.SpringApplication;\n import org.springframework.boot.autoconfigure.SpringBootApplication;\n\n @SpringBootApplication\n public class Application {\n public static void main(String[] args) {\n SpringApplication.run(Application.class, args);\n }\n }\n\n### Node.js\n\n\nTo authenticate to Eventarc, 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 const app = require('./app.js');\n const PORT = parseInt(process.env.PORT) || 8080;\n\n app.listen(PORT, () =\u003e\n console.log(`nodejs-events-storage listening on port ${PORT}`)\n );\n\n### Python\n\n\nTo authenticate to Eventarc, 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 cloudevents.http import from_http\n\n from flask import Flask, request\n\n app = Flask(__name__)\n if __name__ == \"__main__\":\n app.run(debug=True, host=\"0.0.0.0\", port=int(os.environ.get(\"PORT\", 8080)))\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=eventarc)."]]