Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code from the "Riak with Rails" webinar
# Add this line to your config/application.rb to enable
# initialization of Ripple, after the other framework railties
require 'ripple/railtie'
# To use the Riak cache store, place this line inside the
# Application class
config.cache_store = :riak_store
# Stores files in Riak, redirects their download to reverse-proxy through nginx
class FilesController < ApplicationController
def index
@files = Ripple.client['files'].keys
end
def create
uploaded = params[:file]
key = ::File.basename(uploaded.original_filename)
Ripple.client['files'].new(key).tap do |robj|
robj.data = uploaded.read
robj.content_type = uploaded.content_type
robj.store
end
redirect_to files_path
end
def show
key = [params[:id],params[:format]].compact.join(".")
if Ripple.client['files'].exists?(key)
headers["X-Accel-Redirect"] = "/riak/files/#{key}"
head :ok
else
redirect_to files_path
end
end
end
<!-- this is the index view for the FilesController -->
<h1>Files</h1>
<ul>
<% @files.each do |file| %>
<li><%= link_to file, "/files/#{file}" %></li>
<% end %>
</ul>
<h2>Upload a file</h2>
<%= form_tag files_path, :multipart => true do %>
<%= file_field_tag :file %>
<%= submit_tag "Upload" %>
<% end %>
# Sets up internal redirects to Riak backend, for X-Accel-Redirect file-serving
# Thanks to Pat Allan (@pat) for most of this config.
daemon off;
events {
worker_connections 1024;
}
http {
include /usr/local/etc/nginx/mime.types;
# Assuming path has been set to a Rails application
access_log log/nginx.access.log;
error_log log/nginx.error.log;
client_body_temp_path tmp/nginx.client_body_temp;
fastcgi_temp_path tmp/nginx.client_body_temp;
proxy_temp_path tmp/nginx.proxy_temp;
# Replace this with whatever `passenger-config --root` outputs
passenger_root /Users/sean/.rvm/gems/ruby-1.9.2-rc2@webinar/gems/passenger-2.2.15;
# Replace this with whatever `which ruby` outputs
passenger_ruby /Users/sean/.rvm/rubies/ruby-1.9.2-rc2/bin/ruby;
# Riak reverse-proxy configuration
upstream riak {
server localhost:8098;
# server riak2:8098;
# server riak3:8098;
}
server {
listen 3000;
server_name localhost;
root public;
passenger_enabled on;
rails_env development;
# Target for X-Accel-Redirect responses
location /riak {
internal;
proxy_pass http://riak/riak;
}
}
}
#!/usr/bin/env bash
nginx -p `pwd`/ -c `pwd`/config/nginx.conf \
-g "error_log `pwd`/log/nginx.error.log; pid `pwd`/log/nginx.pid;";
# config/ripple.yml
# Just like database.yml - one key per environment
development:
host: 127.0.0.1
port: 8098
test: &TEST
host: 127.0.0.1
port: 9000
production:
host: prod-riak-host
port: 8098
cucumber:
<<: *TEST
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.