DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Grape API Mounted on RACK with Static Pages

Grape API Mounted on RACK with Static Pages

Daniel Doubrovkine user avatar by
Daniel Doubrovkine
·
Feb. 02, 12 · Interview
Like (0)
Save
Tweet
Share
6.26K Views

Join the DZone community and get the full member experience.

Join For Free

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).

    source "http://rubygems.org"
     
    gem "rack", "1.3.5"
    gem "rack-contrib", :git => "https://github.com/rack/rack-contrib.git", :require => "rack/contrib"
    gem "grape", :git => "http://github.com/intridea/grape.git", :branch => "frontier"

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.

    require 'rubygems'
    require 'bundler/setup'

Lets define our Acme Grape API that will have a system resource that answers ping requests with “pong”. This goes into api/api.rb.

    module Acme
      class API < Grape::API
        version 'v1', :using => :header, :vendor => 'acme', :format => :json    
        resource :system do
          desc "Returns pong."
          get :ping do
            "pong"
          end
        end
      end
    end

The application requires Bundler, all the gems in Gemfile and our API. Lets define it in config/application.rb.

    require File.expand_path('../boot', __FILE__)
     
    Bundler.require :default, ENV['RACK_ENV']
     
    require File.expand_path('../../api/api', __FILE__)



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.

    ENV['RACK_ENV'] ||= :test
     
    require File.expand_path('../application', __FILE__)

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.

    require File.expand_path('../config/environment', __FILE__)
     
    use Rack::TryStatic,
      :root => File.expand_path('../public', __FILE__),
      :urls => %w[/], :try => ['.html', 'index.html', '/index.html']
     
    run Acme::API

Run the application with bundle exec rackup.

image

Full source on Github.

Article Source: http://code.dblock.org/grape-api-mounted-on-rack-w-static-pages


 

 

 

API

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Recent Amazing AI Advancements
  • How to Assess the Technical Skills of a Software Development Partner
  • Creating a Personal ReadMe for Scrum Masters With ChatGPT
  • Chaos Data Engineering Manifesto: 5 Laws for Successful Failures

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: