I'm trying to get hello world working remotely in sinatra:
require 'sinatra'
get '/' do
"hello world"
end
locally it works fine:
curl localhost:4567
hello world
but when I try to access it remotely, I get a 404 error.
The server is visible; I have other web applications running just fine (but not on nonstandard ports). this is a near-stock ubuntu install so there aren't any iptables rules that would block access to port 4567. Is there something I'm missing? I've had difficulty googling this.
I assume this is not firewall issue. Add bind set :bind, '0.0.0.0' something like below
#app.rb
require 'sinatra'
set :bind, '0.0.0.0'
get "/" do
"Working"
end
to run this
ruby app.rb
Related
I try to set up and get Ruby working with docker. How can I setup logging for ruby server (fatal error, warning etc.). I ran into problem where the server would crash every time I got a fatal error and I had to restart docker again. Thanks in advance. Here is the example:
Dockerfile:
CMD ["ruby", "server.rb", "-e", "production"]
server.rb
require 'sinatra'
set :server, :puma
set :bind, '0.0.0.0'
require_relative 'routes/barcode'
I have a Sinatra application with WebPack, when I start my app in development I spawn a process running Webpack Dev Server and use RackProxy to serve assets from my Sinatra app.
I also have to run Webpack Dev Server when running my JS Capybara acceptance tests just when Capybara spawns the ruby server.
So far I've came up with placing the WDS start in the driver register block, but I have different drivers.
Capybara.register_driver :headless_chrome do |app|
Rack::WebpackProxy.start_server
Capybara::Selenium::Driver.new(app, browser: :chrome, args: ["headless"])
end
What is the hook to run code when Capybara server is spawned?
Capybara.server= takes the registered name of the server proc to use when starting the server, and you can register your own using Capybara.register_server. Therefore you should be able to do something like
Capybara.register_server :my_server do |app, port, host, **options|
# start whatever you need started here
do_my_stuff
Capybara.servers[:puma].call(app, port, host) # assuming you want puma as the actual server
end
Capybara.server = :my_server
What could possibly be wrong? I run this file:
require 'sinatra'
get '/' do
"Just Do It"
end
Server starts up:
== Sinatra (v2.0.0) has taken the stage on 3000 for development with backup from Thin
Thin web server (v1.7.1 codename Muffin Mode)
Maximum connections set to 1024
Listening on 127.0.0.1:3000, CTRL+C to stop
(I set the address and port because it is what I have in my vagrantfile.
However I get nothing, either in Chrome or Firefox. I've tried various config.ru's and changed the file above to an ru extension to try rackup but that hasn't worked either.
I have uninstalled and reinstalled the Mustermann and Sinatra gems and made sure I had the gems that are required in base.rb.
Any ideas?
I searched the internet for a "Hello, World" type program for Webrick in Ruby, but could not find anything that worked. I found this guide on SO but for the life of me could not get it to work.
Consulting the Ruby Documentation for Webrick led me to some code snippets that got me going in the right direction. There were no easy-to-follow tutorials so I wanted to add my answer on SO.
I was using Ubuntu 14.04 without Apache or Nginx and wanted my server for a virtual host. Webrick by default does not respond to requests concurrently but for me this was a plus in addition to its small footprint. I was hoping to get it working without the Rails framework for an even lighter footprint.
To get started, I installed Ruby with the Ubuntu package manager. If you are using CentOS or another Linux distribution, you can adapt this step to your particular package manager. Also make sure port 80 is open on your web server. It's possible to get SSL with Webrick but I chose not to at this point.
sudo apt-get install ruby
Here is the script which I named myapp.rb that I am using. I placed it /var/www/myapp. Ideally, I think it should not be in document root. You should also create a special user and group just to run the script to improve security (I have not outlined those steps here)
require 'webrick'
server = WEBrick::HTTPServer.new(:Port => 80,
:SSLEnable => false,
:DocumentRoot => '/var/www/myapp',
:ServerAlias => 'myapp.example.com')
server.mount_proc '/' do |req, res|
res.body = 'Hello, world!'
end
trap 'INT' do server.shutdown end
server.start
The require statement above tells Ruby to include the Webrick classes when running the program. The second line of the script creates an instance of Webrick with the following options:
Use Port 80
Disable SSL
Set the document root to /var/www/myapp
Set the Server Alias to myapp.example.com
Of course, you'll have to configure your particular domains DNS'. The server.mount_proc is telling Ruby to serve the response, "Hello, world" at document root. I think you can specify a subdirectory there if you life. The Ruby Webrick documentation above has information on that.
The line that begins with trap means that the web server can be stopped with Ctrl-C. To start the script I typed the following at the SSH command line:
ruby myapp.rb
I've got a Sinatra "hello world" app that I am trying to run using jRuby. It works when I run the app, but not when I run rackup. Can anyone tell me what is going on here?
Here's the app, in a file 'app.rb':
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
configure do
set :bind, '0.0.0.0'
end
get '/' do
'Boo!'
end
I can run this using bundle exec ruby app.rb and it works fine:
jonea#centos7andy[~/andy/sinatra_sand_jruby]%: bundle exec ruby app.rb
[2015-01-12 10:36:06] INFO WEBrick 1.3.1
[2015-01-12 10:36:06] INFO ruby 1.9.3 (2014-12-09) [java]
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from WEBrick
[2015-01-12 10:36:06] INFO WEBrick::HTTPServer#start: pid=31654 port=4567
Here is my config.ru to call the above program:
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require './app'
run Sinatra::Application
If I run this, it appears to work, but I can't access the server using a web browser:
jonea#centos7andy[~/andy/sinatra_sand_jruby]%: bundle exec rackup -p4567
[2015-01-12 10:29:06] INFO WEBrick 1.3.1
[2015-01-12 10:29:06] INFO ruby 1.9.3 (2014-12-09) [java]
[2015-01-12 10:29:06] INFO WEBrick::HTTPServer#start: pid=31553 port=4567
I note the suspicious lack of "Sinatra has taken the stage..."
When you run the Ruby file directly (or when you add Sinatra.run! to the config.ru file) Sinatra runs its own server. In this case the call to set :bind, '0.0.0.0' will take effect. When you run through rackup this setting is ignored.
The default host that rackup listens to is localhost, so the server will only be available through the same machine, you won’t be able to access it from other machines. To access it through other machines set the --host option:
bundle exec rackup -p4567 --host 0.0.0.0
(Note the output of rackup -h for the current version says the default host is 0.0.0.0, but this is out of date and has been fixed in master.)
Well, this is hardly sufficient to explain what is going on, but I can make it work if in config.ru I replace
run Sinatra::Application
with
Sinatra::Application.run!
In fact, knowing that makes me even more confused. Some sort of bug in Rack?
#config.ru
require "./app.rb"
set :bind, '0.0.0.0'
set :port, 9292 #set your port!
Sinatra::Application.run!
try this code and type rackup
Then you can get the results you want.
I have slightly similar situation.
But the difference is that, my Jruby + Sinatra rackup app is finally starts responding.
But it takes lots of time, sometimes it starts responding 5 minutes after app start.
I found out, that after app start port is not listened for some period time.
If we make netstat -an it will not show our app port.
Actually I don't know the reason of such behavior, but I'll dig for it.