Algorithmic Programmer
Algorithmic Programmer

Reputation: 766

How do you create an API server based on data from an existing Rails/Postgres web application?

I have an existing web application that's developed with Ruby on Rails and PostgreSQL. I need to create a mobile application (and possibly a separate web application) using the data from that web application, so I'm looking to create an API server. Is it possible to do this without altering the source code from the original Rails/Postgres web application?

Any ideas on the best way to do this? Or can someone point me in the right direction on what to research?

Upvotes: 0

Views: 199

Answers (2)

Ti Zhang
Ti Zhang

Reputation: 17

Sounds like essentially you want to have two applications connecting to the same database offering the same methods, but respond in different formats (html vs, for example, json). One way of doing that relatively easily might be pushing another api only Rails app to heroku that connects to the same Postgres database (which was mentioned in the comments), but you would have to figure out how to handle authentication differently for your API end points. This depends on whether you are exposing these end points to the public or to something like a mobile front-end. You may want to switch to token-based authentication if you were formerly using sessions on the web-app. Once you implement secure authenticatoin for your api routes, all you have to do is make sure your methods, instead of rendering erb or haml templates, are returning raw data consumable by your intended client.

Upvotes: 0

jvillian
jvillian

Reputation: 20263

To connect a new application hosted on Heroku to a PostgreSQL database hosted on Heroku just push your new application to Heroku as normal.

Then, under Settings on your new application dashboard, go into Config Variables and add a new config for DATABASE_URL. Put the value of the url for your existing database.

Your new application will need to be under the same account as your existing application. Heroku doesn't allow you to connect across accounts.

You probably want to take a look at this question for additional details.

Upvotes: 1

Related Questions