Skip to content

Instantly share code, notes, and snippets.

@zh
Forked from udzura/almost-sinatra.rb
Created February 1, 2012 07:54
Show Gist options
  • Select an option

  • Save zh/1715858 to your computer and use it in GitHub Desktop.

Select an option

Save zh/1715858 to your computer and use it in GitHub Desktop.
expanding almost-sinatra.rb
%w(rack tilt backports).map do |lib|
require lib
end
%w(INT TERM).map do |sig|
trap(sig){ $r.stop }
end
Sinatra = Module.new {
extend Rack
a = (Application = Builder.new)
D = Object.method(:define_method)
S = /@@ *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m
$p = 4567
q = a
%w[get post put delete].map do |meth|
D.call(meth) do |path, &block|
a.map(path) do
response_app = lambda do |env|
[
200,
{"Content-Type"=>"text/html"},
[a.instance_eval(&block)]
]
end
run(response_app)
end
end
end
Tilt.mappings.map do |k,v|
D.call(k) do |n, *o|
h = {}
app_file = File.read(caller[0][/^[^:]+/])
app_file.scan(S){|a, b| h[a] = b }
$t ||= h
template_engine = v.first
template_engine.new(*o){
n.to_s == n ? n : $t[n.to_s]
}.render(
a,
(o[0].try(:[],:locals) || {})
)
end
end
%w[set enable disable configure helpers use register].each do |m|
D.(m){|*_, &b| b.try :call }
end
END {
Handler.get("webrick").run(a, :Port => $p){|s| $r = s }
}
%w[params session].each do |m|
D.(m){ q.send m }
end
a.use Session::Cookie
a.use Lock
D.(:before){|&b| a.use Rack::Config, &b }
before do |e|
q = Request.new e
q.params.dup.map{|k, v| params[k.to_sym] = v }
end
}
$n = Sinatra
puts "== almost #$n/No Version has taken the stage on #$p for development with backup from Webrick"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment