Skip to content

Commit 794f64b

Browse files
authored
Merge pull request fnproject#188 from fnproject/aeicher_fix_go_intro
Update Go tutorial to reflect changes in the CLI init function
2 parents 3ebd2e1 + 21d3ab5 commit 794f64b

File tree

1 file changed

+126
-80
lines changed

1 file changed

+126
-80
lines changed

Introduction/README.md

Lines changed: 126 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ In the terminal type the following:
2828

2929
![User Input Icon](images/userinput.png)
3030
>```sh
31-
> fn init --runtime go --trigger http gofn
31+
> fn init --runtime go gofn
3232
>```
3333
3434
The output will be
3535
3636
```yaml
37-
Creating function at: /gofn
38-
Runtime: go
37+
Creating function at: ./gofn
3938
Function boilerplate generated.
4039
func.yaml created.
4140
```
@@ -61,7 +60,7 @@ Now get a list of the directory contents.
6160
>```
6261
6362
```txt
64-
Gopkg.toml func.go func.yaml
63+
func.go func.yaml go.mod
6564
```
6665
6766
The `func.go` file which contains your actual Go function is generated along
@@ -124,10 +123,6 @@ name: gofn
124123
version: 0.0.1
125124
runtime: go
126125
entrypoint: ./func
127-
triggers:
128-
- name: gofn-trigger
129-
type: http
130-
source: /gofn-trigger
131126
```
132127
133128
The generated `func.yaml` file contains metadata about your function and
@@ -140,10 +135,6 @@ declares a number of properties including:
140135
in `--runtime`.
141136
* entrypoint--the name of the executable to invoke when your function is called,
142137
in this case `./func`
143-
* triggers--identifies the automatically generated trigger name and source. For
144-
example, this function would be executed from the URL
145-
<http://localhost:8080/t/appname/gofn-trigger>. Where appname is the name of
146-
the app chosen for your function when it is deployed.
147138

148139
There are other user specifiable properties but these will suffice for
149140
this example. Note that the name of your function is taken from the containing
@@ -152,7 +143,7 @@ folder name. We'll see this come into play later on.
152143
### Other Function Files
153144
The `fn init` command generated one other file.
154145

155-
* `Gopkg.toml` -- the Go dep tool dependency management tool file which
146+
* `go.mod` -- the Go modules file which
156147
specifies all the dependencies for your function.
157148

158149
## Deploy Your First Function
@@ -163,16 +154,16 @@ running in the cloud, in your datacenter, or on your local machine like we're
163154
doing here.
164155

165156
### Check your Context
166-
Make sure your context is set to default and you are using a demo user. Use the `fn list context` command to check.
157+
Make sure your context is set to default and you are using a demo user. Use the `fn list contexts` command to check.
167158

168159
![user input](images/userinput.png)
169160
>```sh
170161
> fn list contexts
171162
>```
172163
173164
```cs
174-
CURRENT NAME PROVIDER API URL REGISTRY
175-
* default default http://localhost:8080 fndemouser
165+
CURRENT NAME PROVIDER API URL REGISTRY
166+
* default default http://localhost:8080 fndemouser
176167
```
177168
178169
If your context is not configured, please see [the context installation instructions](https://github.com/fnproject/tutorials/blob/master/install/README.md#configure-your-context) before proceeding. Your context determines where your function is deployed.
@@ -211,63 +202,45 @@ You should see output similar to:
211202
```yaml
212203
Deploying gofn to app: goapp
213204
Bumped to version 0.0.2
214-
Building image fndemouser/gofn:0.0.2
205+
Building image fndemouser/gofn:0.0.2
215206
FN_REGISTRY: fndemouser
216207
Current Context: default
217208
Sending build context to Docker daemon 5.12kB
218209
Step 1/10 : FROM fnproject/go:dev as build-stage
219-
dev: Pulling from fnproject/go
220-
ff3a5c916c92: Already exists
221-
f32d2ea73378: Pull complete
222-
3bdfb30a4c89: Pull complete
223-
6487ee6212c5: Pull complete
224-
074903419fc0: Pull complete
225-
3db945ee2177: Pull complete
226-
Digest: sha256:6ebffaea00a2f53373c68dd52e0df209d7e464d691db0d52b31060d06df8e839
227-
Status: Downloaded newer image for fnproject/go:dev
228-
---> fac877f7d14d
210+
---> 96c8fb94a8e1
229211
Step 2/10 : WORKDIR /function
230-
---> Running in ec4e59c12f13
231-
Removing intermediate container ec4e59c12f13
232-
---> 98fcbeba3fcc
233-
Step 3/10 : RUN go get -u github.com/golang/dep/cmd/dep
234-
---> Running in d04b9dfa8dc6
235-
Removing intermediate container d04b9dfa8dc6
236-
---> 3bae0bbe8a81
237-
Step 4/10 : ADD . /go/src/func/
238-
---> b33514c5876b
239-
Step 5/10 : RUN cd /go/src/func/ && dep ensure
240-
---> Running in 6147e9cf3ddf
241-
Removing intermediate container 6147e9cf3ddf
242-
---> 2f7f21a2eb15
212+
---> Using cache
213+
---> bee171e861d4
214+
Step 3/10 : WORKDIR /go/src/func/
215+
---> Using cache
216+
---> d0102d3148a1
217+
Step 4/10 : ENV GO111MODULE=on
218+
---> Using cache
219+
---> 22ecbf50c559
220+
Step 5/10 : COPY . .
221+
---> 0a2992d2d99a
243222
Step 6/10 : RUN cd /go/src/func/ && go build -o func
244-
---> Running in 81188a61a4d1
245-
Removing intermediate container 81188a61a4d1
246-
---> 1af60a363a46
223+
---> Running in e480baa937d4
224+
go: finding github.com/fnproject/fdk-go latest
225+
go: downloading github.com/fnproject/fdk-go v0.0.0-20190716163646-1458ca84e01d
226+
Removing intermediate container e480baa937d4
227+
---> d8cc615e1e64
247228
Step 7/10 : FROM fnproject/go
248-
latest: Pulling from fnproject/go
249-
1eae7a7426b0: Pull complete
250-
7a855df78530: Pull complete
251-
Digest: sha256:8e03716b576e955c7606e4d8b8748c0f959a916ce16ba305ab262f042562340f
252-
Status: Downloaded newer image for fnproject/go:latest
253-
---> 76aed4489768
229+
---> bc635796c9df
254230
Step 8/10 : WORKDIR /function
255-
---> Running in ce2fc52be3a7
256-
Removing intermediate container ce2fc52be3a7
257-
---> 9d2da540bf02
231+
---> Using cache
232+
---> b853b5d6b840
258233
Step 9/10 : COPY --from=build-stage /go/src/func/func /function/
259-
---> cf718c6f8fd8
234+
---> Using cache
235+
---> ee3af55a0670
260236
Step 10/10 : ENTRYPOINT ["./func"]
261-
---> Running in a60600bcb994
262-
Removing intermediate container a60600bcb994
263-
---> efa4793bf85f
264-
Successfully built efa4793bf85f
237+
---> Using cache
238+
---> 3e41594de5c8
239+
Successfully built 3e41594de5c8
265240
Successfully tagged fndemouser/gofn:0.0.2
266241
267242
Updating function gofn using image fndemouser/gofn:0.0.2...
268243
Successfully created function: gofn with fndemouser/gofn:0.0.2
269-
Successfully created trigger: gofn-trigger
270-
Trigger Endpoint: http://localhost:8080/t/goapp/gofn-trigger
271244
```
272245
273246
All the steps to load the current language Docker image are displayed.
@@ -284,8 +257,7 @@ let's us know that the function is packaged in the image
284257

285258
Note that the containing folder name `gofn` was used as the name of the
286259
generated Docker container and used as the name of the function that container
287-
was bound to. By convention it is also used to create the trigger name
288-
`gofn-trigger`.
260+
was bound to.
289261

290262
Normally you deploy an application without the `--verbose` option. If you rerun the command a new image and version is created and loaded.
291263

@@ -383,33 +355,107 @@ NAME ID
383355
goapp 01D37WY2N2NG8G00GZJ0000001
384356
```
385357
386-
We can also see the functions that are defined by an application. Since functions are exposed via triggers, the `fn list triggers <appname>` command is used. To list the functions included in "goapp" we can type:
358+
The `fn list functions <app-name>` command lists all the functions associated with an app.
387359

388-
![User Input Icon](images/userinput.png)
360+
![user input](images/userinput.png)
389361
>```sh
390-
> fn list triggers goapp
362+
> fn list functions goapp
391363
>```
392364
393-
```sh
394-
FUNCTION NAME ID TYPE SOURCE ENDPOINT
395-
gofn gofn-trigger 01D37X3AVGNG8G00GZJ0000003 http /gofn-trigger http://localhost:8080/t/goapp/gofn-trigger
365+
The returns all the functions associated with the `goapp`.
366+
367+
```
368+
NAME IMAGE ID
369+
gofn fndemouser/gofn:0.0.2 01DJZQXW47NG8G00GZJ0000014
396370
```
397371
398372
The output confirms that `goapp` contains a `gofn` function which may be invoked via the
399-
specified URL. Now that we've confirmed deployment was successsful, let's
373+
specified URL. Now that we've confirmed deployment was successful, let's
400374
call our function.
401375

376+
# Invoke your Deployed Function
377+
378+
There are two ways to call your deployed function.
379+
380+
### Invoke with the CLI
381+
382+
The first is using the `fn` CLI which makes invoking your function relatively
383+
easy. Type the following:
384+
385+
![user input](images/userinput.png)
386+
>```sh
387+
> fn invoke goapp gofn
388+
>```
389+
390+
which results in:
391+
392+
```js
393+
{"message":"Hello World"}
394+
```
395+
396+
When you invoked "goapp gofn" the Fn server looked up the
397+
"goapp" application and then looked for the Docker container image
398+
bound to the "gofn" function and executed the code.
399+
400+
You can also pass data to the run command. Note that you set the content type for the data passed. For example:
401+
402+
![user input](images/userinput.png)
403+
>```sh
404+
> echo -n '{"name":"Bob"}' | fn invoke goapp gofn --content-type application/json
405+
>```
406+
407+
```js
408+
{"message":"Hello Bob"}
409+
```
410+
411+
The JSON data was parsed and since `name` was set to "Bob", that value is passed
412+
in the output.
413+
414+
415+
### Getting a Function's Invoke Endpoint
416+
417+
In addition to using the Fn `invoke` command, we can call a function by using a
418+
URL. To do this, we must get the function's invoke endpoint. Use the command
419+
`fn inspect function <appname> <function-name>`. To list the `gofn` function's
420+
invoke endpoint we can type:
421+
422+
![user input](images/userinput.png)
423+
>```sh
424+
> fn inspect function goapp gofn
425+
>```
426+
427+
```js
428+
{
429+
"annotations": {
430+
"fnproject.io/fn/invokeEndpoint": "http://localhost:8080/invoke/01DJZQXW47NG8G00GZJ0000014"
431+
},
432+
"app_id": "01DJZQWHVWNG8G00GZJ0000013",
433+
"created_at": "2019-08-23T17:21:03.111Z",
434+
"id": "01DJZQXW47NG8G00GZJ0000014",
435+
"idle_timeout": 30,
436+
"image": "fndemouser/gofn:0.0.2",
437+
"memory": 128,
438+
"name": "gofn",
439+
"timeout": 30,
440+
"updated_at": "2019-08-23T17:21:03.111Z"
441+
}
442+
```
443+
444+
The output confirms that the `gofn` function's invoke endpoint is:
445+
`http://localhost:8080/invoke/01DJZQXW47NG8G00GZJ0000014`. We can use this URL
446+
to call the function.
447+
402448
### Invoke with Curl
403449

404-
The other way to invoke your function is via HTTP. The Fn server exposes our
405-
deployed function at `http://localhost:8080/t/goapp/gofn-trigger`, a URL
406-
that incorporates our application and function trigger as path elements.
450+
Once we have the invoke endpoint, the second method for invoking our function
451+
can be used, HTTP. The Fn server exposes our deployed function at
452+
`http://localhost:8080/invoke/01DJZQXW47NG8G00GZJ0000014`.
407453

408-
Use `curl` to invoke the function:
454+
Use curl to invoke the function:
409455

410456
![user input](images/userinput.png)
411457
>```sh
412-
> curl -H "Content-Type: application/json" http://localhost:8080/t/goapp/gofn-trigger
458+
> curl -X "POST" -H "Content-Type: application/json" http://localhost:8080/invoke/01DJZQXW47NG8G00GZJ0000014
413459
>```
414460
415461
The result is once again the same.
@@ -418,12 +464,12 @@ The result is once again the same.
418464
{"message":"Hello World"}
419465
```
420466
421-
We can again pass JSON data to our function and get the value of name passed to
422-
the function back.
467+
We can again pass JSON data to our function get the value of name passed to the
468+
function back.
423469

424470
![user input](images/userinput.png)
425-
>```
426-
> curl -H "Content-Type: application/json" -d '{"name":"Bob"}' http://localhost:8080/t/goapp/gofn-trigger
471+
>```sh
472+
> curl -X "POST" -H "Content-Type: application/json" -d '{"name":"Bob"}' http://localhost:8080/invoke/01DJZQXW47NG8G00GZJ0000014
427473
>```
428474
429475
The result is once again the same.
@@ -434,8 +480,8 @@ The result is once again the same.
434480
435481
## Wrap Up
436482

437-
Congratulations! In this tutorial you've accomplished a lot. You've created your
438-
first function, deployed it to your local Fn server and invoked it
439-
over HTTP.
483+
Congratulations! In this tutorial you've accomplished a lot. You've created
484+
your first function, deployed it to your local Fn server and invoked it over
485+
HTTP.
440486

441487
**Go:** [Back to Contents](../README.md)

0 commit comments

Comments
 (0)