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 @default_config ||= {
167 :raise_errors => false,
168 :env => :development,
169 :root => File.dirname($0),
170 :default_static_mime_type => 'text/plain',
171 :default_params => { :format => 'html' }
175 def determine_route(verb, path)
176 routes[verb].eject { |r| r.match(path) } || routes[404]
179 def content_type_for(path)
180 ext = File.extname(path)[1..-1]
181 Sinatra.mime_types[ext] || config[:default_static_mime_type]
184 def serve_static_file(path)
185 path = Sinatra.config[:root] + '/public' + path
188 'Content-Type' => Array(content_type_for(path)),
189 'Content-Length' => Array(File.size(path))
191 [200, headers, File.read(path)]
196 request = Rack::Request.new(env)
198 if found = serve_static_file(request.path_info)
202 response = Rack::Response.new
203 route = determine_route(
204 request.request_method.downcase.to_sym,
207 context = EventContext.new(request, response, route.params)
210 context = handle_with_filters(context, &route.block)
211 context.status ||= route.default_status
214 raise e if config[:raise_errors]
215 route = Sinatra.routes[500]
217 context.body Array(context.instance_eval(&route.block))
222 def define_route(verb, path, &b)
223 routes[verb] << route = Route.new(path, &b)
227 def define_error(code, &b)
228 routes[code] = Error.new(code, &b)
231 def define_filter(type, &b)
238 setup_default_events!
243 def handle_with_filters(cx, &b)
244 caught = catch(:halt) do
245 filters[:before].each { |x| cx.instance_eval(&x) }
248 caught = catch(:halt) do
251 result = caught.to_result(cx) if caught
252 filters[:after].each { |x| cx.instance_eval(&x) }
253 cx.body Array(result.to_s)
259 URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
260 PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
262 Result = Struct.new(:path, :block, :params, :default_status)
264 attr_reader :block, :path
266 def initialize(path, &b)
267 @path, @block = path, b
269 regex = path.to_s.gsub(PARAM) do
270 @param_keys << $1.intern
273 if path =~ /:format$/
274 @pattern = /^#{regex}$/
276 @param_keys << :format
277 @pattern = /^#{regex}(?:\.(#{URI_CHAR}+))?$/
282 return nil unless path =~ @pattern
283 params = @param_keys.zip($~.captures.compact.map(&:from_param)).to_hash
284 Result.new(@path, @block, include_format(params), 200)
287 def include_format(h)
288 h.delete(:format) unless h[:format]
289 Sinatra.config[:default_params].merge(h)
298 def initialize(code, &b)
299 @code, @block = code, b
312 paths.map { |path| Sinatra.define_route(:get, path, &b) }
316 paths.map { |path| Sinatra.define_route(:post, path, &b) }
320 paths.map { |path| Sinatra.define_route(:put, path, &b) }
323 def delete(*paths, &b)
324 paths.map { |path| Sinatra.define_route(:delete, path, &b) }
327 def error(*codes, &b)
328 raise 'You must specify a block to assciate with an error' if b.nil?
329 codes.each { |code| Sinatra.define_error(code, &b) }
333 Sinatra.define_filter(:before, &b)
337 Sinatra.define_filter(:after, &b)
340 def mime_type(content_type, *exts)
341 exts.each { |ext| Sinatra::MIME_TYPES.merge(ext.to_s, content_type) }
345 Sinatra::EventContext.class_eval(&b)
348 def configures(*envs)
349 yield if (envs.include?(Sinatra.config[:env]) ||
351 !Sinatra.config[:reloading]
354 Sinatra.setup_default_events!
358 Sinatra.run if Sinatra.config[:run]