Skip to content

Local dev server doesn't work when started from Intellij IDE #267

@gregorygaines

Description

@gregorygaines

I work with alot of IDE tools and when I start the functions framework from my 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)
	}
}

I think we can resolve some confusion if we mention this in the quickstart docs, I created a PR #266 to include this comment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Low PrioritydocumentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions