What is the simplest way to run locally a Ruby CGI app
I am looking for a very simple e.g. 5 lines of code if possible way without using external servers such as NginX and Apache etc
EDIT:
To be more precise:
Given a simple Ruby script I would like to serve it as CGI, either by requiring a Gem within it or by using another tiny .rb script.
By serve as CGI I mean to be able to interact with it using my web browser
$ gem install cgiup
$ cgiup ruby_cgi_script.rb
If you're not married to a particular webserver and don't need a ton of volume, you can set up and run Sinatra with its standalone Webrick server in about five lines of code.
CGI proper will require more setup, however the Lighttpd web server is relatively simple to configure for Ruby CGI. The only things you have to add to lighttpd.conf are:
server.modules += ( "mod_cgi" )
cgi.assign = (
".rb" => "/usr/local/bin/ruby" # or whatever your path to Ruby is
)
Related
I'm currently writing a couple Twitter bots for my friends using the Twitter gem for Ruby. My plan was to store the keys for them in a .txt file with the rest of the bot's code on my server, but everything I've read has said the keys shouldn't be readable within the code. Is this secure enough, and if not what would be a good solution? Thanks!
A common approach is to save the environment variables into a file called .env that is ignored by version control (and therefore won't be included on Github) but read by the code. One gem to help with this is dotenv.
add .env to the .gitignore file.
create a local .env file with all your env vars
require 'dotenv' and put Dotenv.load somewhere at the beginning of your script. In Rails, the require is unnecessary and you can place the load call in any file in the config/initializers folder
Check that your app works fine locally. The environment variables should be found in the ENV hash from Ruby code.
Save changes and push new version of app to digital ocean
manually create the .env file on the digital ocean server, in the root of the repo
run digital ocean server and check that everything works.
other notes:
see How To Read and Set Environmental and Shell Variables on a Linux VPS
some platforms like heroku have a different mechanism for setting environment variables, such as heroku config:set or web UIs.
You can set environment variables on a one-off basis using the env command in bash, for example:
env a=hello b=' world' ruby -e 'puts ENV["a"] + ENV["b"]'
# => hello world
This can give a quick way to configure a program without getting into argument parsing. For example in Rails, you can say rails c test to open a console using the test environment, but env RAILS_ENV=test rails c should do the same thing.
I created a rails app that is hosted on Heroku. Now I want to upload a ruby script that will populate my database.
This script already exists and is using
PGconn.connect( :hostaddr=>"127.0.0.1", :port=>5432, :dbname=>"myDB", :user=>"MyUser", :password=>'MyPass');
This will obviously not work on Heroku.
I tried to get it using:
Rails.configuration.database_configuration
But I get
uninitialized constant Rails
How can I get the information so I can connect to my Heroku DB? Is there any way to make the same code wotk both on my localhost and on Heroku?
(ps: I am using foreman and the .ENV file, if it helps)
Edit:
The solution I took:
require 'yaml'
require 'erb'
config = YAML.load(ERB.new(File.read(File.join("config","database.yml"))).result)
In config I have all the information I need to perform the DB access.
Why would you not just access ENV['DATABASE_URL'] and break it up into the constituent parts? To see how this is done, if you do heroku run bash and do cat config\database.yml you will see how Heroku does this itself.
To ensure that locally it works as well if you execute via foreman run <Scriptname> then it will be executed and the contents of your .env will be loaded.
Is it possible to have multiple schedule.rb files when using the whenever gem in rails to setup cron jobs? I am looking to have a regular schedule.rb file and also a reports_schedule.rb file that is going to be deployed to a different server and it has its own specific reports environment.
How does whenever use the schedule.rb file? Is this possible?
It looks like it is possible if a bit ugly. Looking at the source code on job_list.rb:25 whenever just does an instance eval. So you can do something like the following.
schedule.rb
#Load reporting schedules
instance_eval(File.read('reporting_schedule.rb'), 'reporting_schedule.rb')
# All your regular jobs
# ...
reporting_schedule.rb
#Need some way to know if you are on the reporting server
if `hostname` =~ /reporting_server/
# All your reporting jobs
# ...
end
Worked for me doing some quick tests with the whenever command. Have not tried using it in a deploy though.
Not sure whether this was added after the question was asked, but you seem to be able to do role-based scheduling now: See README at https://github.com/javan/whenever
I am using Rails 3 and have a need to run WEBrick with SSL support during development. To achieve this, I've followed this guide:
http://www.nearinfinity.com/blogs/chris_rohr/configuring_webrick_to_use_ssl.html
This works well, however, I want to ensure that these settings do no affect my rails application when run in production mode. We are currently using Apache/Passenger, and the project appears to still run fine. Is there a clean way, however, to make sure that this code isn't even executed? I'm thinking a possible answer could be an if/end block around the code, or perhaps a built-in rails facility that allows development-only code to be placed in a separate file or something similar.
Looks like ENV['RAILS_ENV'] is your friend. The ENV hash shows you the Unix environment the app is being run under and Rails itself will look at RAILS_ENV to decide in which mode to run. You could do something like this:
if ENV['RAILS_ENV'].to_s == 'development' || ENV['RAILS_ENV'].to_s == ''
# do your thing here
end
You can also make sure that you run webrick with that environment:
#> RAILS_ENV=development /path/to/webrick/script
Hope it helps.
Is there a ruby script to generate password for .htpasswd? Thanks!
HTAuth is a pure Ruby implementation of the Apache htpasswd and htdigest utilities.
It also sports an API so you can use the functionality from within your own Ruby code without running external scripts.