googlearchive / cloud-functions-go

Unofficial Native Go Runtime for Google Cloud Functions
Apache License 2.0
423 stars 44 forks source link

How to use with a web server framework #42

Open alejomendoza opened 6 years ago

alejomendoza commented 6 years ago

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

ssttevee commented 6 years ago

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).

prep commented 6 years ago

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.

iangudger commented 6 years ago

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

prep commented 6 years ago

@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).