Extend Middleman Server

I want to extend Middleman::Application to serve a new Rack::Builder instance.
I tried the same approach used by Middleman::MetaPages:

server = ::Middleman::Application.server
rack_app = Rack::Builder.new do
  map '/foo' do
    [200, { 'Content-Type' => 'text/html' }, "That is a foo page."]       
  end
end
server.use '/foo_root', rack_app

Running middleman runs without exceptions, but my foo page isn’t reachable. Got 404 instead. Suggestions#any? :smile:

You’ll want to use the existing Middleman application instance instead, i.e. the app you receive when initializing an extension or just self in config.rb:

foo_app = Rack::Builder.new do
  map '/foo' do
    run ->(_env) { [200, { 'Content-Type' => 'text/plain' }, ['Hello']] }
  end
end

map '/foo_root' do
  run foo_app
end
1 Like