Skip to content

Commit

Permalink
There should be a simple, unified way of configuring a running Vanill…
Browse files Browse the repository at this point in the history
…a application, which doesn't involve putting the config into Vanilla itself. This should make it simpler for Vanilla to simply be a gem in the future.
  • Loading branch information
lazyatom committed Dec 13, 2008
1 parent b4eed26 commit 3e44c1f
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 34 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Expand Up @@ -3,6 +3,5 @@ pkg
*.db
.DS_Store
*~
vanilla-authorization.yml
tmp
defensio.yml
config.yml
2 changes: 1 addition & 1 deletion README
Expand Up @@ -32,7 +32,7 @@ Thee Darke Invocation

$ gem install soup sqlite3-ruby rack ratom RedCloth BlueCloth

$ rake bootstrap
$ rake setup

... a bunch of stuff gets created

Expand Down
24 changes: 16 additions & 8 deletions Rakefile
Expand Up @@ -71,8 +71,9 @@ task :upgrade => ["upgrade:dynasnips"]
desc 'Add a user (or change an existing password)'
task :add_user => :prepare do
puts "Adding a new user"
credential_file = File.join(Vanilla::App.root,'config','vanilla-authorization.yml')
credentials = YAML.load(File.open(credential_file)) rescue {}
# config_file = ENV['VANILLA_CONFIG'] || 'config.yml'
# config_file = YAML.load(File.open(config_file)) rescue {}
app = Vanilla::App.new(ENV['VANILLA_CONFIG'])
print "Username: "
username = STDIN.gets.chomp.strip
print "Password: "
Expand All @@ -82,14 +83,15 @@ task :add_user => :prepare do
if password != confirm_password
raise "Passwords don't match!"
else
credentials[username] = MD5.md5(password).to_s
File.open(credential_file, "w") { |f| f.write credentials.to_yaml }
app.config[:credentials] ||= {}
app.config[:credentials][username] = MD5.md5(password).to_s
app.config.save!
puts "User '#{username}' added."
end
end

desc 'Generate file containing secret for cookie-based session storage'
task :generate_secret_file do
task :generate_secret do
# Adapted from old rails secret generator.
require 'openssl'
if !File.exist?("/dev/urandom")
Expand All @@ -103,8 +105,10 @@ task :generate_secret_file do
end
data = OpenSSL::BN.rand(2048, -1, false).to_s
secret = OpenSSL::Digest::SHA512.new(data).hexdigest
File.open(File.join(Vanilla::App.root,'config','secret.yml'),'w') {|f| f.write({"secret" => secret}.to_yaml)}
puts "Secret file generated."
app = Vanilla::App.new(ENV['VANILLA_CONFIG'])
app.config[:secret] = secret
app.config.save!
puts "Secret generated."
end


Expand All @@ -123,7 +127,11 @@ EOM

puts <<-EOM
Generating the file that will contain the secret for cookie-based session storage.
Now I'm going to generate your configuration. This will be stored either in
'config.yml' in the current directory, or in the path you provide via the
environment variable VANILLA_CONFIG.
Generating the secret for cookie-based session storage.
EOM
Rake::Task[:generate_secret_file].invoke

Expand Down
5 changes: 5 additions & 0 deletions config.example.yml
@@ -0,0 +1,5 @@
---
:filename: config.example.yml
:secret: a6bc097eff86cabd92ee72ba4687c3f057b7556deaa3e4a6b284460871056b87ba3e91548c37dcc44fbc10241cee5b386556e6bcc2946fd9b609dc3bc1b24488
:credentials:
admin: 5f4dcc3b5aa765d61d8327deb882cf99
2 changes: 1 addition & 1 deletion config.ru
Expand Up @@ -6,4 +6,4 @@ use Rack::Session::Cookie, :key => 'vanilla.session',
:expire_after => 2592000,
:secret => YAML.load(File.read(File.join(Vanilla::App.root,'config','secret.yml')))['secret']
use Rack::Static, :urls => ["/public"], :root => File.join(File.dirname(__FILE__), *%w[vanilla])
run Vanilla::App.new
run Vanilla::App.new(ENV['VANILLA_CONFIG'])
17 changes: 12 additions & 5 deletions lib/vanilla/app.rb
Expand Up @@ -4,9 +4,10 @@
module Vanilla
class App

attr_reader :request, :response
attr_reader :request, :response, :config

def initialize
def initialize(config_file=nil)
prepare_configuration(config_file)
Soup.prepare
end

Expand Down Expand Up @@ -83,9 +84,15 @@ def render_missing_snip(snip_name)
"[snip '#{snip_name}' cannot be found]"
end

def self.root
File.dirname(__FILE__)
end
private

def prepare_configuration(config_file)
config_file ||= "config.yml"
@config = YAML.load(File.open(config_file)) rescue {}
@config[:filename] = config_file
def @config.save!
File.open(self[:filename], 'w') { |f| f.puts self.to_yaml }
end
end
end
end
2 changes: 0 additions & 2 deletions lib/vanilla/config/secret.yml.example

This file was deleted.

2 changes: 0 additions & 2 deletions lib/vanilla/config/vanilla-authorization.yml.example

This file was deleted.

3 changes: 1 addition & 2 deletions lib/vanilla/dynasnips/login.rb
Expand Up @@ -27,8 +27,7 @@ def get(*args)
end

def post(*args)
credentials = YAML.load(File.open(File.join(Vanilla::App.root,'config','vanilla-authorization.yml')))
if credentials[cleaned_params[:name]] == MD5.md5(cleaned_params[:password]).to_s
if app.config[:credentials][cleaned_params[:name]] == MD5.md5(cleaned_params[:password]).to_s
app.request.session['logged_in_as'] = cleaned_params[:name]
login_controls
else
Expand Down
38 changes: 38 additions & 0 deletions spec/vanilla_app_spec.rb
@@ -0,0 +1,38 @@
require File.join(File.dirname(__FILE__), 'spec_helper')

describe Vanilla::App do
describe "when behaving as a Rack application" do
it "should return an array of status code, headers and response" do
create_snip(:name => "test", :content => "content")
result = Vanilla::App.new.call(mock_env_for_url("/test.text"))
result.should be_a_kind_of(Array)
result[0].should == 200
result[1].should be_a_kind_of(Hash)
result[2].each{ |output| output.should == "content" }
end
end

describe "when being configured" do
it "should load a config file from the current working directory by default" do
File.should_receive(:open).with("config.yml").and_return(StringIO.new({}.to_yaml))
Vanilla::App.new
end

it "should load a config file given" do
File.open("/tmp/vanilla_config.yml", "w") { |f| f.write({:hello => true}.to_yaml) }
app = Vanilla::App.new("/tmp/vanilla_config.yml")
app.config[:hello].should be_true
end

it "should allow saving of configuration to the same file it was loaded from" do
config_file = "/tmp/vanilla_config.yml"
File.open(config_file, "w") { |f| f.write({:hello => true}.to_yaml) }
app = Vanilla::App.new(config_file)
app.config[:saved] = true
app.config.save!

config = YAML.load(File.open(config_file))
config[:saved].should be_true
end
end
end
11 changes: 0 additions & 11 deletions spec/vanilla_rack_app_spec.rb

This file was deleted.

0 comments on commit 3e44c1f

Please sign in to comment.