1616├── func.yaml
1717├── go.mod
1818├── go.sum
19- ├── handle .go
20- └── handle_test .go
19+ ├── function .go
20+ └── function_test .go
2121```
2222
2323Aside from the ` func.yaml ` file, this looks like the beginning of just about
@@ -69,8 +69,8 @@ You can get the URL for your deployed function with the `info` command.
6969
7070
7171Go functions can be tested locally on your computer. In the project there is
72- a ` handle_test .go` file which contains simple test which can be extended as needed.
73- Yo can run this test locally as you would do with any Go project.
72+ a ` function_test .go` file which contains a simple test which can be extended as needed.
73+ You can run this test locally as you would do with any Go project.
7474
7575```
7676❯ go test
@@ -79,9 +79,10 @@ Yo can run this test locally as you would do with any Go project.
7979## Function reference
8080
8181Boson Go functions have very few restrictions. You can add any required dependencies
82- in ` go.mod ` and you may include additional local Go files. The only real requirement are
83- that your project is defined in a ` function ` module and exports the function ` Handle() `
84- (supported contracts of this function will be discussed more deeply later).
82+ in ` go.mod ` and you may include additional local Go files. The only real requirement is
83+ that your project is defined in a ` function ` module and exports a ` New() ` constructor
84+ that returns a struct with a ` Handle() ` method (supported contracts of this method
85+ will be discussed more deeply later).
8586In this section, we will look in a little more detail at how Boson functions are invoked,
8687and what APIs are available to you as a developer.
8788
@@ -94,14 +95,18 @@ They each will listen and respond to incoming HTTP events.
9495
9596#### Function triggered by HTTP request
9697
97- When an incoming request is received, your function will be invoked with two parameters:
98+ When an incoming request is received, your function's ` Handle ` method will be invoked with two parameters:
9899Golang's [ http.ResponseWriter] ( https://golang.org/pkg/net/http/#ResponseWriter ) and [ http.Request] ( https://golang.org/pkg/net/http/#Request ) .
99100
100101Then you can use standard Golang techniques to access the request (eg. read the body)
101102and set a proper HTTP response of your function, as you can see on the following example:
102103
103104``` go
104- func Handle (res http .ResponseWriter , req *http .Request ) {
105+ type Function struct {}
106+
107+ func New () *Function { return &Function{} }
108+
109+ func (f *Function ) Handle (res http .ResponseWriter , req *http .Request ) {
105110
106111 // Read body
107112 body , err := ioutil.ReadAll (req.Body )
@@ -121,22 +126,22 @@ func Handle(res http.ResponseWriter, req *http.Request) {
121126If the incoming request is a ` CloudEvent ` , the event is provided via
122127[ CloudEvents Golang SDK] ( https://cloudevents.github.io/sdk-go/ ) and its ` Event ` type
123128as a parameter. There's possibility to leverage Golang's
124- [ Context] ( https://golang.org/pkg/context/ ) as the optional parameter in the function contract,
125- as you can see in the list of supported function signatures:
129+ [ Context] ( https://golang.org/pkg/context/ ) as the optional parameter in the method contract,
130+ as you can see in the list of supported ` Handle ` method signatures:
126131
127132``` go
128- Handle ()
129- Handle () error
130- Handle (context.Context )
131- Handle (context.Context ) error
132- Handle (cloudevents.Event )
133- Handle (cloudevents.Event ) error
134- Handle (context.Context , cloudevents.Event )
135- Handle (context.Context , cloudevents.Event ) error
136- Handle (cloudevents.Event ) *cloudevents.Event
137- Handle (cloudevents.Event ) (*cloudevents.Event , error )
138- Handle (context.Context , cloudevents.Event ) *cloudevents.Event
139- Handle (context.Context , cloudevents.Event ) (*cloudevents.Event , error )
133+ (f *Function) Handle ()
134+ (f *Function) Handle () error
135+ (f *Function) Handle (context.Context )
136+ (f *Function) Handle (context.Context ) error
137+ (f *Function) Handle (cloudevents.Event )
138+ (f *Function) Handle (cloudevents.Event ) error
139+ (f *Function) Handle (context.Context , cloudevents.Event )
140+ (f *Function) Handle (context.Context , cloudevents.Event ) error
141+ (f *Function) Handle (cloudevents.Event ) *cloudevents.Event
142+ (f *Function) Handle (cloudevents.Event ) (*cloudevents.Event , error )
143+ (f *Function) Handle (context.Context , cloudevents.Event ) *cloudevents.Event
144+ (f *Function) Handle (context.Context , cloudevents.Event ) (*cloudevents.Event , error )
140145```
141146
142147For example, a ` CloudEvent ` is received which contains a JSON string such as this in its data property,
@@ -157,7 +162,11 @@ type Purchase struct {
157162 ProductId string ` json:"productId"`
158163}
159164
160- func Handle (ctx context .Context , event cloudevents .Event ) err error {
165+ type Function struct {}
166+
167+ func New () *Function { return &Function{} }
168+
169+ func (f *Function ) Handle (ctx context .Context , event cloudevents .Event ) err error {
161170
162171 purchase := &Purchase{}
163172 if err = cloudevents.DataAs (purchase); err != nil {
@@ -172,8 +181,12 @@ func Handle(ctx context.Context, event cloudevents.Event) err error {
172181Or we can use Golang's ` encoding/json ` package to access the ` CloudEvent ` directly as
173182a JSON in form of bytes array:
174183
175- ``` golang
176- func Handle (ctx context .Context , event cloudevents .Event ) {
184+ ``` go
185+ type Function struct {}
186+
187+ func New () *Function { return &Function{} }
188+
189+ func (f *Function ) Handle (ctx context .Context , event cloudevents .Event ) {
177190
178191 bytes , err := json.Marshal (event)
179192
@@ -186,7 +199,11 @@ As mentioned above, HTTP triggered functions can set the response directly via
186199Golang's [ http.ResponseWriter] ( https://golang.org/pkg/net/http/#ResponseWriter ) .
187200
188201``` go
189- func Handle (ctx context .Context , res http .ResponseWriter , req *http .Request ) {
202+ type Function struct {}
203+
204+ func New () *Function { return &Function{} }
205+
206+ func (f *Function ) Handle (ctx context .Context , res http .ResponseWriter , req *http .Request ) {
190207
191208 // Set response
192209 res.Header ().Add (" Content-Type" , " text/plain" )
@@ -206,7 +223,11 @@ to set a unique `ID`, proper `Source` and a `Type` of the CloudEvent. The data c
206223from a defined structure or from a ` map ` .
207224
208225``` go
209- func Handle (ctx context .Context , event cloudevents .Event ) (resp *cloudevents .Event , err error ) {
226+ type Function struct {}
227+
228+ func New () *Function { return &Function{} }
229+
230+ func (f *Function ) Handle (ctx context .Context , event cloudevents .Event ) (resp *cloudevents .Event , err error ) {
210231
211232 // ...
212233
0 commit comments