Before the filter in the sinatra subposition

I have a Rails application and a Sinatra application inside it:

routes.rb:

mount MySinatraApp => "/streaming"

Sinatra:

class MySinatraApp< Sinatra::Base
  ...
  before do
    puts 'hello from sinatra!'
  end
  ...
end

I expect the filter to be triggered only for queries starting with /streaming..., but, what amazes me, the filter is triggered for each request to the entire Rails application. What can I do with this behavior? I can add the regexp filter after before, but I don't think this is a good style.

+5
source share
3 answers

Well, I replaced

mount MySinatraApp => "/streaming"

with

match "/streaming" => SinatraQuoridor, :anchor => false

and the filters started talking as expected. Although I do not understand why this happens: according to this , mount should just call a match with :anchor => false, so the routes should be the same. But the behavior is different.

+2

, , , , , filters . , Sinatra::Namespace sinatra-contrib,

require 'sinatra/namespace'

class App < Sinatra::Base
  register Sinatra::Namespace

  namespace "/" do
    before do
      # this will only run within routes marked "/",
      # which should also be prepended with the mounted route, 
      # so "/streaming/"
    end

    get "/?" do
      # something here
    end

  end

, , , - , "/streaming/" not "/streaming" ( , ), , , .


Update

, URL- . , . , mount, Rack - ? ?

require 'rubygems'
require 'bundler'
Bundler.require

require 'sinatra'
require 'sinatra/namespace'

class StreamingApp < Sinatra::Base
  register Sinatra::Namespace

  namespace "/" do
    before do
      warn "Entering Streaming before filter"
    end

    get "/?" do
      "Hello from the StreamingApp"
    end
  end

  namespace "/something" do
    before do
      warn "Entering Streaming /something before filter"
      warn "request.path_info = #{request.path_info}"
    end
    get "/?" do
      "Hello from StreamingApp something"
    end
  end
end

class OtherApp < Sinatra::Base
  before do
    warn "Entering OtherApp before filter"
    warn "request.path_info = #{request.path_info}"
  end
  get "/" do
    "Hello from the OtherApp"
  end
end

app = Rack::URLMap.new(
  "/streaming" => StreamingApp,
  "/" => OtherApp 
)

run app

:

[2013-01-25 14:19:52] INFO  WEBrick 1.3.1
[2013-01-25 14:19:52] INFO  ruby 1.9.3 (2012-04-20) [x86_64-darwin10.8.0]
[2013-01-25 14:19:52] INFO  WEBrick::HTTPServer#start: pid=78178 port=9292

Entering OtherApp before filter
request.path_info = /
127.0.0.1 - - [25/Jan/2013 14:20:03] "GET / HTTP/1.1" 200 23 0.0201

Entering Streaming before filter
request.path_info = 
127.0.0.1 - - [25/Jan/2013 14:20:11] "GET /streaming HTTP/1.1" 200 27 0.0044

Entering Streaming before filter
request.path_info = /
127.0.0.1 - - [25/Jan/2013 14:20:15] "GET /streaming/ HTTP/1.1" 200 27 0.0016

Entering Streaming before filter
request.path_info = /something
Entering Streaming /something before filter
request.path_info = /something
127.0.0.1 - - [25/Jan/2013 14:20:21] "GET /streaming/something HTTP/1.1" 200 33 0.0018

, StreamingApp Namespace , . , , - .

+1
['/streaming', '/otherlink', '/whatever'].each do |path|
  before path do
    puts 'hello from sinatra!'
  end
end

edit:

If you really want to keep your structure, you can also do the following (but my first answer is by far the best solution):

before do
  if request.path.start_with? '/streaming'
    puts 'hello from sinatra!'
  end
end
0
source

All Articles