diff --git a/README.md b/README.md index 8a51938a..04abc09b 100644 --- a/README.md +++ b/README.md @@ -103,8 +103,16 @@ handling logic. if envPort := os.Getenv("PORT"); envPort != "" { port = envPort } - if err := funcframework.Start(port); err != nil { - log.Fatalf("funcframework.Start: %v\n", err) + + // By default, listen on all interfaces. If testing locally, run with + // LOCAL_ONLY=true to avoid triggering firewall warnings and + // exposing the server outside of your own machine. + hostname := "" + if localOnly := os.Getenv("LOCAL_ONLY"); localOnly == "true" { + hostname = "127.0.0.1" + } + if err := funcframework.StartHostPort(hostname, port); err != nil { + log.Fatalf("funcframework.StartHostPort: %v\n", err) } } ``` @@ -113,8 +121,7 @@ handling logic. 1. Start the local development server: ```sh - export FUNCTION_TARGET=HelloWorld - go run cmd/main.go + FUNCTION_TARGET=HelloWorld LOCAL_ONLY=true go run cmd/main.go # Output: Serving function: HelloWorld ``` diff --git a/funcframework/framework.go b/funcframework/framework.go index 2aeb6b1d..335daf76 100644 --- a/funcframework/framework.go +++ b/funcframework/framework.go @@ -101,11 +101,16 @@ func RegisterCloudEventFunctionContext(ctx context.Context, path string, fn func // Start serves an HTTP server with registered function(s). func Start(port string) error { + return StartHostPort("", port) +} + +// StartHostPort serves an HTTP server with registered function(s) on the given host and port. +func StartHostPort(hostname, port string) error { server, err := initServer() if err != nil { return err } - return http.ListenAndServe(":"+port, server) + return http.ListenAndServe(fmt.Sprintf("%s:%s", hostname, port), server) } func initServer() (*http.ServeMux, error) {