How to specify memcache server to Rack::Session::Memcache? - ruby

I'm trying to configure my Rack app to use Memcache for sessions with Rack::Session::Memcache
How do I give it the options (such as server, username and password)?
Presently I have
use Rack::Session::Memcache
But I get the error
in `initialize': No memcache servers (RuntimeError)
Heroku has put the config in environment variables
MEMCACHE_PASSWORD:
MEMCACHE_SERVERS:
MEMCACHE_USERNAME:
I know I can get these in Ruby with ENV['MEMCACHE_PASSWORD'] but I don't know how to give them to Rack::Session::Memcache
Edit: or to Rack::Session::Dalli that would be great too https://github.com/mperham/dalli

This config worked for Heroku, Dalli is clever and knows to look in the environment variables
require 'dalli'
require 'rack/session/dalli'
use Rack::Session::Dalli, :cache => Dalli::Client.new
After reading the source code at https://github.com/mperham/dalli/commit/4ac5a99

Related

How to enable redis for Dashing?

I use free heroku instance to run my Dashing project. In result, it looses the value passed previously, when my instance sleeps. I was recommended to use Redis to keep history. I tryed to follow the instruction given here. In result I got the following config.ru (as part of my dashing project):
require 'dashing'
require 'redis-objects'
require 'yaml'
configure do
set :auth_token, 'my-token'
set :default_dashboard, 'def' # https://github.com/Shopify/dashing/wiki/How-To:-Change-the-default-dashboard
helpers do
def protected!
# Put any authentication code you want in here.
# This method is run before accessing any resource.
end
end
end
def redis?
ENV.has_key? 'REDISTOGO_URL'
end
if redis?
redis_uri = URI.parse(ENV['REDISTOGO_URL'])
Redis.current = Redis.new(:host => redis_uri.host,
:port => redis_uri.port,
:password => redis_uri.password)
set :history, Redis::HashKey.new('dashing-history')
elsif File.exists?(settings.history_file)
set history: YAML.load_file(settings.history_file)
else
set history: {}
end
map Sinatra::Application.assets_prefix do
run Sinatra::Application.sprockets
end
run Sinatra::Application
and the following Gemfile:
source 'https://rubygems.org'
gem 'dashing'
gem 'redis-objects'
## Remove this if you don't need a twitter widget.
gem 'twitter', '>= 5.9.0'
But it didn't help. What I did incorrectly?
I also tried to use this tutorial. But it was giving me an error at line redis_uri = URI.parse(ENV["REDISTOGO_URL"]) (something like wrong url is given).
The problem was that the app requires the add-on Redis To Go
If Redis To Go is configured, REDISTOGO_URL is added to environment variables, it will work
For more information on how to setup Redis To Go, read the heroku article
Adding Redis to an application provides benefits, you may be using RedisToGo to power simple Resque or Sidekiq jobs, or using the raw power of Redis 2.6 Lua Scripting to do some crazy fast operations. Redis can be used a database, but it’s often used as a complementary datastore. With over 140 commands, the possibilities are endless.

How Can I use nginx X-Accel-Redirect in a ruby cgi script

I'm using nginx to run a ruby script. I've already set up the nginx config correctly as it works with php-fpm
In php I set
header('X-Accel-Redirect, file_path)
Is there an equivalent for it in ruby.
I have tried it with
cgi = CGI.new
cgi.out( "X-Accel-Redirect" => new_file)
But it doesn't seem to work...
Is any any gem that I could include.
I'm new to ruby. any help would be highly appreciated.
Assuming you are using a Rack application (Rails, Sinatra, Padrino etc.) the same way you used your php-fpm apps (i.e. behind Nginx as a reverse proxy), you should be able to do the same thing you did in PHP:
response['X-Accel-Redirect'] = file_path

Sinatra heroku database access

I have a sinatra ruby app in heroku. I am trying to access the database via the console.When I run the heroku run console , I am getting the following error.
Running console attached to terminal... up, run.10
/app/vendor/ruby-1.9.2/lib/ruby/1.9.1/irb/init.rb:281:in `require':LoadError: no such file to load -- ./console.
When I try to access the record using the following command, I am getting the following error :
irb(main):001:0> Setting.first
NameError: uninitialized constant Object::Setting
from (irb):1
from bin/irb:12:in `<main>'
Can anyone help me in what needs to be done. Am I missing some file or Is there a different way to access the tables in heroku?
The heroku console thing is an old hack for rails apps, but it won't work elsewhere. As you can see from the output, it's trying to load a file called ./console. So, create a console file on your project root, and invoke IRB from it after having connected to your database. For example:
#!/usr/bin/env ruby
require 'irb'
require 'irb/completion'
require 'rubygems'
require 'bundler/setup'
# require something that connects to your database
# or just connect here using ENV['DATABASE_URL']
require 'your_project_setup'
IRB.start

How to get Gzip and Expires Header on a Rails 3.1.1 app on Heroku Cedar?

I'm running a Rails 3.1.1 application on Heroku Cedar. By default this stack doesn't Gzip and set Expires Headers on assets.
There is some doc about that, but it's not very clear : http://devcenter.heroku.com/articles/http-routing
Can somebody give me the piece of code to activate that ?
Thank you very much
Cedar doesn't use Nginx, so you have to gzip assets yourself with Rack::Deflater, like so :
# config.ru
require ::File.expand_path('../config/environment', __FILE__)
use Rack::Deflater
run YourApp::Application
Also you can set headers for static files directly in your app :
# config/environments/production.rb
config.static_cache_control = "public, max-age=3600"
Finally you're probably better off setting up Rack::Cache to replace Varnish caching. See this blog post for more infos.
Shameless plug - I created a gem which enables compression, but avoids compressing images.
https://github.com/romanbsd/heroku-deflater
It is important that the middleware be included early, before ActionDispatch::Static
#production.rb
config.middleware.insert_before ActionDispatch::Static, Rack::Deflater
> rake middleware
use Rack::Deflater
use ActionDispatch::Static
use Rack::Lock
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007f8e18455e90>
use Rack::Runtime
use Rack::MethodOverride
use ActionDispatch::RequestId
use Rails::Rack::Logger
use ActionDispatch::ShowExceptions
use ActionDispatch::DebugExceptions
use ActionDispatch::RemoteIp
use ActionDispatch::Reloader
use ActionDispatch::Callbacks
use ActionDispatch::Cookies
use ActionDispatch::Session::CookieStore
use ActionDispatch::Flash
use ActionDispatch::ParamsParser
use Remotipart::Middleware
use ActionDispatch::Head
use Rack::ConditionalGet
use Rack::ETag
use ActionDispatch::BestStandardsSupport
use Warden::Manager
use Rack::Mongoid::Middleware::IdentityMap
use Rack::Pjax
run MyApp::Application.routes

How to get ip address, referer, and user agent in ruby?

I want to log user's ip address, referer, and user agent.
In PHP, I can get them from the following variables:
$_SERVER['REMOTE_ADDR']
$_SERVER['HTTP_REFERER']
$_SERVER['HTTP_USER_AGENT']
How to get them in ruby?
PHP is embedded in a web server. Ruby is a general-purpose language: if you need a web server context, you'll have to install it yourself. Fortunately, it's easy.
One of the easiest ways to get started is with Sinatra. Install the gem:
gem install sinatra
Then create myapp.rb:
require 'sinatra'
get '/' do
request.user_agent
end
Start up the web server:
ruby -rubygems myapp.rb
Visit Sinatra's default URL: http://localhost:4567/
Et voilà.
You need the array request.env
request.env['REMOTE_ADDR']:
I'm assuming you by ruby you mean ruby on rails, the following link shows you how to access them:
http://techoctave.com/c7/posts/25-rails-request-environment-variables

Resources