#Sinatra Web Server
Details | |
---|---|
Re-created by: | Holloway, Chew Kean Ho |
Version: | 0.0.9 |
Contribution: | Hobby. Best effort basis. |
This Ruby Sinatra web skeleton was re-furbished for rapid prototyping a web API before deploying to rails server. It's referenced from Rails file structure with mild tweaking and CodeDivision Sinatra skeleton.
##Supports
- Local Support
- Heroku Support - using PUMA
- Bluemix Support - using PUMA
- Cloud9 Support - using Shotgun
NOTE: This guide assumes you are good with Ruby, Heroku, Bluemix and understands MVC architecture patterns.
- Heroku - https://github.com/hollowaykeanho/sinatra-web-server/tree/heroku
- Bluemix - https://github.com/hollowaykeanho/sinatra-web-server/tree/bluemix
- Cloud9 - continue to read README.
Master branch currently based on Heroku platform. Please use with caution.
- Perform a git clone to this repo using the following link:
# http
$ git clone https://github.com/hollowaykeanho/sinatra-web-server.git
# ssh
$ git clone [email protected]:hollowaykeanho/sinatra-web-server.git
# Heroku Platform
$ git clone -b heroku https://github.com/hollowaykeanho/sinatra-web-server.git
# Bluemix Platform
$ git clone -b bluemix https://github.com/hollowaykeanho/sinatra-web-server.git
- Rename the skeleton if needed
$ mv sinatra-web-server <your-desired-app-name>
- Enter into the skeleton and perform bundle install
$ cd <your-desired-app-name>
$ bundle install
# Open issue in this github repo if any issue
- Perform a short test by launching the server
$ rake server
If you're working on cloud9 server, you should use the following command:
$ rake c9-server
- Hooray! You may now begin your code development.
The skeleton is primarily based on Rails file structure with focus towards MVC architectural pattern. However, unlike Rails, this skeleton is to provide more structural freedom for you to prototype or to bootstrap and idea quickly. The flexibility is up to deploying the SaaS platform.
Rakefile has a simplified command for launching the server in development mode. To perform, execute:
$ rake server
- Include/remove your gem inside Gemfile depending on group.
# File location: <repo_root>\Gemfile
- Perform bundle install
$ bundle install
- Head to config\environments\init.rb to ensure your require is aligned to your adjustment.
# Perform requiring gem that we need
#######################################################
# basic
require 'rubygems'
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'pathname'
# database
require 'pg'
require 'active_record'
require 'logger'
# sinatra
require 'sinatra'
require "sinatra/reloader" if development?
# embedded ruby
require 'erb'
require 'uri'
# Additional Gem includes after this comments
#######################################################
- Done. You're ready.
You can create a controller ruby file inside app/controllers manually. As long as there is no conflicted routes, you can create many controller files. Sinatra go through each controller file and compile all available route.
In this example, let's create 'sessions' routing:
# app/controllers/users.rb
get '/signup' do
erb :'users/new'
end
post '/signup' do
# Do something processing with user input
redirect to '/user/dashboard'
end
get '/user/dashboard' do
erb :dashboard
end
### To Create Views You can create a view erb file inside **app/views** manually. This framework uses erb gem to generate the view. Views can be created in full-form or partial-form. Examples, #### To create simple erb view file: ``` # app/views/root.erb ... # To route it, use ' erb :root ' in controller ``` #### To create erb view file inside a sub-folder: ``` # app/views/users/new.erb ... # To route it, use ' erb :"users/new" ' in controller ``` #### To create partial erb view file: ``` # app/views/partials/form.erb ...
# in app/views/users/new.erb
<h2> User Data </h2>
<%= erb :"partial/form" %>
<br><br>
### To Create Model
Model creation is supported by Rakefile. To create, simply execute:
$ rake generate:model NAME=<singular_model_name>
>**NOTE**:
> Due to maintaining structural freedom, rake will only create model file and **not** with database migration file. You're expected to handle database migration file separately.
<br><br>
### To Create Helper
Helper file can be created inside **app/helpers** manually and at will. To ensure the functions are working in the helpers, any helper file should has the helpers loop. Example:
helpers do # for repetitive in html view def em(param) ... end
# for repetitive math calculation
def calculate_square(param)
param * param
end
# More repetitive functions
...
end
<br>
Any function within helpers loop can be called directly like a Ruby modules' methods. Example:
post '/' do calculate_square(params[:input]) end
... <%= em("String") %> ... ``` More information can be found here: https://www.sinatrarb.com/faq.html#helpviewDatabase creation is supported by Rakefile. To create, execute:
$ rake db:create
Database migration file creation is supported by Rakefile. To create, execute:
$ rake generate:migration NAME=<filename>
REMEMBER: ------be careful with ActiveRecord naming convention especially singular/plural!
Database migration is supported by Rakefile. To perform, execute:
$ rake db:migrate
Database migration is supported by Rakefile. To perform, execute:
$ rake db:drop
Database data seeding is supported by Rakefile. To perform, execute:
$ rake db:seed
Database current migration version view is supported by Rakefile. To perform, execute:
$ rake db:version
- CodeDivision for their code bootcamp training.
- Josh who motivated me to re-code this framework.
- All friends and teams in CodeDivision.
- https://www.blacktm.com/talks/building_web_apps_with_rack_and_sinatra/#simple_rack
- https://www.sinatrarb.com/intro.html#Environments
- https://nycda.com/blog/integrating-activerecord-into-a-sinatra-project/
- https://rake.rubyforge.org/
- https://recipes.sinatrarb.com/p/development/bundler
- https://robots.thoughtbot.com/test-rake-tasks-like-a-boss
- https://apidock.com/rails/String/singularize
- https://code.tutsplus.com/tutorials/how-to-integrate-rspec-into-a-sinatra-app--net-21564
- https://www.millwoodonline.co.uk/blog/mini-minitest-tutorial
- https://www.sinatrarb.com/configuration.html
- https://devcenter.heroku.com/articles/getting-started-with-ruby-o
- https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server
- https://blog.codeship.com/puma-vs-unicorn/
- https://devcenter.heroku.com/articles/getting-started-with-rails3
- https://www.getlaura.com/how-to-enable-sessions-with-sinatra/
- https://stackoverflow.com/questions/5693528/how-to-use-sinatra-session
- https://www.sinatrarb.com/configuration.html
- sinatra/sinatra#495
- https://stackoverflow.com/questions/18302934/how-to-set-a-custom-directory-for-layouts-in-sinatra
- https://www.sinatrarb.com/intro.html#Inline%20Templates