Our great sponsors
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
-
cost_calc_app
A Sinatra app to help digital nomads know if they can afford to live in a city given how much they earn. This app was built to accompany the blog post "How to Use Sinatra to Build a Ruby Application"
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
Compared to Ruby on Rails, a full-stack web framework, Sinatra is a very lean micro-framework originally developed by Blake Mizerany to help Ruby developers build applications with "minimal effort".
Because of its lightweight and Rack-based architecture, Sinatra is great for building APIs, mountable app engines, command-line tools, and simple apps like the one we'll build in this tutorial.
You can also get the full code for the example app here.
This one file contains everything needed for this simplified app to run. Run it with ruby main.rb, which should spin up an instance of the Thin web server (the default web server that comes with Sinatra). Visit localhost:4567 and you'll see the JSON response.
Once you've successfully deployed your Sinatra app, you can easily use Appsignal's Ruby APM service. AppSignal offers an integration for Rails and Rack-based apps like Sinatra.
Database adapter and ORM gems included in the Gemfile. We are using ActiveRecord for our app. Datamapper is another option you could use.
# Gemfile source "https://rubygems.org" # Ruby version ruby "3.0.4" gem 'sinatra' gem 'activerecord' gem 'sinatra-activerecord' # ORM gem gem 'sinatra-contrib' gem 'thin' gem 'rake' gem 'faraday' group :development do gem 'sqlite3' # Development database adaptor gem gem 'tux' # gives you access to an interactive console similar to 'rails console' gem 'dotenv' end group :production do gem 'pg' # Production database adaptor gem end
We also add a local copy of Bulma CSS and a custom stylesheet in public/css to provide styling for our app.