Ruby Sinatra Webservice running on localhost:4567 but not on IP
Following worked for me. ruby app.rb -o 0.0.0.0
Following worked for me. ruby app.rb -o 0.0.0.0
config.ru is a Rack configuration file (ru stands for “rackup”). Rack provides a minimal interface between web servers that support Ruby and Ruby frameworks. It’s like a Ruby implementation of a CGI which offers a standard protocol for web servers to execute programs. Rack’s run command here means for requests to the server, make Sinatra::Application … Read more
this seems to do it for me: require ‘sinatra/base’ require ‘webrick’ require ‘webrick/https’ require ‘openssl’ CERT_PATH = ‘/opt/myCA/server/’ webrick_options = { :Port => 8443, :Logger => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG), :DocumentRoot => “/ruby/htdocs”, :SSLEnable => true, :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE, :SSLCertificate => OpenSSL::X509::Certificate.new( File.open(File.join(CERT_PATH, “my-server.crt”)).read), :SSLPrivateKey => OpenSSL::PKey::RSA.new( File.open(File.join(CERT_PATH, “my-server.key”)).read), :SSLCertName => [ [ “CN”,WEBrick::Utils::getservername ] ] } … Read more
Sinatra provides a request object, which is the interface to the client request data that you should be using. Using request.ip is the preferred method to find the client’s IP address: get “https://stackoverflow.com/” do “Your IP address is #{request.ip}” end
Here is a very simple authentication scheme for Sinatra. I’ll explain how it works below. class App < Sinatra::Base set :sessions => true register do def auth (type) condition do redirect “/login” unless send(“is_#{type}?”) end end end helpers do def is_user? @user != nil end end before do @user = User.get(session[:user_id]) end get “https://stackoverflow.com/” do … Read more
just pass the :locals to the erb() in your routes: get ‘/hello/:name’ do erb :hello, :locals => {:name => params[:name]} end and then just use it in the views/hello.erb: Hello <%= name %> (tested on sinatra 1.2.6)
There was a recent commit to Sinatra that changed the default listen address to localhost from 0.0.0.0 in development mode due to security concerns. In order to explicitly allow access from the network, you need to either run your app in another mode (e.g. production), or set the bind option to 0.0.0.0. You can do … Read more
Use a sinatra before handler: before do request.body.rewind @request_payload = JSON.parse request.body.read end this will expose it to the current request handler. If you want it exposed to all handlers, put it in a superclass and extend that class in your handlers.
menu.to_s.encode(‘UTF-8’, invalid: :replace, undef: :replace, replace: ‘?’) This worked perfectly, I had to replace some extra characters but there are no more errors.
Sure, add content_type to the before callback: class MyApp < Sinatra::Base before do content_type ‘application/json’ end … end Sinatra 1.1 introduces pattern-matching before filters: before ‘/admin/*’ do check_logged_in end