tl;dr – the source is here
Here’s how I mount a Grape API on Rack and also serve static pages. Most useful for building a service with documentation.
Setup bundler with the required gems. I am using the next version of Grape (frontier branch and the next version of rack-contrib which contains _Rack::TryStatic _that we’re going to want to use).
Static content goes to a new public folder, for example public/index.html.
Our application will need to boot with all these gems, we’ll do it Rails-style by starting with a config/boot.rb file. It brings in Bundler.
Let’s define our Acme Grape API that will have a system resource that answers ping requests with “pong”. _This goes into _api/api.rb.
The application requires Bundler, all the gems in Gemfile and our API. Let’s define it in config/application.rb.
Note the odd File.expand_path construct, borrowed from Rails, - it translates a relative path to the current file into an absolute path, there’re allowing us to run the application from any directory – useful for hosting where you never know who boots the application.
Continuing to borrow from Rails, we will want different environments (development, production, etc.), so it’s a good idea to keep things organized. Setup the environment in _config/environment.rb _and then load the application.
Finally, we need to “rackup” this whole thing in config.ru. We will use Rack::TryStatic to serve static pages when available and pass through to the Acme API otherwise.
Run the application with bundle exec rackup.
Full source on Github.