0

Rack (on rails) question:

Given I am loading a submodule into my Rails app, And I want to serve static files from submodule's {folder_a, folder_b}

I cannot move the submodule's folder into /public, because I only want to serve some of the files from the submodule (and not all)

How do I configure Rack to do that?

My structure looks like this:

config.ru
submodule/public/folder_a/
submodule/public/folder_b/
public/

And I want the following mapping

submodule/public/folder_a => http://localhost:3000/folder_a
submodule/public/folder_b => http://localhost:3000/folder_b

What I tried so far:

use Rack::Static, :urls => ["/folder_a", "folder_b"], :root => "submodule/public"

That did not work, just no response on /folder_a from my server. This idea was partly taken from the (old) question How to serve static files via Rack?, but it seems to suggest old resolutions which fails (currently my project is using Rack 1.5.2).

I also tried just symlinking my desired folders into /public, which works. But I am sure it will be prettier and more elegant to use Rack directly

Any suggestions?

Community
  • 1
  • 1
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
  • Can't provide full help, but I think I remember using `Rack::Directory` for such a purpose. Did you had a look at it? – Benjamin Bouchet Feb 19 '15 at 07:34
  • @BenjaminSinclaire, yeah, I found an example but it turned out to be outdated. Could not get it to work. Was it failing with `Rack::Directory.new` does not exist?? – Jesper Rønn-Jensen Feb 19 '15 at 07:43

1 Answers1

2

It looks like the working directory might be messed with depending on how your Rails application is launched.

I got it working inside a Rails applicaiton with the following config.ru:

# This file is used by Rack-based servers to start the application.

use Rack::Static, :urls => ["/folder_a", "/folder_b"], :root => ::File.expand_path("submodule/public")

require ::File.expand_path('../config/environment', __FILE__)
run Rails.application

Then in a terminal:

$ rails server -d
=> Booting WEBrick
=> Rails 4.2.0 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options

$ curl http://0.0.0.0:3000/folder_a/index.html
A

$ curl http://0.0.0.0:3000/folder_b/index.html
B
Jakob S
  • 19,575
  • 3
  • 40
  • 38
  • looks good, seems odd though that you have to do the File.expand_path. Main difference from my (failing) example: The preprended slash in `"/folder_a"` – Jesper Rønn-Jensen Feb 19 '15 at 12:54