I want to serv static files as well as dynamic contents through Rack. Previously, I used WEBrick without using Rack, with code like this, and it worked:
@s = WEBrick::HTTPServer.new(
Port: 3000,
BindAddress: "localhost",
Logger: WEBrick::Log.new(File::NULL),
AccessLog: [nil, nil]
)
%w[INT TERM].each{|signal| trap(signal){@s.shutdown}}
@s.mount("/", self)
@s.mount("/resource/", WEBrick::HTTPServlet::FileHandler, "/")
@s.start
In the above, requests to localhost:3000
will be responded by dynamic contents, and requests to localhost:3000/resource/path_to_file
will be responded by a static file located at /path_to_file
on my computer.
Now, I want to switch to a system using Rack with Thin server. I wrote the following, but could not get the static files served. How should it be changed to make it work?
Rack::Handler::Thin.run(->env{
h = Rack::Utils.parse_nested_query(env["QUERY_STRING"])
# I tried the following three lines one at a time, but neither worked.
use(Rack::Static, urls: "/resource/", root: "/") # First try
Rack::File.new("/resource").call(env) # Second try
Rack::Directory.new("/resource").call(env) # Third try
[200, {}, [some_dyamically_generated_content]]
}, Port: 3000)
I know that ther is a similar question: How to serve static files via Rack?, but I could not make it work. I am not sure how to use Rack::Static
, Rack::File
, or Rack::Directory
. Please teach me.
/public
. Thanks for the help. – Reeducate