Giter VIP home page Giter VIP logo

grape-goliath-example's Introduction

Grape + Goliath Example REST API

What is this?

  • Grape is micro-framework for creating REST-like APIs in Ruby.
  • Goliath is a non-blocking Ruby web server

Together you can create a highly scalable API and use the nice features of Grape to specify how your REST API will work.

Getting Started

First take a copy of the project

git clone https://github.com/djones/grape-goliath-example.git
cd grape-goliath-example/

Install dependencies

bundle install

Next create and migrate your database

rake db:setup

Finally start the server and you're done!

ruby server.rb -vs

Now let's list all the posts in the database:

curl http://localhost:9000/v1/posts.json
=> []

A blank array in response tells us there are no posts yet.

Adding a Post

curl -X POST -d '{"post":{"title":"David Jones","body":"this is my message"}}' http://localhost:9000/v1/posts/create

Now list all the posts again

curl http://localhost:9000/v1/posts.json
=> [{"body":"this is my message","created_at":"2012-05-11T13:35:03-07:00","id":1,"title":"David Jones","updated_at":"2012-05-11T13:35:03-07:00"}]

Your first post has now shown up.

Next Steps

This is just a basic Grape API example. You can see the post API specified in app/api/posts.rb. You could expand that API and add your own models in app/models. Grape's readme has some basic usage examples that will help get you started.

Deploy on Heroku

First we create a new Heroku application

heroku create --stack cedar YOURAPPNAME

Next we push the code to Heroku

git push heroku master

Finally we need to migrate the database on Heroku

heroku run rake db:migrate RACK_ENV=production

Now you should be able to request posts from your app and get an empty array back as there are no posts saved yet.

curl http://YOURAPPNAME.herokuapp.com/v1/posts.json
=> []

Next you could use the "Adding a Post" example above to write your first post to the server.

Extras

You can use Rails-like database commands.

Locally, you can:

Drop your database:

rake db:drop

Create your database:

rake db:create

Migrate your database:

rake db:migrate

Create and migrate your database:

rake db:setup

On Heroku, you can:

Migrate your database:

heroku run rake db:migrate RACK_ENV=production

Resources

Todo

  • Unify all the 'require' statements.
  • Make it work with databases other than PostgreSQL.
  • Write some tests using the API.
  • Add some example authentication.
  • Add support for multiple environments e.g. test, development and production.

grape-goliath-example's People

Contributors

djones avatar newlee avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.