djones/grape-goliath-example(opens in a new tab)
Language: Ruby
A non-blocking REST API example application using Grape and Goliath together with a Postgres database.
- stars
- 152
- watchers
- 7
- forks
- 32
- open issues
- 4
- Last updated
- 05/20/2014
Language: Ruby
A non-blocking REST API example application using Grape and Goliath together with a Postgres database.
Together you can create a highly scalable API and use the nice features of Grape to specify how your REST API will work.
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.
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.
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.
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.
You can use Rails-like database commands.
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
Migrate your database:
heroku run rake db:migrate RACK_ENV=production