"rackup config.ru" returns nothing? - ruby

I have my beginner Ruby application working perfectly on my local server using Rack and Sinatra. I'm in the process of getting it to work on my VPS webhost.
Here are my questions:
I tried running:
rackup config.ru
via SSH, and nothing happens, it just shows an empty line and I have to hit CNTRL+C to abort. Does anyone have ideas why?
If I run ruby main.rb, it successfully says Sinatra is taking the stage, but that's it, so I know you can't have it working with JUST that, but at least Sinatra is working properly.
I have a public_html folder. Is the main.rb and config.ru supposed to go in root "/" or is it supposed to go in public_html?
If it's the later, and I put a blank index.html inside public_html, will rackup know to 'replace' it with main.rb in / when it's running, and if it's not it just displays the index.html in public_html
When all is up and running, do I need to access the Ruby app via site.com:xxxx (xxxx=whatever port), or will I simply be able to go to site.com
You dont really need my code for main.rb, it just outputs "hello"
Here's config.ru:
require File.dirname(__FILE__) + "/main"
run Sinatra::Application

Q1, just rackup without config.ru
Q2, in general, the static folder name is public, not public_html unless you set it manually. other files, such as the main.rb, that is supposed to put into root directory, stay with the config.ru in the same folder if the code of main.rb as your given.
Q3, access by http://0.0.0.0:9292/ if you use the rackup by default setting to rack up the web server

Related

Running cron job on localhost using Whenever gem

Currently I have a Ruby and Sinatra project in which I am trying to automate a task. I have followed all the installation steps for the Whenever gem and written a function in my schedule.rb file that looks like this:
set :output, "./cron_log.log"
every 1.minutes do
command "ruby ./scripts/my_script.rb"
end
My project will eventually be deployed to a remote server, but I do not have access to that at the moment. Instead, I am testing it all by running it on my local machine using the command bundle exec shotgun -o 0.0.0.0 -p 9393. When I run it, none of the functions in my schedule.rb file are run. I ran a test by putting require './scripts/my_script.rb' at the top of the schedule.rb file. Whenever I run bundle exec whenever, that outside function will execute, but not the function inside the every block. I figured maybe it only works on a live server, so I ran the bundle exec shotgun -o 0.0.0.0 -p 9393 command to start my server on localhost but nothing in the schedule.rb file was called, including the outside function. I tried putting the command system("bundle exec whenever") in my authentication file, which gets activated every time a user loads the home page of my website, and the outside function in schedule.rb does get called, but the every function does not. Even if it did work like this, I don't want the file to get called every single time a user accesses the home page.
I also tried putting require './config/schedule' in the authentication file and that just completely breaks the website and gives this error message:
Boot Error
Something went wrong while loading config.ru
NoMethodError: undefined method `every' for main:Object
Here is part of the output when running the crontab -l command:
# Begin Whenever generated tasks for: /file_path_redacted/config/schedule.rb at: 2022-10-21 18:50:21 -0500
* * * * * /bin/bash -l -c 'ruby ./scripts/my_script.rb >> ./cron_log.log 2>&1'
# End Whenever generated tasks for: /file_path_redacted/config/schedule.rb at: 2022-10-21 18:50:21 -0500
So, my question is this: How and when is the schedule.rb file supposed to get called? And is it important that I deploy the project to the remote server for the cron job to work? Again, it is not possible for me to deploy it, as it must be fully working before being deployed. If it is possible to run on localhost, what am I doing wrong?

Phusion passenger is overriding my database config

I am using nginx with Phusion Passenger to bootload a ruby on rails application, if I run the application like
rails s -e production
It does connect correctly to the database, but when bootloaded from nginx with passenger it tries to use root to the database, like ignoring the config files.
I already tried giving permissions, but doesnt look like there is the problem, I already opened the 3 possible host names for root, which could be "localhost", "%" and "127.0.0.1", but in any case it should be openning a connection with root
I would say something is weird on how passenger behaves or that somewhere (not in the app) is ignoring the database.yml or overriding the credentials
in my opinion, I think you should try Passenger Standalone that is what I use and it works fine.

How can I get Heroku's database information in a ruby script?

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.

Simplest way to run ruby CGI app

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
)

Running .rb Ruby file

What is needed to run .rb file in a web browser?
(My first day in ruby)
I have Apache server (I'm coming from PHP) with passenger. Terminal proof:
gundars#linuxr528:~$ apache2ctl -t -D DUMP_MODULES
passenger_module (shared)
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
I also have installed rails etc, Tried to make a quick app with rails and it worked fine.
Now I would like to simply open .rb file in browser, and it is prompting me to save it.
This is how it looks in my browser, the window on right side pops-up after clicking, it asks where to save it.
Entries, concerning Ruby and this file, are:
apache2.conf:
LoadModule passenger_module /home/gundars/.rvm/gems/ruby-1.9.3-p0/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
PassengerRoot /home/gundars/.rvm/gems/ruby-1.9.3-p0/gems/passenger-3.0.11
PassengerRuby /home/gundars/.rvm/wrappers/ruby-1.9.3-p0/ruby
virtual-hosts.conf
<VirtualHost *:80>
ServerName tictactoe.ruby.dev
DocumentRoot "/localhost/sandbox/RUBY/tictactoe/"
ErrorLog /var/log/rubyonrails2.log
</VirtualHost>
/etc/hosts
127.0.0.1 tictactoe.ruby.dev
So the question - What is needed to run .rb file in a web browser?
I thought it's what passenger was for...
STEPS TO RUN .rb SCRIPTS FROM APACHE:
Edit your apache configuration file (e.g., /etc/httpd/conf/httpd.conf). Update or create an AddHandler setting so that it includes ".rb", e.g.:
AddHandler cgi-script .cgi .rb
Create your ruby script (.rb). Make sure you have a shebang (#!) pointing to your ruby interpreter. Use the "which" command if you're not sure where ruby is.
#!/bin/ruby
puts "Content-type: text/html"
puts
puts "<html>"
puts "<body>"
puts "<h3>Hello</h3>"
Set proper ownership and execute permissions (x) on your .rb file so apache can execute it, e.g.:
-rwxr-x--- 1 apache apache 163 Nov 26 10:55 index.rb
That might be all you have to do. If your script still doesn't execute, the first troubleshooting step I would try is temporarily disabling SELinux (using setenforce 0).
First day? Try a Sinatra tutorial. It's easy to get something up and running quickly and then you can concentrate on learning the language a bit.
Ruby is a server-side language. What you need is for a process to intercept your request, route it to the correct Ruby file, execute it, and send the output to your browser. Luckily, somebody has done this for you, with Rack and Passenger.
Why you would want to do this instead of working within the Rails framework is beyond me.

Resources