rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / Atom feed
* can I use Rack::Directory inside Rack::Builder block?
@ 2010-05-24  7:18 Stephen Bannasch
  2010-05-24 17:56 ` Stephen Bannasch
  0 siblings, 1 reply; 5+ messages in thread
From: Stephen Bannasch @ 2010-05-24  7:18 UTC (permalink / raw)
  To: rack-devel

I want to emulate a static web server with directory indexes using rack.

Is there a way to use Rack::Directory inside a Rack::Builder block?

This works fine serving static files:

   myapp = Rack::Builder.new do
     map "/" do
       use Rack::Static, :urls => ["/"], :root => 'cache'
       run lambda {|env| [404, {}, '']}
     end
   end

   Rack::Handler::WEBrick.run(myapp, :Port => 4321)

But when I add Rack::Directory

   myapp = Rack::Builder.new do
     map "/" do
       use Rack::Static, :urls => ["/"], :root => 'cache'
       use Rack::Directory, 'cache'
       run lambda {|env| [404, {}, '']}
     end
   end

   Rack::Handler::WEBrick.run(myapp, :Port => 4321)

I'm getting this error:

   rack-1.1.0/lib/rack/directory.rb:46:in `initialize': can't convert Proc into String (TypeError)
           from ./rack-1.1.0/lib/rack/builder.rb:54:in `new'
           from ./rack-1.1.0/lib/rack/builder.rb:54:in `use'
           from ./rack-1.1.0/lib/rack/builder.rb:73:in `call'
           from ./rack-1.1.0/lib/rack/builder.rb:73:in `to_app'
           from ./rack-1.1.0/lib/rack/builder.rb:73:in `each'
           from ./rack-1.1.0/lib/rack/builder.rb:73:in `inject'
           from ./rack-1.1.0/lib/rack/builder.rb:73:in `to_app'
           from ./rack-1.1.0/lib/rack/builder.rb:63:in `map'
           from ./rack-1.1.0/lib/rack/builder.rb:66:in `map'

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: can I use Rack::Directory inside Rack::Builder block?
  2010-05-24  7:18 can I use Rack::Directory inside Rack::Builder block? Stephen Bannasch
@ 2010-05-24 17:56 ` Stephen Bannasch
  0 siblings, 0 replies; 5+ messages in thread
From: Stephen Bannasch @ 2010-05-24 17:56 UTC (permalink / raw)
  To: rack-devel

At 3:18 AM -0400 5/24/10, Stephen Bannasch wrote:
>I want to emulate a static web server with directory indexes using rack.
>
>Is there a way to use Rack::Directory inside a Rack::Builder block?




The problem was that Rack::Directory#initialize:
  http://github.com/chneukirchen/rack/blob/master/lib/rack/directory.rb#L45

expects arguments in a different order than Rack::Builder provides:
  http://github.com/chneukirchen/rack/blob/master/lib/rack/builder.rb#L53


Here's what Rack::Directory#initialize looks like:

  module Rack
    class Directory
      def initialize(root, app=nil)
        @root = F.expand_path(root)
        @app = app || Rack::File.new(@root)
      end
    end
  end


While Rack::Builder#use calls middleware like this:

  middleware.new(app, *args, &block)

Here's my simple fix:

  module Rack
    class MyDirectory < Rack::Directory
      def initialize app, root
        super(root, app)
      end
    end
  end


  myapp = Rack::Builder.new do
    map "/" do
      use Rack::MyDirectory, 'cache'
      use Rack::Static, :urls => ["/"], :root => 'cache'
      run lambda {|env| [404, {}, '']}
    end
  end

Should the middleware included with rack work with Rack::Builder#use ??

There don't seem to be tests for this behavior.

Would these tests be useful?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: can I use Rack::Directory inside Rack::Builder block?
       [not found] ` <p06240801c8206c949189@63.138.152.134>
@ 2010-05-24 20:16   ` Magnus Holm
  2010-05-25  0:42     ` Stephen Bannasch
  0 siblings, 1 reply; 5+ messages in thread
From: Magnus Holm @ 2010-05-24 20:16 UTC (permalink / raw)
  To: rack-devel

Rack::Directory is an endpoint, not a middleware:

map "/cache" do
  run Rack::Directory.new("cache")
end

// Magnus Holm



On Mon, May 24, 2010 at 19:56, Stephen Bannasch
<stephen.bannasch@deanbrook.org> wrote:
> At 3:18 AM -0400 5/24/10, Stephen Bannasch wrote:
>>I want to emulate a static web server with directory indexes using rack.
>>
>>Is there a way to use Rack::Directory inside a Rack::Builder block?
>
>
>
>
> The problem was that Rack::Directory#initialize:
>  http://github.com/chneukirchen/rack/blob/master/lib/rack/directory.rb#L45
>
> expects arguments in a different order than Rack::Builder provides:
>  http://github.com/chneukirchen/rack/blob/master/lib/rack/builder.rb#L53
>
>
> Here's what Rack::Directory#initialize looks like:
>
>  module Rack
>    class Directory
>      def initialize(root, app=nil)
>        @root = F.expand_path(root)
>        @app = app || Rack::File.new(@root)
>      end
>    end
>  end
>
>
> While Rack::Builder#use calls middleware like this:
>
>  middleware.new(app, *args, &block)
>
> Here's my simple fix:
>
>  module Rack
>    class MyDirectory < Rack::Directory
>      def initialize app, root
>        super(root, app)
>      end
>    end
>  end
>
>
>  myapp = Rack::Builder.new do
>    map "/" do
>      use Rack::MyDirectory, 'cache'
>      use Rack::Static, :urls => ["/"], :root => 'cache'
>      run lambda {|env| [404, {}, '']}
>    end
>  end
>
> Should the middleware included with rack work with Rack::Builder#use ??
>
> There don't seem to be tests for this behavior.
>
> Would these tests be useful?
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: can I use Rack::Directory inside Rack::Builder block?
  2010-05-24 20:16   ` Magnus Holm
@ 2010-05-25  0:42     ` Stephen Bannasch
  2010-05-25  1:14       ` James Tucker
  0 siblings, 1 reply; 5+ messages in thread
From: Stephen Bannasch @ 2010-05-25  0:42 UTC (permalink / raw)
  To: rack-devel

At 10:16 PM +0200 5/24/10, Magnus Holm wrote:
>Rack::Directory is an endpoint, not a middleware:
>
>map "/cache" do
>  run Rack::Directory.new("cache")
>end
>
>// Magnus Holm

Thanks Magnus,

That helped a bunch -- made what I wanted to do much simpler and helped me think more clearly about what's going on.

Looking at the source code it seems Rack::Directory can be used as an endpoint (passing files to Rack::File) or call a custom app when a file is found -- but it doesn't follow middleware calling conventions.

The rdoc, http://rdoc.info/projects/chneukirchen/rack doesn't make it clear what is middleware and what's an endpoint -- probably only helpful in the first couple of hours playing with it however.

I'm working on a rack middleware that implements a Java web start jnlp server.

Looks like this so far: http://gist.github.com/412606

This is what the Rack::Builder block simplified to:

  jnlp_app = Rack::Builder.new do
    map "/" do
      use Rack::Jnlp
      run Rack::Directory.new(jnlp_cache)
    end
  end

  Rack::Handler::WEBrick.run(jnlp_app, :Port => 4321)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: can I use Rack::Directory inside Rack::Builder block?
  2010-05-25  0:42     ` Stephen Bannasch
@ 2010-05-25  1:14       ` James Tucker
  0 siblings, 0 replies; 5+ messages in thread
From: James Tucker @ 2010-05-25  1:14 UTC (permalink / raw)
  To: rack-devel


On 24 May 2010, at 21:42, Stephen Bannasch wrote:
> The rdoc, http://rdoc.info/projects/chneukirchen/rack doesn't make it clear what is middleware and what's an endpoint -- probably only helpful in the first couple of hours playing with it however.

At minimum, you probably want this one: http://rdoc.info/projects/rack/rack

Doc patches are always welcome :-)

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-05-25  1:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-05-24  7:18 can I use Rack::Directory inside Rack::Builder block? Stephen Bannasch
2010-05-24 17:56 ` Stephen Bannasch
     [not found] <p06240801c81fd6be2bb9@192.168.1.106>
     [not found] ` <p06240801c8206c949189@63.138.152.134>
2010-05-24 20:16   ` Magnus Holm
2010-05-25  0:42     ` Stephen Bannasch
2010-05-25  1:14       ` James Tucker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).