picnic 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/History.txt +10 -0
  2. data/Manifest.txt +30 -13
  3. data/Rakefile +2 -0
  4. data/lib/picnic.rb +5 -120
  5. data/lib/picnic/authentication.rb +40 -4
  6. data/lib/picnic/cli.rb +86 -43
  7. data/lib/picnic/conf.rb +45 -43
  8. data/lib/picnic/logger.rb +41 -0
  9. data/lib/picnic/server.rb +99 -0
  10. data/lib/picnic/version.rb +2 -2
  11. data/picnic.gemspec +44 -0
  12. data/vendor/{camping-1.5.180 → camping-2.0.20090212}/CHANGELOG +17 -10
  13. data/vendor/{camping-1.5.180 → camping-2.0.20090212}/COPYING +0 -0
  14. data/vendor/{camping-1.5.180 → camping-2.0.20090212}/README +2 -2
  15. data/vendor/{camping-1.5.180 → camping-2.0.20090212}/Rakefile +62 -5
  16. data/vendor/camping-2.0.20090212/bin/camping +99 -0
  17. data/vendor/camping-2.0.20090212/doc/camping.1.gz +0 -0
  18. data/vendor/camping-2.0.20090212/examples/README +5 -0
  19. data/vendor/camping-2.0.20090212/examples/blog.rb +375 -0
  20. data/vendor/camping-2.0.20090212/examples/campsh.rb +629 -0
  21. data/vendor/camping-2.0.20090212/examples/tepee.rb +242 -0
  22. data/vendor/camping-2.0.20090212/extras/Camping.gif +0 -0
  23. data/vendor/camping-2.0.20090212/extras/flipbook_rdoc.rb +491 -0
  24. data/vendor/camping-2.0.20090212/extras/permalink.gif +0 -0
  25. data/vendor/{camping-1.5.180 → camping-2.0.20090212}/lib/camping-unabridged.rb +168 -294
  26. data/vendor/camping-2.0.20090212/lib/camping.rb +54 -0
  27. data/vendor/{camping-1.5.180/lib/camping/db.rb → camping-2.0.20090212/lib/camping/ar.rb} +4 -4
  28. data/vendor/{camping-1.5.180/lib/camping → camping-2.0.20090212/lib/camping/ar}/session.rb +23 -14
  29. data/vendor/camping-2.0.20090212/lib/camping/mab.rb +26 -0
  30. data/vendor/camping-2.0.20090212/lib/camping/reloader.rb +163 -0
  31. data/vendor/camping-2.0.20090212/lib/camping/server.rb +158 -0
  32. data/vendor/camping-2.0.20090212/lib/camping/session.rb +74 -0
  33. data/vendor/camping-2.0.20090212/setup.rb +1551 -0
  34. data/vendor/camping-2.0.20090212/test/apps/env_debug.rb +65 -0
  35. data/vendor/camping-2.0.20090212/test/apps/forms.rb +95 -0
  36. data/vendor/camping-2.0.20090212/test/apps/misc.rb +86 -0
  37. data/vendor/camping-2.0.20090212/test/apps/sessions.rb +38 -0
  38. data/vendor/camping-2.0.20090212/test/test_camping.rb +54 -0
  39. metadata +43 -16
  40. data/lib/picnic/postambles.rb +0 -292
  41. data/lib/picnic/utils.rb +0 -36
  42. data/vendor/camping-1.5.180/lib/camping.rb +0 -57
  43. data/vendor/camping-1.5.180/lib/camping/fastcgi.rb +0 -244
  44. data/vendor/camping-1.5.180/lib/camping/reloader.rb +0 -163
  45. data/vendor/camping-1.5.180/lib/camping/webrick.rb +0 -65
@@ -1,3 +1,13 @@
1
+ === 0.8.0 :: 2009-03-18
2
+
3
+ * Overhauled for Camping 2.0. Major changes throughout. Some things to
4
+ watch out for:
5
+ * Your app's bin script should now specify :app_file instead of :app_path,
6
+ and :app_file should point to your Camping app's main .rb file.
7
+ * Postamples for Mongrel and WEBrick are gone, since everything is now
8
+ handled by Rack. It is also now possible to run Picnic apps on Phusion
9
+ Passenger (mod_rails/mod_rack).
10
+
1
11
  === 0.7.1 :: 2008-11-10
2
12
 
3
13
  * Fixed config file loading problems rooted in $APP_PATH inconsistencies.
@@ -9,21 +9,38 @@ lib/picnic/authentication.rb
9
9
  lib/picnic/cli.rb
10
10
  lib/picnic/conf.rb
11
11
  lib/picnic/controllers.rb
12
- lib/picnic/postambles.rb
12
+ lib/picnic/logger.rb
13
+ lib/picnic/server.rb
13
14
  lib/picnic/service_control.rb
14
- lib/picnic/utils.rb
15
15
  lib/picnic/version.rb
16
+ picnic.gemspec
16
17
  setup.rb
17
18
  test/picnic_test.rb
18
19
  test/test_helper.rb
19
- vendor/camping-1.5.180/CHANGELOG
20
- vendor/camping-1.5.180/COPYING
21
- vendor/camping-1.5.180/README
22
- vendor/camping-1.5.180/Rakefile
23
- vendor/camping-1.5.180/lib/camping-unabridged.rb
24
- vendor/camping-1.5.180/lib/camping.rb
25
- vendor/camping-1.5.180/lib/camping/db.rb
26
- vendor/camping-1.5.180/lib/camping/fastcgi.rb
27
- vendor/camping-1.5.180/lib/camping/reloader.rb
28
- vendor/camping-1.5.180/lib/camping/session.rb
29
- vendor/camping-1.5.180/lib/camping/webrick.rb
20
+ vendor/camping-2.0.20090212/CHANGELOG
21
+ vendor/camping-2.0.20090212/COPYING
22
+ vendor/camping-2.0.20090212/README
23
+ vendor/camping-2.0.20090212/Rakefile
24
+ vendor/camping-2.0.20090212/bin/camping
25
+ vendor/camping-2.0.20090212/doc/camping.1.gz
26
+ vendor/camping-2.0.20090212/examples/README
27
+ vendor/camping-2.0.20090212/examples/blog.rb
28
+ vendor/camping-2.0.20090212/examples/campsh.rb
29
+ vendor/camping-2.0.20090212/examples/tepee.rb
30
+ vendor/camping-2.0.20090212/extras/Camping.gif
31
+ vendor/camping-2.0.20090212/extras/flipbook_rdoc.rb
32
+ vendor/camping-2.0.20090212/extras/permalink.gif
33
+ vendor/camping-2.0.20090212/lib/camping-unabridged.rb
34
+ vendor/camping-2.0.20090212/lib/camping.rb
35
+ vendor/camping-2.0.20090212/lib/camping/ar.rb
36
+ vendor/camping-2.0.20090212/lib/camping/ar/session.rb
37
+ vendor/camping-2.0.20090212/lib/camping/mab.rb
38
+ vendor/camping-2.0.20090212/lib/camping/reloader.rb
39
+ vendor/camping-2.0.20090212/lib/camping/server.rb
40
+ vendor/camping-2.0.20090212/lib/camping/session.rb
41
+ vendor/camping-2.0.20090212/setup.rb
42
+ vendor/camping-2.0.20090212/test/apps/env_debug.rb
43
+ vendor/camping-2.0.20090212/test/apps/forms.rb
44
+ vendor/camping-2.0.20090212/test/apps/misc.rb
45
+ vendor/camping-2.0.20090212/test/apps/sessions.rb
46
+ vendor/camping-2.0.20090212/test/test_camping.rb
data/Rakefile CHANGED
@@ -22,6 +22,7 @@ ENV['NODOT'] = '1'
22
22
 
23
23
  NAME = "picnic"
24
24
  REV = nil
25
+ #REV = Time.now.strftime('%Y%m%d%H%M')
25
26
  #REV = `svn info`[/Revision: (\d+)/, 1] rescue nil
26
27
  VERS = ENV['VERSION'] || (Picnic::VERSION::STRING + (REV ? ".#{REV}" : ""))
27
28
  CLEAN.include ['**/.*.sw?', '*.gem', '.config']
@@ -56,6 +57,7 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
56
57
 
57
58
  # note that camping-1.5.0.180 is now bundled with picnic in the vendor directory.
58
59
  p.extra_deps = [
60
+ 'rack',
59
61
  'markaby',
60
62
  'activesupport'
61
63
  ]
@@ -1,126 +1,11 @@
1
1
  require 'rubygems'
2
- #require 'active_support'
2
+ require 'activesupport'
3
3
 
4
- $: << File.dirname(File.expand_path(__FILE__))+"/../vendor/camping-1.5.180/lib"
5
- require "camping"
4
+ $: << File.dirname(__FILE__)
5
+ $: << File.dirname(__FILE__) + "/../vendor/camping-2.0.20090212/lib"
6
6
 
7
- $: << File.dirname(File.expand_path(__FILE__))
8
- require "picnic/utils.rb"
9
- require "picnic/conf.rb"
10
- require "picnic/postambles.rb"
7
+ require "camping"
11
8
 
12
9
 
13
- class Module
14
-
15
- # Adds Picnic functionality to a Camping-enabled module.
16
- #
17
- # Example:
18
- #
19
- # Camping.goes :Blog
20
- # Blog.picnic!
21
- #
22
- # Your <tt>Blog</tt> Camping app now has Picnic functionality.
23
- def picnic!
24
- include Picnic
25
-
26
- puts "Adding Picnic functionality to #{self} from #{File.dirname(File.expand_path(__FILE__))}..."
27
- self.module_eval do
28
- # Initialize your application's logger.
29
- # This is automatically done for you when you call #picnic!
30
- # The logger is initialized based on your <tt>:log</tt> configuration.
31
- # See <tt>config.example.yml</tt> for info on configuring the logger.
32
- def init_logger
33
- puts "Initializing #{self} logger..."
34
- $LOG = Picnic::Utils::Logger.new(self::Conf.log[:file])
35
- $LOG.level = Picnic::Utils::Logger.const_get(self::Conf.log[:level])
36
- end
37
- module_function :init_logger
38
-
39
- # Initialize your application's database logger.
40
- # If enabled, all SQL queries going through ActiveRecord will be logged here.
41
- #
42
- # THIS SEEMS TO BE BROKEN RIGHT NOW and I can't really understand why.
43
- def init_db_logger
44
- begin
45
- if self::Conf.db_log
46
- log_file = self::Conf.db_log[:file] || "#{self.to_s.downcase}_db.log"
47
- self::Models::Base.logger = Picnic::Utils::Logger.new(log_file)
48
- self::Models::Base.logger.level = Picnic::Utils::Logger.const_get(self::Conf.db_log[:level] || 'DEBUG')
49
- $LOG.debug "Logging database queries to #{log_file.inspect}"
50
- end
51
- rescue Errno::EACCES => e
52
- $LOG.warn "Can't write to database log file at '#{log_file}': #{e}"
53
- end
54
- end
55
- module_function :init_db_logger
56
-
57
- # Enable authentication for your app.
58
- #
59
- # For example:
60
- #
61
- # Camping.goes :Blog
62
- # Blog.picnic!
63
- #
64
- # $CONF[:authentication] ||= {:username => 'admin', :password => 'picnic'}
65
- # Blog.authenticate_using :basic
66
- #
67
- # module Blog
68
- # def self.authenticate(credentials)
69
- # credentials[:username] == Taskr::Conf[:authentication][:username] &&
70
- # credentials[:password] == Taskr::Conf[:authentication][:password]
71
- # end
72
- # end
73
- #
74
- # Note that in the above example we use the authentication configuration from
75
- # your app's conf file.
76
- #
77
- def authenticate_using(mod)
78
- load "picnic/authentication.rb"
79
- mod = self::Authentication.const_get(mod.to_s.camelize) unless mod.kind_of? Module
80
-
81
- $LOG.info("Enabling authentication for all requests using #{mod.inspect}.")
82
-
83
- module_eval do
84
- include mod
85
- end
86
- end
87
- module_function :authenticate_using
88
-
89
- # Launches the web server to run your Picnic app.
90
- # This method will continue to run as long as your server is running.
91
- def start_picnic
92
- require "#{File.dirname(File.expand_path(__FILE__))}/picnic/postambles.rb"
93
- self.extend self::Postambles
94
-
95
- if $PID_FILE && !(self::Conf.server.to_s == 'mongrel' || self::Conf.server.to_s == 'webrick')
96
- $LOG.warn("Unable to create a pid file. You must use mongrel or webrick for this feature.")
97
- end
98
-
99
- puts "\nStarting with configuration: #{$CONF.to_yaml}"
100
- puts
101
-
102
- # begin
103
- raise NoMethodError if self::Conf.server.nil?
104
- send(self::Conf.server)
105
- # rescue NoMethodError => e
106
- # # FIXME: this rescue can sometime report the incorrect error messages due to other underlying problems
107
- # # raising a NoMethodError
108
- # if Fluxr::Conf.server
109
- # raise e, "The server setting '#{Fluxr::Conf.server}' in your config.yml file is invalid."
110
- # else
111
- # raise e, "You must have a 'server' setting in your config.yml file. Please see the Fluxr documentation."
112
- # end
113
- # end
114
- end
115
- module_function :start_picnic
116
-
117
- c = File.dirname(File.expand_path(__FILE__))+'/picnic/controllers.rb'
118
- p = IO.read(c).gsub("Picnic", self.to_s)
119
- eval p, TOPLEVEL_BINDING
120
-
121
- end
122
-
123
- self::Conf.load(self)
124
- init_logger
125
- end
10
+ module Picnic
126
11
  end
@@ -3,6 +3,42 @@ module Picnic #:nodoc:
3
3
  # for your Camping app.
4
4
  #
5
5
  module Authentication
6
+
7
+ def self.included(base)
8
+ base.module_eval do
9
+ # Enable authentication for your app.
10
+ #
11
+ # For example:
12
+ #
13
+ # Camping.goes :Blog
14
+ # Blog.picnic!
15
+ #
16
+ # $CONF[:authentication] ||= {:username => 'admin', :password => 'picnic'}
17
+ # Blog.authenticate_using :basic
18
+ #
19
+ # module Blog
20
+ # def self.authenticate(credentials)
21
+ # credentials[:username] == Taskr::Conf[:authentication][:username] &&
22
+ # credentials[:password] == Taskr::Conf[:authentication][:password]
23
+ # end
24
+ # end
25
+ #
26
+ # Note that in the above example we use the authentication configuration from
27
+ # your app's conf file.
28
+ #
29
+ def authenticate_using(mod)
30
+ mod = Picnic::Authentication.const_get(mod.to_s.camelize) unless mod.kind_of? Module
31
+
32
+ $LOG.info("Enabling authentication for all requests using #{mod.inspect}.")
33
+
34
+ module_eval do
35
+ include mod
36
+ end
37
+ end
38
+ module_function :authenticate_using
39
+ end
40
+ end
41
+
6
42
  # Picnic::Authentication::Basic provides Basic HTTP Authentication for your Camping app.
7
43
  # The module defines a <tt>service</tt> method that only continues the request chain when
8
44
  # proper credentials are provided by the client (browser).
@@ -50,7 +86,7 @@ module Picnic #:nodoc:
50
86
  # Reads the username and password from the headers and returns them.
51
87
  def read_credentials
52
88
  if d = %w{REDIRECT_X_HTTP_AUTHORIZATION X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION}.inject([]) \
53
- { |d,h| env.has_key?(h) ? env[h].to_s.split : d }
89
+ { |d,h| @env.has_key?(h) ? @env[h].to_s.split : d }
54
90
  u,p = ::Base64.decode64(d[1]).split(':')[0..1] if d[0] == 'Basic'
55
91
  return {:username => u, :password => p}
56
92
  end
@@ -68,9 +104,9 @@ module Picnic #:nodoc:
68
104
  s = super(*a)
69
105
  else
70
106
  @status = 401
71
- @headers['Content-type'] = @headers['Content-type'] || 'text/plain'
72
- @headers['Status'] = 'Unauthorized'
73
- @headers['WWW-Authenticate'] = "Basic realm=\"#{app}\""
107
+ headers['Content-type'] = @headers['Content-type'] || 'text/plain'
108
+ #headers['Status'] = 'Unauthorized'
109
+ headers['WWW-Authenticate'] = "Basic realm=\"#{app}\""
74
110
  @body = 'Unauthorized'
75
111
  s = self
76
112
  end
@@ -1,5 +1,9 @@
1
1
  require 'optparse'
2
2
 
3
+ require 'picnic'
4
+ require 'picnic/conf'
5
+ require 'picnic/server'
6
+
3
7
  module Picnic
4
8
  # Provides a command-line interface for your app.
5
9
  # This is useful for creating a 'bin' file for launching your application.
@@ -15,7 +19,7 @@ module Picnic
15
19
  #
16
20
  # cli = Picnic::Cli.new(
17
21
  # 'foo',
18
- # :app_path => "/path/to/foo.br"
22
+ # :app_file => "/path/to/foo.br"
19
23
  # )
20
24
  #
21
25
  # cli.handle_cli_input
@@ -36,85 +40,124 @@ module Picnic
36
40
  # +pid_file+:: Where the app's PID file (containing the app's
37
41
  # process ID) should be placed. By default this is
38
42
  # <tt>/etc/<app>/<app>.pid</tt>
39
- # +verbose+:: True if the cli handler should report
40
- # everything that it's doing to STDOUT.
41
43
  def initialize(app, options = {})
42
44
  @app = app
43
45
 
44
46
  @options = options || {}
45
- @options[:app_file] ||= File.expand_path(File.dirname(File.expand_path($0))+"/../lib/#{app}.rb")
47
+ @options[:app_file] ||= File.expand_path(File.dirname(File.expand_path(__FILE__))+"/../lib/#{app}.rb")
48
+ @options[:app_name] ||= app
46
49
  @options[:app_module] ||= app.capitalize
47
50
  @options[:pid_file] ||= "/etc/#{app}/#{app}.pid"
48
51
  @options[:conf_file] ||= nil
49
- @options[:verbose] ||= false
50
52
  end
51
53
 
52
54
  # Parses command line options given to the script.
53
55
  def handle_cli_input
54
- if File.exists? options[:app_file]
56
+ # if File.exists? options[:app_file]
55
57
  # try to use given app base path
56
- $APP_PATH = File.dirname(options[:app_file]).gsub(/\/(lib|bin)\/?$/, '')
57
- else
58
- require 'rubygems'
59
-
60
- # fall back to using gem installation
61
- matches = Gem::source_index.find_name(app)
62
- raise LoadError, "#{app} gem doesn't appear to be installed!" if matches.empty?
63
-
64
- gem_spec = matches.last
65
- $APP_PATH = gem_spec.full_gem_path
66
-
67
- gem(app)
58
+ $APP_ROOT = File.dirname(options[:app_file]).gsub(/\/(lib|bin)\/?$/, '')
59
+ # else
60
+ # require 'rubygems'
61
+ #
62
+ # # fall back to using gem installation
63
+ # matches = Gem::source_index.find_name(app)
64
+ # raise LoadError, "#{app} gem doesn't appear to be installed!" if matches.empty?
65
+ #
66
+ # gem_spec = matches.last
67
+ # $APP_ROOT = gem_spec.full_gem_path
68
+ #
69
+ # gem(app)
70
+ # end
71
+
72
+ unless File.file?(options[:app_file])
73
+ raise ArgumentError, "options[:app_file] points to #{options[:app_file].inspect} but this does not appear to be a valid Camping application!}"
68
74
  end
69
75
 
70
- $: << $APP_PATH+"/lib"
71
76
 
72
- $PID_FILE = "/etc/#{app}/#{app}.pid"
77
+ puts "Loading #{app.inspect} code from #{$APP_ROOT.inspect}..."
73
78
 
74
- OptionParser.new do |opts|
75
- opts.banner = "Usage: #{app} [options]"
79
+ $: << $APP_ROOT+"/lib"
76
80
 
77
- opts.on("-c", "--config FILE", "Use config file (default is /etc/#{app}/config.yml)") do |c|
78
- puts "Using config file #{c}"
79
- $CONFIG_FILE = c
80
- end
81
-
82
- opts.on("-d", "--daemonize", "Run as a daemon (only when using webrick or mongrel)") do |c|
81
+ $PID_FILE = @options[:pid_file]
82
+ $CONFIG_FILE = @options[:conf_file]
83
+ $VERBOSE = @options[:verbose]
84
+
85
+ opts = OptionParser.new do |opts|
86
+ #opts.banner = ""
87
+ #opts.define_head ""
88
+ #opts.separator ""
89
+ opts.on("-d", "--daemonize", "Run daemonized in the background") do
83
90
  $DAEMONIZE = true
84
91
  end
85
-
86
- opts.on("-P", "--pid_file FILE", "Use pid file (default is /etc/#{app}/#{app}.pid)") do |c|
87
- if $DAEMONIZE && !File.exists?(c)
88
- puts "Using pid file '#{c}'"
89
- $PID_FILE = c
90
- elsif File.exists?(c)
92
+ opts.on("-c", "--config FILE", "Use this config file (default is /etc/<app>/config.yml)") do |c|
93
+ puts "Using config file #{c.inspect}"
94
+ $CONFIG_FILE = c
95
+ end
96
+ opts.on("-P", "--pid_file FILE", "Path to pid file (used only when running daemonized; default is /etc/<app>/<app>.pid)") do |p|
97
+ if $DAEMONIZE && !File.exists?(p)
98
+ puts "Using pid file #{p.inspect}"
99
+ $PID_FILE = p
100
+ elsif File.exists?(p)
91
101
  puts "The pid file already exists. Is #{app} running?\n" +
92
- "You will have to first manually remove the pid file at '#{c}' to start the server as a daemon."
102
+ "You will have to first manually remove the pid file at '#{p}' to start the server as a daemon."
93
103
  exit 1
94
104
  else
95
105
  puts "Not running as daemon. Ignoring pid option"
96
106
  end
97
107
  end
98
-
99
- # :extra_cli_options should be a block with additonal app-specific opts.on() calls
108
+
109
+ # optoinal block with additonal opts.on() calls specific to your application
100
110
  if @options[:extra_cli_options]
101
111
  @options[:extra_cli_options].call(opts)
102
- end
103
-
112
+ end
113
+
114
+ # No argument, shows at tail. This will print an options summary.
115
+ # Try it and see!
104
116
  opts.on_tail("-h", "--help", "Show this message") do
105
117
  puts opts
106
118
  exit
107
119
  end
108
120
 
109
- opts.on_tail("-V", "--version", "Show version number") do
110
- require "#{$APP_PATH}/lib/#{app}/version.rb"
121
+ opts.on_tail("-v", "--version", "Show the application's version number") do
122
+ require "#{$APP_ROOT}/lib/#{app}/version.rb"
111
123
  app_mod = Object.const_get(@options[:app_module])
112
124
  puts "#{app}-#{app_mod::VERSION::STRING}"
113
125
  exit
114
126
  end
115
- end.parse!
127
+ end
128
+
129
+ begin
130
+ opts.parse! ARGV
131
+ rescue OptionParser::ParseError => ex
132
+ STDERR.puts "!! #{ex.message}"
133
+ puts "** use `#{File.basename($0)} --help` for more details..."
134
+ exit 1
135
+ end
136
+
137
+
138
+ $CONF = Picnic::Conf.new
139
+ $CONF.load_from_file(app, $APP_ROOT, $CONF_FILE)
140
+
141
+ if $DAEMONIZE
142
+ # TODO: use Process.daemon when RUBY_VERSION >= 1.9
143
+
144
+ exit if fork
145
+ Process.setsid
146
+ exit if fork
147
+
148
+ Dir.chdir $APP_ROOT
149
+ File.umask 0000
150
+
151
+ STDIN.reopen $CONF.log[:file], "a"
152
+ STDOUT.reopen $CONF.log[:file], "a"
153
+ STDERR.reopen $CONF.log[:file], "a"
154
+
155
+ File.open($PID_FILE, 'w'){ |f| f.write("#{Process.pid}") }
156
+ at_exit { File.delete($PID_FILE) if File.exist?($PID_FILE) }
157
+ end
116
158
 
117
- require "#{$APP_PATH}/lib/#{app}.rb"
159
+ server = Picnic::Server::Base.new($CONF, [options[:app_file]])
160
+ server.start
118
161
  end
119
162
  end
120
163
  end