Skip to content

chore: Mention setting function target env variable when starting dev server using ide #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

gregorygaines
Copy link

When starting the functions framework from the Intellij ide, the application throws 404 on each request:

function.go

package function

import (
	"fmt"
	"net/http"

	"github.com/GoogleCloudPlatform/functions-framework-go/functions"
)

func init() {
	functions.HTTP("HelloWorld", helloWorld)
}

// helloWorld writes "Hello, World!" to the HTTP response.
func helloWorld(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello, World!")
}

cmd/main.go

package main

import (
	"log"
	// Blank-import the function package so the init() runs
	"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
	_ "example.com/hello"
)

func main() {
	if err := funcframework.StartHostPort("127.0.0.1", "8082"); err != nil {
		log.Fatalf("funcframework.StartHostPort: %v\n", err)
	}
}

When starting a go app from an IDE, either set the "FUNCTION_TARGET" env variable or set manually in code:

package main

import (
	"log"
	// Blank-import the function package so the init() runs
	"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
	_ "example.com/hello"
)

func main() {
        err := os.Setenv("FUNCTION_TARGET", "HelloWorld")
	if err != nil {
		log.Fatalf("failed to set FUNCTION_TARGET: %v", err)
	}

	if err := funcframework.StartHostPort("127.0.0.1", "8082"); err != nil {
		log.Fatalf("funcframework.StartHostPort: %v\n", err)
	}
}

This PR adds commented code to the quickstart example mentioning that local servers started from IDE's require the function target env variable set to the function name.

@gregorygaines gregorygaines changed the title chore: Mention setting function target env variable when starting dev server chore: Mention setting function target env variable when starting dev server using ide Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant