4 require 'swiftcore/swiftiplied_mongrel'
5 puts "Using Swiftiplied Mongrel"
7 require 'swiftcore/evented_mongrel'
8 puts "Using Evented Mongrel"
13 require 'sinatra/mime_types'
14 require 'sinatra/send_file_mixin'
15 require 'sinatra/halt_results'
18 old_verbose, $VERBOSE = $VERBOSE, nil
21 $VERBOSE = old_verbose
36 map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
40 self.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
44 reject { |k,v| !keys.include?(k) }
50 Proc.new { |*args| args.shift.__send__(self, *args) }
56 self.inject({}) { |h, (k, v)| h[k] = v; h }
60 Proc.new { |*args| args.shift.__send__(self[0], args + self[1..-1]) }
72 find { |e| result = block[e] and break result }
82 puts "== Sinatra has taken the stage on port #{Sinatra.config[:port]} for #{Sinatra.config[:env]}"
84 Rack::Handler::Mongrel.run(Sinatra, :Port => Sinatra.config[:port]) do |server|
87 puts "\n== Sinatra has ended his set (crowd applauds)"
90 rescue Errno::EADDRINUSE => e
91 puts "== Someone is already performing on port #{Sinatra.config[:port]}!"
96 EventContext = Struct.new(:request, :response, :route_params) do
98 @params ||= request.params.merge(route_params).symbolize_keys
102 self.instance_eval(&b)
105 def method_missing(name, *args)
106 if args.size == 1 && response.respond_to?("#{name}=")
107 response.send("#{name}=", args.first)
109 response.send(name, *args)
114 def setup_default_events!
116 "<h2>#{$!.message}</h2>#{$!.backtrace.join("<br/>")}"
125 @request_types ||= [:get, :put, :post, :delete]
129 @routes ||= Hash.new do |hash, key|
130 hash[key] = [] if request_types.include?(key)
135 @filters ||= Hash.new { |hash, key| hash[key] = [] }
139 @config ||= default_config.dup
147 @default_config ||= {
150 :raise_errors => false,
151 :env => :development,
152 :root => File.dirname($0),
153 :default_static_mime_type => 'text/plain',
154 :default_params => { :format => 'html' }
158 def determine_route(verb, path)
159 routes[verb].eject { |r| r.match(path) } || routes[404]
162 def content_type_for(path)
163 ext = File.extname(path)[1..-1]
164 Sinatra.mime_types[ext] || config[:default_static_mime_type]
167 def serve_static_file(path)
168 path = Sinatra.config[:root] + '/public' + path
171 'Content-Type' => Array(content_type_for(path)),
172 'Content-Length' => Array(File.size(path))
174 [200, headers, File.read(path)]
179 request = Rack::Request.new(env)
181 if found = serve_static_file(request.path_info)
185 response = Rack::Response.new
186 route = determine_route(
187 request.request_method.downcase.to_sym,
190 context = EventContext.new(request, response, route.params)
193 context = handle_with_filters(context, &route.block)
194 context.status ||= route.default_status
197 raise e if config[:raise_errors]
198 route = Sinatra.routes[500]
200 context.body Array(context.instance_eval(&route.block))
205 def define_route(verb, path, &b)
206 routes[verb] << route = Route.new(path, &b)
210 def define_error(code, &b)
211 routes[code] = Error.new(code, &b)
214 def define_filter(type, &b)
221 setup_default_events!
226 def handle_with_filters(cx, &b)
227 caught = catch(:halt) do
228 filters[:before].each { |x| cx.instance_eval(&x) }
231 caught = catch(:halt) do
234 result = caught.to_result(cx) if caught
235 filters[:after].each { |x| cx.instance_eval(&x) }
236 cx.body Array(result.to_s)
242 URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
243 PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
245 Result = Struct.new(:path, :block, :params, :default_status)
247 attr_reader :block, :path
249 def initialize(path, &b)
250 @path, @block = path, b
252 regex = path.to_s.gsub(PARAM) do
253 @param_keys << $1.intern
256 if path =~ /:format$/
257 @pattern = /^#{regex}$/
259 @param_keys << :format
260 @pattern = /^#{regex}(?:\.(#{URI_CHAR}+))?$/
265 return nil unless path =~ @pattern
266 params = @param_keys.zip($~.captures.compact.map(&:from_param)).to_hash
267 Result.new(@path, @block, include_format(params), 200)
270 def include_format(h)
271 h.delete(:format) unless h[:format]
272 Sinatra.config[:default_params].merge(h)
281 def initialize(code, &b)
282 @code, @block = code, b
295 paths.map { |path| Sinatra.define_route(:get, path, &b) }
299 paths.map { |path| Sinatra.define_route(:post, path, &b) }
303 paths.map { |path| Sinatra.define_route(:put, path, &b) }
306 def delete(*paths, &b)
307 paths.map { |path| Sinatra.define_route(:delete, path, &b) }
310 def error(*codes, &b)
311 raise 'You must specify a block to assciate with an error' if b.nil?
312 codes.each { |code| Sinatra.define_error(code, &b) }
316 Sinatra.define_filter(:before, &b)
320 Sinatra.define_filter(:after, &b)
323 def mime_type(content_type, *exts)
324 exts.each { |ext| Sinatra::MIME_TYPES.merge(ext.to_s, content_type) }
328 Sinatra::EventContext.class_eval(&b)
331 def configures(*envs)
332 yield if (envs.include?(Sinatra.config[:env]) ||
334 !Sinatra.config[:reloading]
337 Sinatra.setup_default_events!
339 Sinatra::EventContext.send :include, Sinatra::SendFileMixin
343 Sinatra.run if Sinatra.config[:run]