sinatra introduction
DESCRIPTION
Ruby Tuesday 6/30 xdite's talkabout Sinatra / Rack / Rails MetalTRANSCRIPT
Agenda
• Sinatra Introduction
• Real Example
• Rack
• Rails Intergation
自我介紹
• http://twitter.com/xdite
• http://blog.xdite.net
• Rails Developer
Sinatra is ...
• ruby web framework library
• not MVC ( Model-View-Controller )
• DSL for defining RESTful HTTP actions
RAILS LINGOORM , MVC , Testing , Mail ,
Generator, RJS, I18n, Template , Engine .....
SINATRA LINGOGET / POST / PUT / DELETE , request
paramenters, response, views , helpers , configure , rack
Single file web-app
Single file web-app
Hello World!
HTTP actions
Routes
Views
Helpers
Filter
Configure
適用場合
• Web Service / API
• Tiny Web Application
•Rails Metal
Web Service / API
Requests per second: 81.09 [#/sec] (mean)Time per request: 12.332 [ms] (mean)
Requests per second: 450.56 [#/sec] (mean)Time per request: 2.219 [ms] (mean)
Rails action
Sinatra
(Ubuntu 8.04, Intel Atom N270 @1.60GHz)
Tiny Web Application
• simple
• cherry pick
• ActiveRecord / Datamapper
• Prototype / jQuery
追蹤 twitter 上痛恨 IE6 的人
• CodeIgniter - PHP framework
• 使用 Twitter API 追蹤特定關鍵字並展示
• LOC : 1000+
• NOT support CJK
require 'rubygems'require 'sinatra'require 'twitter_search'get '/' do @client = TwitterSearch::Client.new 'politweets' @results = @client.query :q => '@MrIE6' erb :homeend
10 行內收工..
• ORM
• Paginate
多寫了 40 行左右
Rails Metal with Sinatra
Core Code Middleware Mongrel
rack 是一種 middleware
Rack provides a minimal interface between webservers supporting Ruby and Ruby frameworks
http ProfilingMiddleware
SecurityMiddleware
CachingMiddleware
Handlerfor Rack
Core Code
$ rake middleware
Scaling Rails - Metal
• 使用 Rack 製作一個超級快的 action
• 將 Sinatra 當作一個 Metal app 使用
Performance
class Poller < Rails::Rack::Metal def call(env) if env["PATH_INFO"] =~ /^\/poller/ [[200], {"Content-Type" => "text/html"}, "Hello, World!"] else [[404], {"Content-Type" => "text/html"}, "Not Found"] end endend
class OldPollerController < ApplicationController def poller render :text => "Hello World!" endend
後者比前者快了 25 倍
Requests per second: 81.09 [#/sec] (mean)Time per request: 12.332 [x`x] (mean)
Requests per second: 163.75 [#/sec] (mean)Time per request: 6.107 [ms] (mean)
Rails action
Metal + Sinatra
(Ubuntu 8.04, Intel Atom N270 @1.60GHz)
一般狀況
Q & A
Resources
• Scaling Rails : Episode #14 Rack
• RailsCast: Episode #251 Rack Middleware
• Pragprog.com : Classy Web Development with Sinata
• Github / xdite : tweesage-message-wall