谢谢leucos!!!
我也想过一个 Rack 中间件,但想知道是否没有更“ramaze”的解决方案(比如先天)。
无论如何,这很有效,而且很棒!
这是您的代码的修改版本:
require 'coffee-script'
# An attempt to allow Ramaze to generate Js file from coffee files
module Rack
class BrewedCoffee
def initialize(app, options = {})
@app = app
@lookup_path = options[:lookup_path].nil? ? [__DIR__] : options[:lookup_path]
end
def call(env)
name = env['PATH_INFO']
# Continue processing if requested file is not js
return @app.call(env) if File.extname(name) != '.js'
coffee_name = "#{name[0...-3]}.coffee"
@lookup_path.each do |p|
coffee_file = File.join(p, coffee_name)
next unless File.file?(coffee_file)
response_body = CoffeeScript.compile(File.read(coffee_file))
headers = {}
headers["Content-Type"] = 'application/javascript'
headers["Content-Length"] = response_body.length.to_s
return [200, headers, [response_body]]
end
@app.call(env)
end
end
end
我清理了一些东西并删除了 Ramaze 依赖项,这样它就是一个纯 Rack 中间件:)。
您可以使用它调用:
m.use Rack::BrewedCoffee, :lookup_path => Ramaze.options.roots
再见,安德烈亚斯