DEV Community

Sergio Lima
Sergio Lima

Posted on

The New Hello World for faastRuby - The Serverless Platform for Ruby & Crystal - Part 2.

alt text

Hi folks! Before we get started, if you have not already read my previous article - The New Hello World for faastRuby. The Serverless Platform for Ruby & Crystal: Part 1 -  you should definitely give it a look over as we’re going to be building on what we learned there.

What are we going to create?

Using the faastRuby Platform, we'll create a function in Ruby and make it quickly available on the internet.

Step by Step Function Creation

Login

Enter the folder we created earlier Part 1.

$ cd faastruby
$ faastruby/> 
Enter fullscreen mode Exit fullscreen mode

Log in with your account you previously created. (Part 1).

$ faastruby/>faastruby login       
Email: you@your-email.com
Password: ********
Login successful.
Enter fullscreen mode Exit fullscreen mode

Creating the Hello World Project

Now let's create our project named, Hello World.

$ faastruby/>faastruby new-project hello-world --api
Enter fullscreen mode Exit fullscreen mode

The faastruby new-project command will create the hello-world folder with subfolders, static files, and initial functions. You will see the output below:

+ d ./hello-world
+ f ./hello-world/project.yml
+ f ./hello-world/secrets.yml
+ d ./hello-world/functions/root
+ f ./hello-world/functions/root/handler.rb
+ f ./hello-world/functions/root/faastruby.yml
+ d ./hello-world/functions/catch-all
+ f ./hello-world/functions/catch-all/handler.rb
+ f ./hello-world/functions/catch-all/faastruby.yml
+ d ./hello-world/public
+ f ./hello-world/public/faastruby.yml
+ f ./hello-world/.gitignore
Initialized empty Git repository in /Users/you/workspace/faastruby/hello-world/.git/
Project 'hello-world' initialized.
Now run:
$ cd hello-world
$ faastruby local

Then visit http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

Enter into the hello-world folder we just created.

$ faastruby/> cd hello-world
$ faastruby/hello-world/>
Enter fullscreen mode Exit fullscreen mode

Folder & files explanation

├── functions <---------------- Put your functions inside this directory
│   ├── catch-all <------------ This function will run when a function is not found.
│   │   ├── faastruby.yml
│   │   └── handler.rb
│   └── root <----------------- This function will run when a request ie made to the root path '/'
│       ├── faastruby.yml 
│       └── handler.rb
├── project.yml <-------------- Project configuration file.
├── public <------------------- Put static files inside this folder.
│   └── faastruby.yml <-------- Static files configuration.
└── secrets.yml <-------------- Put your secrets here.
Enter fullscreen mode Exit fullscreen mode

Starting faastRuby local

Now let's start the faastRuby local server and check if our project is really working.

$ faastruby/hello-world/> faastruby local
Enter fullscreen mode Exit fullscreen mode

You'll see the output

Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.0-p0), codename: Llamas in Pajamas
* Min threads: 0, max threads: 32
* Environment: production
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
2019-03-23 16:21:23 -0300 | Detecting existing functions.
---
2019-03-23 16:21:23 -0300 | Ruby functions: ["root", "catch-all"]
---
2019-03-23 16:21:23 -0300 | Crystal functions: []
---
2019-03-23 16:21:23 -0300 | Listening for changes.
---
2019-03-23 16:21:23 -0300 | faastRuby Local is ready at http://localhost:3000
Enter fullscreen mode Exit fullscreen mode

Then, visit http://localhost:3000.

If everything went well, you now have the root function response in your browser.

{
"message": "Welcome to FaaStRuby Local! Edit the function 'root' to customize this response."
}
Enter fullscreen mode Exit fullscreen mode

Creating your first function

Your first function will be called hello and it will respond in json format with the message "Hello, World!".

Note: to create functions, you must be inside the functions folder of your project.

$ faastruby/hello-world/> cd functions
$ faastruby/hello-world/functions/>
Enter fullscreen mode Exit fullscreen mode

Use faastruby new hello command to create the hello function.

$ faastruby/hello-world/functions> faastruby new hello
Enter fullscreen mode Exit fullscreen mode
+ d hello
+ d hello/spec
+ f hello/spec/spec_helper.rb
+ f hello/spec/handler_spec.rb
+ f hello/README.md
+ f hello/Gemfile
+ f hello/handler.rb
+ f hello/faastruby.yml
✔ Installing gems...
Enter fullscreen mode Exit fullscreen mode

The faastruby new command, created specific folders and files for your hello function.

Let's go inside the hello folder to implement our solution in Ruby.

$ faastruby/hello-world/functions/> cd hello
$ faastruby/hello-world/functions/hello/>
Enter fullscreen mode Exit fullscreen mode

Make the file, handler.rb look like:

# hello-world/functions/hello/handler.rb
require 'json'

def handler event
  render json: { 'message' => 'Hello, World!' }
end
Enter fullscreen mode Exit fullscreen mode

We have to modify the unit test:

# hello-world/functions/hello/spec/handler_spec.rb
require 'spec_helper'
require 'handler'
require 'json'

describe '#handler' do
  let(:body) { handler(event).body }
  let(:event) {
    Event.new(
      body: nil, query_params: {},
      headers: {}, context: nil
    )
  }

  context 'when function is requested' do
    let(:response_value) {
      JSON.parse(body).values.first
    }
    it 'returns a String' do
      expect(body).to be_a(String)
    end
    it "returns 'Hello, World!'" do
      expect(response_value).to eq('Hello, World!')
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Now we need to include a gem in the Gemfile file of this function.

Since we are using json gem within the hello function, we need to include it in Gemfile.

# hello-world/functions/hello/Gemfile
source 'https://rubygems.org'

gem 'json'

group :test do
  gem 'rspec'
  gem 'faastruby-rpc'
  gem 'faastruby'
end
Enter fullscreen mode Exit fullscreen mode

Don't forget to run bundle install

$ faastruby/hello-world/functions/hello/> bundle install
Enter fullscreen mode Exit fullscreen mode

Are our unit tests passing?

faastruby/hello-world/functions/hello/> rspec -fd
Enter fullscreen mode Exit fullscreen mode
handler
  when function is requested
    returns 'Hello, World!'
    returns a String

Finished in 3.89 seconds (files took 0.6736 seconds to load)
2 examples, 0 failures
Enter fullscreen mode Exit fullscreen mode

Starting the local server again.

Remember: to upload the local server, the faastruby local command, must be used in the main project folder.

So, go to the root folder of the hello-world project, and start the server.

$ faastruby/hello-world/> faastruby local
Enter fullscreen mode Exit fullscreen mode

Let's see how our hello function works.

Visit in your browser: http://localhost:3000/hello

You will see the following result in your browser.

{
"message": "Hello, World!"
}
Enter fullscreen mode Exit fullscreen mode

Your first deploy

When we deploy the project to the faastRuby platform, the functions and static files go to the Workspace.

In order to deploy the project, the faastruby deploy command must be used in the main project folder.

So make sure you are in the hello-world folder and run the deploy command.

$ faastruby/hello-world/> faastruby deploy
Enter fullscreen mode Exit fullscreen mode
┌ Deploying project 'hello-world' to workspace 'hello-world-stage-8f6999'
├── [✔] Connecting to workspace 'hello-world-stage-999999'
├── [✔] Uploading static assets in 'public'
├── [✔] Deploying function from 'functions/hello'
├── [✔] Deploying function from 'functions/catch-all'
└── [✔] Deploying function from 'functions/root'
* Project URL: https://hello-world-stage-999999.tor1.faast.cloud
Enter fullscreen mode Exit fullscreen mode

Finally, your function is available in the cloud! Your URL will look like the following URL.

Visit in your browser: https://hello-world-stage-999999.tor1.faast.cloud/hello

You'll see the output below:

{
"message": "Hello, World!"
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

  • We learned how to build a faastRuby project from scratch and send it to the cloud.
  • We learned how to create, test and execute a Ruby function locally using faastRuby local server.

I hope you enjoyed the article!

Top comments (0)