Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve static files via Rack?

I am currently developing a Rack-based application and want to redirect all file requests (e.g. filename.filetype) to a specified folder.

Rack::Static only supports file requests for a special folder(e.g. "/media").

Do I have to write my own Rack middleware or does an out-of-the-box solution exist?

like image 919
brainfck Avatar asked Feb 15 '10 09:02

brainfck


People also ask

How do you serve a static file?

To serve static files for Go 1.12+ in the standard environment, you define the handlers in your app. yaml file using either the static_dir or static_files elements. The content in the static files or static directories are unaffected by the scaling settings in your app.


1 Answers

To redirect every request to a particular path, use Rack::File (for some reason this class is absent in recent documentation, but it is still part of the latest Rack):

run Rack::File.new("/my/path") 

To redirect every request, and add an HTML index of all files in the target dir, use Rack::Directory:

run Rack::Directory.new("/my/path") 

To combine several directories or serve only a some requests from the target dir:

map "/url/prefix" do   run Rack::File.new("/my/path") end  # More calls to map if necessary...  # All other requests. run MyApp.new 
like image 114
molf Avatar answered Sep 22 '22 13:09

molf