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/halt_results'
17 old_verbose, $VERBOSE = $VERBOSE, nil
20 $VERBOSE = old_verbose
35 map { |k,v| "#{k}=#{URI.escape(v)}" }.join('&')
39 self.inject({}) { |h,(k,v)| h[k.to_sym] = v; h }
43 reject { |k,v| !keys.include?(k) }
49 Proc.new { |*args| args.shift.__send__(self, *args) }
55 self.inject({}) { |h, (k, v)| h[k] = v; h }
59 Proc.new { |*args| args.shift.__send__(self[0], args + self[1..-1]) }
71 find { |e| result = block[e] and break result }
81 puts "== Sinatra has taken the stage on port #{Sinatra.config[:port]} for #{Sinatra.config[:env]}"
83 Rack::Handler::Mongrel.run(Sinatra, :Port => Sinatra.config[:port]) do |server|
86 puts "\n== Sinatra has ended his set (crowd applauds)"
89 rescue Errno::EADDRINUSE => e
90 puts "== Someone is already performing on port #{Sinatra.config[:port]}!"
97 attr_reader :request, :response, :route_params
99 def initialize(request, response, route_params)
100 @request, @response, @route_params =
101 request, response, route_params
105 @params ||= request.params.merge(route_params).symbolize_keys
109 self.instance_eval(&b)
112 # redirect to another url It can be like /foo/bar
113 # for redirecting within your same app. Or it can
114 # be a fully qualified url to another site.
116 puts "Redirecting to: #{url}"
118 headers.merge!({'Location'=> url})
122 def method_missing(name, *args)
123 if args.size == 1 && response.respond_to?("#{name}=")
124 response.send("#{name}=", args.first)
126 response.send(name, *args)
131 def setup_default_events!
133 "<h2>#{$!.message}</h2>#{$!.backtrace.join("<br/>")}"
142 @request_types ||= [:get, :put, :post, :delete]
146 @routes ||= Hash.new do |hash, key|
147 hash[key] = [] if request_types.include?(key)
152 @filters ||= Hash.new { |hash, key| hash[key] = [] }
156 @config ||= default_config.dup
164 config[:env] == :development
168 @default_config ||= {
171 :raise_errors => false,
172 :env => :development,
173 :root => File.dirname($0),
174 :default_static_mime_type => 'text/plain',
175 :default_params => { :format => 'html' }
179 def determine_route(verb, path)
180 routes[verb].eject { |r| r.match(path) } || routes[404]
183 def content_type_for(path)
184 ext = File.extname(path)[1..-1]
185 Sinatra.mime_types[ext] || config[:default_static_mime_type]
188 def serve_static_file(path)
189 path = Sinatra.config[:root] + '/public' + path
192 'Content-Type' => Array(content_type_for(path)),
193 'Content-Length' => Array(File.size(path))
195 [200, headers, File.read(path)]
201 reload! if Sinatra.development?
203 request = Rack::Request.new(env)
205 if found = serve_static_file(request.path_info)
209 response = Rack::Response.new
210 route = determine_route(
211 request.request_method.downcase.to_sym,
214 context = EventContext.new(request, response, route.params)
217 context = handle_with_filters(context, &route.block)
218 context.status ||= route.default_status
221 raise e if config[:raise_errors]
222 route = Sinatra.routes[500]
224 context.body Array(context.instance_eval(&route.block))
229 def define_route(verb, path, &b)
230 routes[verb] << route = Route.new(path, &b)
234 def define_error(code, &b)
235 routes[code] = Error.new(code, &b)
238 def define_filter(type, &b)
245 setup_default_events!
249 config[:reloading] = true
252 config[:reloading] = false
257 def handle_with_filters(cx, &b)
258 caught = catch(:halt) do
259 filters[:before].each { |x| cx.instance_eval(&x) }
262 caught = catch(:halt) do
265 result = caught.to_result(cx) if caught
266 filters[:after].each { |x| cx.instance_eval(&x) }
267 cx.body Array(result.to_s)
273 URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
274 PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
276 Result = Struct.new(:path, :block, :params, :default_status)
278 attr_reader :block, :path
280 def initialize(path, &b)
281 @path, @block = path, b
283 regex = path.to_s.gsub(PARAM) do
284 @param_keys << $1.intern
287 if path =~ /:format$/
288 @pattern = /^#{regex}$/
290 @param_keys << :format
291 @pattern = /^#{regex}(?:\.(#{URI_CHAR}+))?$/
296 return nil unless path =~ @pattern
297 params = @param_keys.zip($~.captures.compact.map(&:from_param)).to_hash
298 Result.new(@path, @block, include_format(params), 200)
301 def include_format(h)
302 h.delete(:format) unless h[:format]
303 Sinatra.config[:default_params].merge(h)
312 def initialize(code, &b)
313 @code, @block = code, b
326 paths.map { |path| Sinatra.define_route(:get, path, &b) }
330 paths.map { |path| Sinatra.define_route(:post, path, &b) }
334 paths.map { |path| Sinatra.define_route(:put, path, &b) }
337 def delete(*paths, &b)
338 paths.map { |path| Sinatra.define_route(:delete, path, &b) }
341 def error(*codes, &b)
342 raise 'You must specify a block to assciate with an error' if b.nil?
343 codes.each { |code| Sinatra.define_error(code, &b) }
347 Sinatra.define_filter(:before, &b)
351 Sinatra.define_filter(:after, &b)
354 def mime_type(content_type, *exts)
355 exts.each { |ext| Sinatra::MIME_TYPES.merge(ext.to_s, content_type) }
359 Sinatra::EventContext.class_eval(&b)
362 def configures(*envs)
363 yield if (envs.include?(Sinatra.config[:env]) ||
365 !Sinatra.config[:reloading]
368 Sinatra.setup_default_events!
372 Sinatra.run if Sinatra.config[:run]