boson-project / functions

A directory of information and resources for the Boson Project.
Apache License 2.0
6 stars 5 forks source link

How to handle function initialization? #36

Open drriguz opened 6 months ago

drriguz commented 6 months ago

Hi, I'm using Go template in Knative Functions. Since there's only a Handle function to handle income requests, is there any recommendation about application initialization?

For example, we need to load some secret from the environment:

// handle.go
var (
    token string
)

func init {
    token := os.Getenv("SOME_API_TOKEN")
    if(token == "") {
        panic("API TOKEN not configured")
    }
}
func Handle() {
    // result = DoRequest(token)
   //  return result
}

However, it seems that use init to load environment is not a good approach, it's not test friendly anyway, see here.

So the problems is that, how to correctly add additional initialization steps?