-
-
Save zh/1715858 to your computer and use it in GitHub Desktop.
expanding almost-sinatra.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %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