Make config settable from outside the app

This commit is contained in:
Basti 2012-02-29 14:07:51 +01:00
parent 064b8dc06a
commit 64698b8503
2 changed files with 16 additions and 8 deletions

View File

@ -3,10 +3,14 @@ require "riak"
module RemoteStorage module RemoteStorage
module Riak module Riak
def client
return @client if @client
@client = ::Riak::Client.new(LiquorCabinet.config['riak'].symbolize_keys)
end
def authorize_request(user, category, token) def authorize_request(user, category, token)
return true if category == "public" && env["REQUEST_METHOD"] == "GET" return true if category == "public" && env["REQUEST_METHOD"] == "GET"
client = ::Riak::Client.new(settings.riak_config)
categories = client.bucket("authorizations").get("#{user}:#{token}").data categories = client.bucket("authorizations").get("#{user}:#{token}").data
halt 403 unless categories.include?(category) halt 403 unless categories.include?(category)
@ -15,14 +19,12 @@ module RemoteStorage
end end
def get_data(user, category, key) def get_data(user, category, key)
client = ::Riak::Client.new(settings.riak_config)
client.bucket("user_data").get("#{user}:#{category}:#{key}").data client.bucket("user_data").get("#{user}:#{category}:#{key}").data
rescue ::Riak::HTTPFailedRequest rescue ::Riak::HTTPFailedRequest
halt 404 halt 404
end end
def put_data(user, category, key, data) def put_data(user, category, key, data)
client = ::Riak::Client.new(settings.riak_config)
object = client.bucket("user_data").new("#{user}:#{category}:#{key}") object = client.bucket("user_data").new("#{user}:#{category}:#{key}")
object.content_type = "text/plain" object.content_type = "text/plain"
object.data = data object.data = data
@ -32,7 +34,6 @@ module RemoteStorage
end end
def delete_data(user, category, key) def delete_data(user, category, key)
client = ::Riak::Client.new(settings.riak_config)
riak_response = client.bucket("user_data").delete("#{user}:#{category}:#{key}") riak_response = client.bucket("user_data").delete("#{user}:#{category}:#{key}")
halt riak_response[:code] halt riak_response[:code]
rescue ::Riak::HTTPFailedRequest rescue ::Riak::HTTPFailedRequest

View File

@ -9,15 +9,22 @@ class LiquorCabinet < Sinatra::Base
include RemoteStorage::Riak include RemoteStorage::Riak
def self.config=(config)
@config = config
end
def self.config
return @config if @config
config = File.read(File.expand_path('config.yml', File.dirname(__FILE__)))
@config = YAML.load(config)[ENV['RACK_ENV']]
end
configure :development do configure :development do
register Sinatra::Reloader register Sinatra::Reloader
enable :logging enable :logging
end end
configure :development, :test, :production do configure do
config = File.read(File.expand_path('config.yml', File.dirname(__FILE__)))
riak_config = YAML.load(config)[ENV['RACK_ENV']]['riak'].symbolize_keys
set :riak_config, riak_config
end end
before "/:user/:category/:key" do before "/:user/:category/:key" do