NullVoxPopuli
NullVoxPopuli

Reputation: 65183

How to I add my app's docs to be accessed via the app, but not via the public directory? (so authentication is required)

UPDATE:

I'm getting this error:

(No route matches "/docs/index.html"... )

when accessing

admin.example.com/docs/index.html

The file exists.

In my routes.rb file:

map.with_options(:conditions => {:subdomain => 'admin']}) do |subdom|
    subdom.root :controller => 'admin/subscriptions', :action => 'index'
    subdom.with_options(:namespace => 'admin/', :name_prefix => 'admin_', :path_prefix => nil) do |admin|
        admin.connect "/docs/:id", :controller => :docs, :action => :get_file
    end
end

and the docs controller is in app/controllers/admin/

the only method in that controller

def get_file
        path = request.request_uri
        send_file(path)
    end

END UPDATE

So, I have a subdomain, admin.example.com, that I would like to put YARD generated docs behind.

I tried just putting them in the public folder, and modifying the routes.rb file, but the docs were always accessible.

How do I make it so that they are only available once logged in to admin.example.com?

(all the href's in the docs are relative, so, I'm not sure how I'd use controllers, either)

Upvotes: 0

Views: 433

Answers (3)

NullVoxPopuli
NullVoxPopuli

Reputation: 65183

This is what your method should look like:

def get_file
    path = Dir[Rails.root.join("docs", params[:file_or_folder])]
    if path.length > 0
        # check if root directory
        path_str = path[0]
        if path_str =~ /\/docs\/?\z/
            path_str += "/index.html"
        end
        render :file => path_str
    else
        render :text => "File Not Found"
    end

end

And your rails route should look like this:

map.connect "/docs/:file_or_folder", :controller => :docs, :action => :get_file, :file_or_folder => /.*/
map.resources :docs, :only => :get_file

Upvotes: 1

badams
badams

Reputation: 252

Here's a good primer on how you could accomplish protecting files from the public using nginx and send_file.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160321

Don't put them in the public folder.

Put them somewhere else entirely and stream them back to authenticated users only.

One way to do this is to use send_file.

Upvotes: 0

Related Questions