sinatra

Running Sinatra on port 80

一个人想着一个人 提交于 2019-12-04 00:51:40
I installed Sinatra and it works but it uses port 4567 by default. I want it to run on port 80. In an effort to get it to work on port 80, I tried this: require 'rubygems' require 'rack/handler/webrick' require 'sinatra' Sinatra::Application.default_options.merge!( :run => false, :env => :production, :port => 80 ) get '/' do "Hello World" end But I get this error: $ ruby -rubygems index.rb index.rb:5:in `<main>': undefined method `default_options' for Sinatra::Application:Class (NoMethodError) Any idea what's going on? Can't you just use ( http://www.sinatrarb.com/configuration.html ): set

Environment variables locally and Heroku

拥有回忆 提交于 2019-12-04 00:04:26
I have a sinatra app in which i have a yml file to set environment variables, i call them using this method module MyConfig def config environment = ENV["RACK_ENV"] || "development" YAML.load_file("./config/config.yml")[environment] end end so when i want to use a variable i do this for example aws_access_key_id = config['aws_access_key'] I have a .gitignore file that ignores config.yml when pushing to github for example.So when I push to heroku these environment variables will not be accessible? So this leaves me with using the heroku way of setting them like so heroku config:add aws_access

alias url with sinatra / padrino

怎甘沉沦 提交于 2019-12-03 21:57:38
I've this and it works get :about, :map => '/about_us' do render :erb, "<%= 'foo' %>" end get '/:slug' do redirect "/about_us" # <-- end Is possible to do in some way "render" instead of "redirect"? or something like render 'posts/1' get :about, :map => '/about_us' do render :erb, "<%= 'foo' %>" end get '/:slug' do call env.merge('PATH_INFO' => '/about_us') end 来源: https://stackoverflow.com/questions/3384134/alias-url-with-sinatra-padrino

Handling event-stream connections in a Sinatra app

╄→尐↘猪︶ㄣ 提交于 2019-12-03 21:41:35
There is a great example of a chat app using Server-Sent Events by Konstantin Haase. I am trying to run it and have a problem with callbacks (I use Sinatra 1.3.2 and browse with Chrome 16). They simply do not run (e.g. after page reload), and therefore the number of connections is growing. Also, connection is closed in 30-60 sec unless one sets a periodic timer to send empty data, as suggested by Konstantin elsewhere. Can you replicate it? If yes, is it possible to fix these issues somehow? WebSockets work seamlessly in this respect... # ruby get '/stream', provides: 'text/event-stream' do

Accessing the irb in a modular Sinatra application

久未见 提交于 2019-12-03 21:35:21
I am building an application which subclasses Sinatra like so: require 'rubygems' require 'sinatra/base' require 'sinatra/assetpack' class App < Sinatra::Base ... run! end How can I access irb? Options are not parsed when executing sinatra this way, how do I programmatically open an irb shell? I'm a little confused whether you want to open an IRB session from within your app (?) or use IRB to debug your Sinatra project? For debugging Rack-based apps (such as Sinatra), I like using the racksh gem , which " is like script/console in Rails " for Rack applications. Its main advantage over IRB is

Simple Ruby Input Validation Library

天大地大妈咪最大 提交于 2019-12-03 18:51:17
问题 I've been looking all over the place for a simple input validation library for Ruby. Everything seems to point towards ActiveRecord (or similar). I'm not using Rails, I'm using Sinatra without an ORM. What's the best approach for validating user input (without tying directly to the model layer)? Simple things like "string length", "is numeric" etc. Preferably with a nice mechanism for declaring error messages. 回答1: You could use ActiveModel::Validations, from Rails 3 RC: require 'active_model

Deploying sinatra app (with config.ru) on heroku cedar stack

孤人 提交于 2019-12-03 18:44:13
问题 I'm trying to refactor my sinatra code to separate my main file into separate files, using some tips from this response, and I'm having troubles deploying to heroku. Previously I didn't have a config.ru file, and just used my Procfile , which was: web: bundle exec ruby web.rb -p $PORT as per this article. From the refactor, I've now changed my Procfile to web: bundle exec thin -R config.ru start -p $PORT With my config.ru file being root = ::File.dirname(__FILE__) require ::File.join( root,

Can I have Sinatra / Rack not read the entire request body into memory?

非 Y 不嫁゛ 提交于 2019-12-03 17:25:16
Say I have a Sinatra route ala: put '/data' do request.body.read # ... end It appears that the entire request.body is read into memory. Is there a way to consume the body as it comes into the system, rather than having it all buffered in Rack/Sinatra beforehand? I see I can do this to read the body in parts, but the entire body still seems to be read into memory beforehand. put '/data' do while request.body.read(1024) != nil # ... end # ... end Konstantin Haase You cannot avoid this in general without patching Sinatra and/or Rack. It is done by Rack::Request when request.POST is called by

Sinatra server push?

北战南征 提交于 2019-12-03 17:15:38
问题 What is the best way to push data from a server written in Sinatra to a client? Think similarly to a chat room, but without Ajax polling every 2500ms. I know of Juggernaut in Rails, but was curious about Sinatra. 回答1: A little googling turned up this blog post by Marc-André Cournoyer, which discusses the asynchronous response feature of the Thin web server and includes a link to an async-sinatra library to do just this. You could also take a look at orbited-ruby, a Ruby implementation of the

Can you specify the HTTP method to use with Sinatra's redirect?

笑着哭i 提交于 2019-12-03 16:25:27
I have something like this: post "/login" do end get "/login" do end post "/register" do ... redirect "/login" # I would like to redirect to get "/login" instead of post "login" end Can I tell sinatra that I want to use get, and not have it implicitly select "post" ? Most browsers will reset to GET with a 302 (default) redirect. If you're up against a browser that is actually doing the correct thing and NOT changing the request method, you can force it to change with a 303. redirect '/login', 303 Source: http://www.gittr.com/index.php/archive/details-of-sinatras-redirect-helper/ 来源: https:/