---
title: "Grape和Sinatra结合使用"
author: "Goldice"
date: "2015-07-23T23:36:55Z"
tags: []
categories: ["编程语言"]
slug: "grape-sinatra"
draft: false
lastmod: "2015-07-23T23:36:55Z"
id: 2229793
url: "https://jdoc.iteye.com/blog/2229793"
---

### Grape && Sinatra

Grape([https://github.com/intridea/grape](https://github.com/intridea/grape)) is a REST-like API micro-framework for Ruby

Sinatra([http://www.sinatrarb.com/intro.html](http://www.sinatrarb.com/intro.html)) is a DSL for quickly creating web applications in Ruby

可见，Grape适合构建纯Api系统，而Sinatra适合搭建web应用。

### 那么问题来了

如何将Grape和Sinatra融合到一起，取两者之长？

### 解决方案

```
# content.rb

class API < Grape::Api
  get '/' do
    'lets go drinking!' 
  end
end

class Web < Sinatra::Base
  get '/' do 
    'Lets go dancing!'
  end
end

# config.ru

map '/' do
  run API
end

map '/web' do 
  run Web
end
```

参考文档：[http://stackoverflow.com/questions/15102293/sinatra-and-grape-api-together](http://stackoverflow.com/questions/15102293/sinatra-and-grape-api-together)