sinatra introduction

41

Upload: yi-ting-cheng

Post on 15-Jan-2015

3.986 views

Category:

Technology


4 download

DESCRIPTION

Ruby Tuesday 6/30 xdite's talkabout Sinatra / Rack / Rails Metal

TRANSCRIPT

Page 1: Sinatra Introduction
Page 2: Sinatra Introduction

Agenda

• Sinatra Introduction

• Real Example

• Rack

• Rails Intergation

Page 3: Sinatra Introduction

自我介紹

• http://twitter.com/xdite

• http://blog.xdite.net

• Rails Developer

Page 4: Sinatra Introduction

Sinatra is ...

• ruby web framework library

• not MVC ( Model-View-Controller )

• DSL for defining RESTful HTTP actions

Page 5: Sinatra Introduction

RAILS LINGOORM , MVC , Testing , Mail ,

Generator, RJS, I18n, Template , Engine .....

Page 6: Sinatra Introduction

SINATRA LINGOGET / POST / PUT / DELETE , request

paramenters, response, views , helpers , configure , rack

Page 7: Sinatra Introduction

Single file web-app

Page 8: Sinatra Introduction

Single file web-app

Page 9: Sinatra Introduction

Hello World!

Page 10: Sinatra Introduction

HTTP actions

Page 11: Sinatra Introduction

Routes

Page 12: Sinatra Introduction

Views

Page 13: Sinatra Introduction

Helpers

Page 14: Sinatra Introduction

Filter

Page 15: Sinatra Introduction

Configure

Page 16: Sinatra Introduction

適用場合

Page 17: Sinatra Introduction

• Web Service / API

• Tiny Web Application

•Rails Metal

Page 18: Sinatra Introduction

Web Service / API

Page 19: Sinatra Introduction

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)

Page 20: Sinatra Introduction

Tiny Web Application

Page 21: Sinatra Introduction

• simple

• cherry pick

• ActiveRecord / Datamapper

• Prototype / jQuery

Page 22: Sinatra Introduction
Page 23: Sinatra Introduction

追蹤 twitter 上痛恨 IE6 的人

Page 24: Sinatra Introduction
Page 25: Sinatra Introduction

• CodeIgniter - PHP framework

• 使用 Twitter API 追蹤特定關鍵字並展示

• LOC : 1000+

• NOT support CJK

Page 26: Sinatra Introduction

require 'rubygems'require 'sinatra'require 'twitter_search'get '/' do @client = TwitterSearch::Client.new 'politweets' @results = @client.query :q => '@MrIE6' erb :homeend

10 行內收工..

Page 27: Sinatra Introduction

• ORM

• Paginate

Page 28: Sinatra Introduction

多寫了 40 行左右

Page 29: Sinatra Introduction
Page 30: Sinatra Introduction

Rails Metal with Sinatra

Page 31: Sinatra Introduction

Core Code Middleware Mongrel

rack 是一種 middleware

Rack provides a minimal interface between webservers supporting Ruby and Ruby frameworks

Page 32: Sinatra Introduction
Page 33: Sinatra Introduction
Page 34: Sinatra Introduction

http ProfilingMiddleware

SecurityMiddleware

CachingMiddleware

Handlerfor Rack

Core Code

Page 35: Sinatra Introduction

$ rake middleware

Page 36: Sinatra Introduction

Scaling Rails - Metal

• 使用 Rack 製作一個超級快的 action

• 將 Sinatra 當作一個 Metal app 使用

Page 37: Sinatra Introduction

Performance

Page 38: Sinatra Introduction

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 倍

Page 39: Sinatra Introduction

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)

一般狀況

Page 40: Sinatra Introduction

Q & A

Page 41: Sinatra Introduction

Resources

• Scaling Rails : Episode #14 Rack

• RailsCast: Episode #251 Rack Middleware

• Pragprog.com : Classy Web Development with Sinata

• Github / xdite : tweesage-message-wall