11# Introduction to Fn with Ruby
2-
32Fn is a lightweight Docker-based serverless functions platform you can run on
43your laptop, server, or cloud. In this introductory tutorial we'll walk through
5- installing Fn, develop a function using Ruby (without installing a Ruby
6- compiler !) and deploy them to a local Fn server. We'll also learn about the core
7- Fn concepts like applications and routes.
4+ developing a function using the Ruby programming language (without installing
5+ any Ruby tools !) and deploying that function to a local Fn server. We'll also
6+ learn about the core Fn concepts like applications and routes.
87
9- So let's get started!
8+ ### Before you Begin
9+ * Set aside about 15 minutes to complete this tutorial.
10+ * Make sure Fn server is up and running by completing the [ Install and Start Fn Tutorial] ( ../../install/README.md ) .
1011
11- As you make your way through this tutorial, look out for this icon.
12+ > As you make your way through this tutorial, look out for this icon.
1213![ ] ( images/userinput.png ) Whenever you see it, it's time for you to
1314perform an action.
1415
15-
16- ## Installing Fn
17-
18- Setting up a working Fn install is a two-step process. First you need
19- to ensure you have the necessary prerequisites and then you can install
20- Fn itself.
21-
22- ### Prerequisites
23-
24- Before we can install Fn you'll need:
25-
26- 1 . A computer running Linux or MacOS. If you have a Windows machine the
27- easiest thing to do is install [ VirtualBox] ( https://www.virtualbox.org/ )
28- and run a free Linux virtual machine.
29- 2 . [ Docker] ( https://www.docker.com/ ) 17.05 (or higher) needs to be
30- installed and running.
31-
32- > __ NOTE__ In this tutorial we'll work in a purely local development
33- mode. However, when deploying functions to a remote Fn server, a Docker
34- Hub (or other Docker registry) account is required.
35-
36- That's it. You can use your favorite IDE for function development.
37- However, for this tutorial, an IDE isn't necessary.
38-
39-
40- ### Downloading and Installing Fn
41-
42- From a terminal type the following:
43-
44- ![ ] ( images/userinput.png )
45- > ` curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh `
46-
47- Once installed you'll see the Fn version printed out. You should see
48- something similar to the following displayed (although likely with a later
49- version number):
50-
51- ``` sh
52- fn version 0.4.66
53- ```
54-
55- ### Starting Fn Server
56-
57- The final install step is to start the Fn server. Since Fn runs on
58- Docker it'll need to be up and running too.
59-
60- To start Fn you can use the ` fn ` command line interface (CLI). Type the
61- following but note that the process will run in the foreground so that
62- it's easy to stop with Ctrl-C:
63-
64- ![ user input] ( images/userinput.png )
65- > ` fn start `
66-
67- You should see output similar to:
68-
69- ``` sh
70- time=" 2017-09-18T14:37:13Z" level=info msg=" datastore dialed" datastore=sqlite3 max_idle_connections=256
71- time=" 2017-09-18T14:37:13Z" level=info msg=" available memory" ram=1655975936
72- time=" 2017-09-18T14:37:13Z" level=info msg=" Serving Functions API on address ` :8080` "
73-
74- ______
75- / ____/___
76- / /_ / __ \
77- / __/ / / / /
78- /_/ /_/ /_/
79- ```
80-
81- Let's verify everthing is up and running correctly.
82-
83- ** Open a new terminal** and run the following:
84-
85- ![ user input] ( images/userinput.png )
86- > ` fn version `
87-
88- You should see the version of the fn CLI (client) and server displayed (your version will
89- likely differ):
90-
91- ``` sh
92- Client version: 0.4.66
93- Server version: 0.3.385
94- ```
95-
96- If the server version is "?" then the fn CLI cannot reach the server. If
97- this happens it's likely you have something else running on port 8080. In this
98- case stop the other process, and stop (ctrl-c) and restart the fn server as
99- described previously.
100-
10116## Your First Function
102-
103- Let's start with a very simple "hello world" function written in [ Ruby] ( https://www.ruby-lang.org/ ) . Don't worry, you don't need to know Ruby! In
104- fact you don't even need to have Ruby installed on your development machine as
105- Fn provides the necessary Ruby compiler and tools as a Docker container. Let's
106- walk through your first function to become familiar with the process and how Fn
107- supports development.
17+ Let's start with a very simple "hello world" function written in
18+ [ Ruby] ( https://www.ruby-lang.org/ ) . Don't worry, you don't need to know Ruby!
19+ In fact you don't even need to have Ruby installed on your development machine
20+ as Fn provides the necessary Ruby compiler and tools as a Docker container.
21+ Let's walk through your first function to become familiar with the process and
22+ how Fn supports development.
10823
10924Before we start developing we need to set the ` FN_REGISTRY `
11025environment variable. Normally, it's set to your Docker Hub username.
@@ -232,7 +147,7 @@ the necessary Docker images.
232147> ` fn run`
233148
234149` ` ` yaml
235- Building image fndemouser/rubyfn:0.0.1
150+ Building image fndemouser/rubyfn:0.0.1
236151call_id: 12345678901234567890123456
237152{" message" :" Hello World!" }
238153` ` `
@@ -248,7 +163,7 @@ enabled.
248163> `fn --verbose run`
249164
250165```yaml
251- Building image fndemouser/rubyfn:0.0.1
166+ Building image fndemouser/rubyfn:0.0.1
252167Sending build context to Docker daemon 6.144kB
253168Step 1/9 : FROM fnproject/ruby:dev as build-stage
254169 ---> 907fbac5f177
@@ -290,7 +205,7 @@ You can also pass data to the run command. For example:
290205>```
291206
292207```yaml
293- Building image fndemouser/rubyfn:0.0.1
208+ Building image fndemouser/rubyfn:0.0.1
294209call_id: 12345678901234567890123456
295210{"message":"Hello Bob!"}
296211```
@@ -351,7 +266,7 @@ You should see output similar to:
351266` ` ` yaml
352267Deploying rubyfn to app: rubyapp at path: /rubyfn
353268Bumped to version 0.0.2
354- Building image fndemouser/rubyfn:0.0.2
269+ Building image fndemouser/rubyfn:0.0.2
355270Updating route /rubyfn using image fndemouser/rubyfn:0.0.2...
356271` ` `
357272
@@ -430,7 +345,7 @@ that incorporates our application and function route as path elements.
430345Use curl to invoke the function:
431346
432347
433- >`curl http://localhost:8080/r/rubyapp/rubyfn`
348+ >`curl -H "Content-Type: application/json" http://localhost:8080/r/rubyapp/rubyfn`
434349
435350The result is once again the same.
436351
@@ -442,7 +357,7 @@ We can again pass JSON data to our function get the value of name passed to the
442357function back.
443358
444359
445- >`curl http://localhost:8080/r/rubyapp/rubyfn -d ' {" name" :" Bob" }' `
360+ >`curl -H "Content-Type: application/json" -d ' {" name" :" Bob" }' http://localhost:8080/r/rubyapp/rubyfn `
446361
447362The result is once again the same.
448363
0 commit comments