ยง2023-02-27

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

```rb
require 'sinatra'
get '/frank-says' do
  'Put this in your pipe & smoke it!'
end
```
  1. Qucik-start
$ mkdir sinatra-quick-start && cd $_
$ bundle init
Writing new Gemfile to /home/alexlai/ruby-proj/sinatra-quick-start/Gemfile
$ cat Gemfile 
# frozen_string_literal: true

source "https://rubygems.org"

# gem "rails"
  1. Edit Gemfile as,
# frozen_string_literal: true

source "https://rubygems.org"

# gem "rails"
gem "sinatra"
gem "puma"
  1. bundle install
(FRUM) alexlai@hc4Bullseye:~/ruby-project/sinatra-quick-start$ bundle install
Fetching gem metadata from https://rubygems.org/....
Resolving dependencies...
Using bundler 2.4.6
Using nio4r 2.5.8
Using rack 2.2.6.2
Using ruby2_keywords 0.0.5
Fetching tilt 2.1.0
Fetching rack-protection 3.0.5
Fetching puma 6.1.0
Fetching mustermann 3.0.0
Installing rack-protection 3.0.5
Installing mustermann 3.0.0
Installing tilt 2.1.0
Fetching sinatra 3.0.5
Installing puma 6.1.0 with native extensions
Installing sinatra 3.0.5
Bundle complete! 2 Gemfile dependencies, 9 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
  1. ~/myapp.rb as
require 'sinatra'

get '/' do
  'Hello world!'
end
  1. run it
$ ruby myapp.rb 
== Sinatra (v3.0.4) has taken the stage on 4567 for development with backup from Puma
Puma starting in single mode...
* Puma version: 6.1.0 (ruby 3.2.1-p31) ("The Way Up")
*  Min threads: 0
*  Max threads: 5
*  Environment: development
*          PID: 22648
* Listening on http://127.0.0.1:4567
* Listening on http://[::1]:4567
Use Ctrl-C to stop
127.0.0.1 - - [27/Feb/2023:08:23:14 +0800] "GET / HTTP/1.1" 200 12 0.0253
127.0.0.1 - - [27/Feb/2023:08:23:14 +0800] "GET /favicon.ico HTTP/1.1" 404 469 0.0027
  1. Verify with http://localhost:4567

The code you changed will not take effect until you restart the server. Please restart the server every time you change or use a code reloader like [rerun](https://github.com/alexch/rerun) or [rack-unreloader](https://github.com/jeremyevans/rack-unreloader).

  1. Modified Gemfile as,
# frozen_string_literal: true

source "https://rubygems.org"

# gem "rails"
gem "sinatra"
gem "puma"
gem "rerun"

and, bundler install [bundle and bundler can be used interchangeably](https://stackoverflow.com/questions/43299366/bundle-vs-bundler-bundle-vs-bundle-install

  1. Start with rerun
 rerun myapp.rb 

08:31:07 [rerun] Sinatra-quick-start launched
08:31:07 [rerun] Rerun (22948) running Sinatra-quick-start (22956)
== Sinatra (v3.0.4) has taken the stage on 4567 for development with backup from Puma
Puma starting in single mode...
* Puma version: 6.1.0 (ruby 3.2.1-p31) ("The Way Up")
*  Min threads: 0
*  Max threads: 5
*  Environment: development
*          PID: 22956
* Listening on http://127.0.0.1:4567
* Listening on http://[::1]:4567
Use Ctrl-C to stop
08:31:09 [rerun] Watching . for **/*.{rb,js,coffee,css,scss,sass,erb,html,haml,ru,yml,slim,md,feature,c,h} with Linux adapter
08:31:44 [rerun] Change detected: 1 modified: myapp.rb
- Gracefully stopping, waiting for requests to finish
== Sinatra has ended his set (crowd applauds)

08:31:44 [rerun] Sinatra-quick-start restarted
08:31:44 [rerun] Rerun (22948) running Sinatra-quick-start (23064)
== Sinatra (v3.0.4) has taken the stage on 4567 for development with backup from Puma
Puma starting in single mode...
* Puma version: 6.1.0 (ruby 3.2.1-p31) ("The Way Up")
*  Min threads: 0
*  Max threads: 5
*  Environment: development
*          PID: 23064
* Listening on http://127.0.0.1:4567
* Listening on http://[::1]:4567
Use Ctrl-C to stop
127.0.0.1 - - [27/Feb/2023:08:31:51 +0800] "GET / HTTP/1.1" 200 25 0.0129