Using same facebook apikey for heroku production and local development - ruby-on-rails

I would like to test my rails app on my local machine and also have it functional on heroku. However, if I specify my IP address for the "Website" field on the facebook app settings, then my heroku breaks and vice versa. Is there any way to have them both work using the same API Key?
If not, how do I tell Omniauth to use one api key for the development environment and another for the production environment? Thanks!

Use a separate FB app for dev (local) and for production (Heroku).
Read the key out of the environment like:
ENV["FACEBOOK_APP_ID"]
ENV["FACEBOOK_SECRET"]
Then set the key/creds in your config on Heroku using heroku config:add.
Locally use foreman to run your app and set the dev key/creds in a .env file. http://blog.daviddollar.org/2011/05/06/introducing-foreman.html
Keep in mind that FB requires you to use SSL so you'll need to setup something locally that can handle SSL requests.

You will either have to create another development Facebook application for development, which is what I do, or you will have to create an entry in your /etc/hosts file that points the hostname of your Heroku app to you local machine.

Related

How to run rails applications on digitalocean using "rails s" (make it a development environment)

My issue is that I created a droplet to develop Rails apps in digitalocean .
I used the one-click rails droplet. And now I want to create more rails apps than the default rails app in this droplet.
The issue here is that it comes installed with nginx/unicorn .. And they're always on with path of default rails project in their config files.
Now let's assume I created another rails app(file) and I want to run it using "rails s" instead of default rails app that is created by the droplet. How can I do it?
Note: I don't want to change the file path in configs each time I decide to try another app
PS: I tried stopping the service of unicorn/nginx one at a time and both of them in the same time to use "rail s" to run the app .. But it didn't work .. Web pages were not loading
I know it might be a question of a rookie. But I'm kinda new to these stuff and I'd appreciate it if anyone could help me.
If you run it with rails s on the server, chances are it will be running with Puma, or if you're on an older version of Rails, Webrick. Unicorn is not involved in that case because Rails is using its own default web server. If you see that 'rails s' is not running in the right environment, it may be because RAILS_ENV is being set in your shell profile. You can override that by doing:
RAILS_ENV=development rails s
To launch your console.
That being said, rails s runs on localhost:3000 by default - and in the case you described it would be running on DigitalOcean's localhost - not yours. In order to get to it from your local machine, you would need to set up some sort of reverse proxy to allow connections to DO to get served from localhost. This is what nginx is doing for you - it's facilitating a reverse proxy.
If you want do use your DO server as a development machine for a second rails app you have, you're going to have to create that new rails app on the server, then create the reverse proxy settings in nginx to direct to it, then finally create the unicorn settings to serve it. This is an uncommon way of developing though. I recommend using your local machine to develop, and setting up Capistrano or some other deploy tool to deploy it to DO instead. You'd still need to add the settings in nginx/unicorn for the second app, but it will save you headache down the road.

Should the database.yml be configured when testing locally for Heroku with foreman?

Can someone explain to me what they do when they initialise a rails app locally with foreman (part of Heroku toolbelt) (using postgreSQL), destined to run on Heroku?
I'm going by this guide: developing locally with foreman and what I don't understand is if we are expected to specify database username and passwords or if foreman is supposed to handle it as Heroku itself does?
This perplexes me a little as if we are supposed to modify the database.yml to hook it up to postgreSQL, then what is the point of using foreman instead of rails server?
If it does handle it, how does it handle it, and how would I configure my pg_hba.conf to respect it? Something like local all myuser trust?
Yes, database.yml needs to be configured with valid information for your development and test databases.
Foreman is only running what's in your Procfile, not ripping things out and plugging different things in like Heroku does.
So why do you want to use Foreman instead of rails server? Because it:
Runs all roles defined in your Procfile with one command
Automatically loads your .env
Will fail if any of your roles fail (so less scratching your head because some necessary backend service isn't running)

How do I work on my Rails app in development mode, but push it in production mode?

Simple question, I do all my work on my local at home computer and then I git push to my repo on github, and then on my Ubuntu Server I do a git pull to get the content, but this causes inconsistencies because it pulls in development mode which I always have to go in and change. Is there a way to make it so I can work in development mode but push it to the repo production mode? Thanks.
This isn't a git problem. Rails environments ('production', 'development', 'test') affect how the code runs, but the code isn't changed.
When you run rails server on your development machine, the application starts in development mode because that is the default for that command.
You probably aren't using the same command to host your site on WEBrick on your server, but rather using something like Phusion Passenger, which can (should) be configured to boot your application in Production mode.
If the environment is set correctly, the config/environments/#[RAILS_ENV}.rb file is correctly selected upon loading the app, and the correct database sources are selected from config/database.yml.
Rails apps default to development unless you set the RAILS_ENV variable to production. Git doesn't really factor in to this. On your production machine, you'll want to set RAILS_ENV.
Where to do this depends on your production deployment environment, and how your server is configured. Things like Passenger default to production mode, and configuring that depends on your server (Apache, nginx). If you're manually starting the server from the command line (via rails s thin or something along those lines), then you can configure it in your shell's startup file (.profile or something similar, depending on your shell). thin also optionally takes an environment argument like -e production.
If you provide more details on how your production environment works, you might get a more specific answer for where to set RAILS_ENV.

How to migrate heroku rails app to the same app on new host?

I need to switch my rails app from Heroku to my new host on Linode.
I need to address the following concerns and would appreciate some advice
1) How to migrate the data from Heroku to my new Linode host?
2) How to redirect the Heroku domain from myapp.herokuapp.com to my_new_host.com ?
I need to achieve the above as simultaneously as possible so that new data is not lost
Assuming you're running PostGres on your Linode host then you will simply need to backup your on Heroku and then restore it into Heroku (pgbackups). There are other options - You could use heroku db:pull to pull it down from Heroku into your local database and then transfer than to Linode. Another option would to be use the Heroku gem on your linode server to pull the data directly from the heroku app into the linode database.
You can only redirect on Heroku using code, so you would have to push up an app that does a redirect (rack_rewrite) to your new host.
You'll want to put your application on Heroku into maintenance mode on Heroku. Perform the data transfer via which ever mechanism you choose. Then deploy the 'redirect' code to the Heroku app and then turn maintenance mode off on Heroku. Visitors to the herokuapp.com address will then be redirected to the application running on the new URL wherever that may be.

Is it possible to connect single Heroku-RedisToGo from 2 different Heroku instances?

I have one Rails instance and another Node.js instance. The plan is to publish information from the Rails project using Redis's pubsub and subscribe by the Node.js program. The node.js program will publish the information to other clients through web sockets (or long polls).
The whole thing is working perfectly while deployed in combination of remote linux machines. On Heroku, Redis To Go is added as an add-on with the Rails instance. Redis can be connected and working fine from this instance.
But I am not getting how to reach to this Redis add-on from the Node.js instance. I've added REDISTOGO_URL from the environment of Rails instance to the environment of Node.js, but it's not working. Sample errors I'm getting (from logs and REPL) are
redis.set("a","b");
==> Error: Connection in pub/sub mode, only pub/sub commands may be used
redis.psubscribe('*');
==> false
Can somebody please show me a way how to accomplish this?
Yes, all you have to do is set the heroku config value similar to:
'heroku config:add REDISTOGO_URL=[your redis togo url]'
Heroku will then add your config var and restart your app.
You can check to make sure it's added correctly by doing a 'heroku config' on both repositories and comparing them.

Resources