One Framework a Day keeps the Boredom Away: Sinatra

Welcome to this new edition of One Framework a Day keeps the Boredom Away. In this series I will show you how to deploy a particular framework on Clever Cloud every day until I want to go back to boredom. Today it's about Sinatra.

In each post of this series we'll see how to deploy a particular framework on Clever Cloud. Today we are taking a look at Sinatra.

If you want to tag along, make sure you have git, a Clever Cloud account and that you have installed our CLI Clever-Tools.

What is Sinatra?

Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort

It's been around for 10 years now and has already inspired several other framework since then. Ratpack which was showcased last week has been heavily inspired by sinatra for instance. Our own Keruspe wanted to play with a couple of Ruby technologies like Puma and Sinatra. And so he built a sample project for it. That's what we are going to deploy today.

Setup

  • Start by cloning the sources: git clone https://github.com/CleverCloud/demo-sinatra-puma-activerecord
  • Create the database you want to use, in my case Postgres: clever addon create postgresql-addon --plan dev --region eu sinatraPG
  • Create the Ruby application on Clever Cloud: clever create --type ruby sinatraSample --region par
  • Link the Postgres add-on to the application: clever service link-addon sinatraPG
  • Tell Clever Cloud to deploy the API using puma (the default is uwsgi as of now): clever env set CC_RACKUP_SERVER puma
  • Tell Clever Cloud to run database migrations before launching the API:clever env set CC_PRE_RUN_HOOK 'bundle exec rake db:migrate'
  • Add a domain name: clever domain add mySinatraSample.cleverapps.io

Specify the maximum number of PG connections your application will use (depending on the selected plan) by adding an environment variable like DB_POOL=5 if you want to use 5 connections. For production, you should also set:

clever env set RACK_ENV production
clever env set RAILS_ENV production

Deploy

  • Deploy the application with clever deploy

And you are up and running. This application provides an API to store users. They only have a name field. Users can be created with a POST request and retrieve with a GET request like so:

  • Create an object with curl --data "regis" https://mySinatraSample.cleverapps.io
  • Retrieve that object with curl https://mySinatraSample.cleverapps.io/regis

Everything works out of the box because this application has been written for Clever Cloud. If you take a look at the condfiguration file under ./config/database.yml you should see some environment variables being used. They are the one provided by our Postgres add-on. You can get the full list of variables with clever env.

Let us know your thoughts on this in the comments below. We will be back tomorrow for another post 🙂

Blog

À lire également

Clever Cloud announces 11 new products at its Clever Cloud Fest

Clever Cloud is celebrating its 15th anniversary with the Clever Cloud Fest on 6 and 7 February 2025 in Nantes. This event will bring together customers and partners, during which Clever Cloud will unveil 11 new products and an international growth strategy.
Company Événements Press

What is Clever AI?

AI services are everywhere, but the sheer number and diversity of them makes it harder for developers to manage them. All the more so when they work in a team. That's why we came up with Clever AI: a multi-vendor, multi-model range of solutions to simplify access to AI for businesses.
Company

Protect yourself: beware of job scams spoofing Clever Cloud’s brand

At Clever Cloud, we provide reliable, secure cloud hosting services for businesses and developers worldwide. Unfortunately, our reputation is being exploited by malicious actors engaging in fraudulent activities under the guise of our company name. We want to set the record straight and help protect you from falling victim to these scams.
Company