cloud-functions-go icon indicating copy to clipboard operation
cloud-functions-go copied to clipboard

How to use with a web server framework

Open alejomendoza opened this issue 5 years ago • 4 comments

Is there a way we can connect this library to a web server like (https://github.com/zenazn/goji)?

alejomendoza avatar Jul 30 '18 21:07 alejomendoza

Yes, you should be able to add a framework layer as long as it can take the http.Handler interface.

Using that specific library, it would look something like this:

func main() {
	flag.Parse()

	goji.Handle(nodego.HTTPTrigger, func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, I'm native Go!")
	})

	DefaultMux.Compile()

	http.Handle("/", DefaultMux)

	nodego.TakeOver()
}

However, I don't see much of a purpose since there is only one url you can handle on GCF (i.e. /execute).

ssttevee avatar Jul 30 '18 22:07 ssttevee

Are you sure about that latter part of your comment, @ssttevee? If I deploy a Google Cloud Function written in Go and call /foo/bar/zoink on the base URL, it gets received as /execute/foo/bar/zoink in my http.Handler.

To make that work with my custom router I just need to strip away /execute from r.URL.Path and r.RequestURI.

prep avatar Aug 04 '18 08:08 prep

@prep, are you asking how to strip the path?

iangudger avatar Aug 05 '18 01:08 iangudger

@iangudger, I was saying that the latter part of @ssttevee's comment seems to suggest that a function is only been given a singular URL path, namely /execute, and therefore an alternative HTTP router doesn't make much sense.

In my experience, you can serve many paths from your function with the only caveat that all URL paths have the /execute prefix to them which I suggested can be stripped away before they hit your router (by way of stripping them in ServeHTTP(), for example).

prep avatar Aug 05 '18 07:08 prep