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
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin require "rubygems" rescue LoadError end
4
+ require "camping"
5
+
6
+ Camping.goes :EnvDebug
7
+
8
+ module EnvDebug
9
+ module Controllers
10
+ class ShowEnv < R '/', '/(.*)'
11
+ def get(extra = nil)
12
+ @extra = extra
13
+ render :show_env
14
+ end
15
+ alias post get
16
+ end
17
+ end
18
+
19
+ module Views
20
+ def layout
21
+ html do
22
+ head{ title C }
23
+ body do
24
+ ul do
25
+ li{ a "show env", :href=>R(ShowEnv)}
26
+ end
27
+ p { yield }
28
+ end
29
+ end
30
+ end
31
+
32
+ def _print_hash(hash)
33
+ hash.keys.sort.each do |key|
34
+ value = hash[key]
35
+ pre "%30s: %s" % [key.inspect, value.inspect]
36
+ end
37
+ end
38
+
39
+ def show_env
40
+ b "extra: #{@extra.inspect}"
41
+ h2 "@status : #{@status.inspect}"
42
+ h2 "@method : #{@method.inspect}"
43
+ h2 "@root : #{@root.inspect}"
44
+ h2 "@env :"
45
+ _print_hash @env
46
+ h2 "@input : "
47
+ _print_hash @input
48
+ h2 "@headers :"
49
+ _print_hash @headers
50
+ end
51
+
52
+ end
53
+ end
54
+
55
+ # For CGI
56
+ if $0 == __FILE__
57
+ EnvDebug.create if EnvDebug.respond_to? :create
58
+ if ARGV.any?
59
+ require 'camping/fastcgi'
60
+ #Dir.chdir('/var/camping/blog/')
61
+ Camping::FastCGI.start(EnvDebug)
62
+ else
63
+ puts EnvDebug.run
64
+ end
65
+ end
@@ -0,0 +1,95 @@
1
+ require "camping"
2
+
3
+ Camping.goes :Forms
4
+
5
+ module Forms
6
+ module Controllers
7
+ class Index < R '/'
8
+ def get; render :index end
9
+ end
10
+ class GetForm
11
+ def get; render :get_form; end
12
+ end
13
+ class GetFormResult
14
+ def get; render :form_result; end
15
+ end
16
+ class PostForm
17
+ def get; render :post_form; end
18
+ def post; render :form_result; end
19
+ end
20
+ class FileForm
21
+ def get; render :file_form; end
22
+ def post; render :form_result; end
23
+ end
24
+ end
25
+
26
+ module Views
27
+ def layout
28
+ html do
29
+ head{ title C }
30
+ body do
31
+ ul do
32
+ li{ a "index", :href=>R(Index)}
33
+ li{ a "get form", :href=>R(GetForm)}
34
+ li{ a "post form", :href=>R(PostForm)}
35
+ li{ a "file form", :href=>R(FileForm)}
36
+ end
37
+ p { yield }
38
+ end
39
+ end
40
+ end
41
+
42
+ def form_result
43
+ p @input.inspect
44
+ end
45
+
46
+ def index
47
+ h1 "Welcome on the Camping test app"
48
+ end
49
+
50
+ def get_form
51
+ form :action=>R(GetFormResult), :method=>:get do
52
+ label "Give me your name!", :for=>:name
53
+ input :type=>:text, :name=>:name; br
54
+ input :type=>:text, :name=>"arr[]"; br
55
+ input :type=>:text, :name=>"arr[]"; br
56
+ input :type=>:text, :name=>"hash[x]"; br
57
+ input :type=>:text, :name=>"hash[y]"; br
58
+
59
+ input :type=>:submit
60
+ end
61
+ end
62
+
63
+ def post_form
64
+ form :action=>R(PostForm), :method=>:post do
65
+ label "Give me your name!", :for=>:name
66
+ input :type=>:text, :name=>:name; br
67
+ input :type=>:text, :name=>"arr[]"; br
68
+ input :type=>:text, :name=>"arr[]"; br
69
+ input :type=>:text, :name=>"hash[x]"; br
70
+ input :type=>:text, :name=>"hash[y]"; br
71
+
72
+ input :type=>:submit
73
+ end
74
+ end
75
+
76
+ def file_form
77
+ form :action=>R(FileForm), :method=>:post, :enctype=>"multipart/form-data" do
78
+ input :type=>:text, :name=>"arr"
79
+ input :type=>:text, :name=>"arr"; br
80
+ input :type=>:file, :name=>"first_file"; br
81
+ input :type=>:file, :name=>"files[]"; br
82
+ input :type=>:file, :name=>"files[]"; br
83
+ input :type=>:submit
84
+ end
85
+ end
86
+
87
+
88
+ end
89
+ end
90
+
91
+ # For CGI
92
+ if $0 == __FILE__
93
+ Forms.create
94
+ Forms.run
95
+ end
@@ -0,0 +1,86 @@
1
+ require "camping"
2
+
3
+ Camping.goes :Misc
4
+
5
+ module Misc
6
+ module Controllers
7
+ class Index < R '/'
8
+ def get; render :index end
9
+ end
10
+ class RenderPartial
11
+ def get; render :_partial; end
12
+ end
13
+ class Xsendfile
14
+ def get
15
+ @headers["X-Sendfile"] = File.expand_path(__FILE__)
16
+ "You shouldn't get this text"
17
+ end
18
+ end
19
+ class Links < R '/links', '/links/(\w+)/with/(\d+)/args'
20
+ def get(*args); render :links; end
21
+ end
22
+ class Redirect
23
+ def get; redirect(RR) end
24
+ end
25
+ class RR
26
+ def get; render :rr; end
27
+ end
28
+ class BadLinks
29
+ def get; render :bad_links; end
30
+ end
31
+ class BadMethod; end
32
+ end
33
+
34
+ module Views
35
+ def layout
36
+ html do
37
+ head{ title C }
38
+ body do
39
+ ul do
40
+ li{ a "index", :href=>R(Index)}
41
+ li{ a "render partial", :href=>R(RenderPartial)}
42
+ li{ a "X-Sendfile", :href=>R(Xsendfile)}
43
+ li{ a "Links", :href=>R(Links)}
44
+ li{ a "BadLinks", :href=>R(BadLinks)}
45
+ li{ a "Redirect", :href=>R(Redirect)}
46
+ li{ a "BadMethod", :href=>R(BadMethod)}
47
+ end
48
+ p { yield }
49
+ end
50
+ end
51
+ end
52
+
53
+ def _partial
54
+ a "go back to index", :href=>R(Index)
55
+ end
56
+
57
+ def index
58
+ h1 "Welcome on the Camping test app"
59
+ end
60
+
61
+ def links
62
+ a "plain", :href=>R(Links); br
63
+ a "with args and hash", :href=>R(Links, "moo", 3, :with=>"Hash"); br
64
+ a "with args and mult. hash", :href=>R(Links, "hoi", 8, :with=>"multiple", 3=>"hash"); br
65
+ # TODO : with <AR::Base object
66
+ end
67
+
68
+ def bad_links
69
+ a "null controller", :href=>R(nil)
70
+ a "bad arity", :href=>R(RR, :moo)
71
+ a "bad args", :href=>R(Links, 3, "moo")
72
+ end
73
+
74
+ def rr
75
+ p "got redirected"
76
+ end
77
+
78
+
79
+ end
80
+ end
81
+
82
+ # For CGI
83
+ if $0 == __FILE__
84
+ Misc.create
85
+ Misc.run
86
+ end
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "camping"
5
+
6
+ Camping.goes :Sessions
7
+ require 'camping/session'
8
+
9
+ module Sessions
10
+ include Camping::Session
11
+ module Controllers
12
+ class One < R('/')
13
+ def get
14
+ @state = C::H['one',rand(100)]
15
+ puts "1:" + @state.inspect
16
+ redirect R(Two)
17
+ end
18
+ end
19
+
20
+ class Two < R('/steptwo')
21
+ def get
22
+ @state['two'] = "This is in two"
23
+ puts "2:" + @state.inspect
24
+ redirect R(Three)
25
+ end
26
+ end
27
+
28
+ class Three < R('/stepthree')
29
+ def get
30
+ @state['three'] = "This is in three"
31
+ puts "3:" + @state.inspect
32
+ return "Accumulated state across redirects: #{@state.inspect}"
33
+ redirect R(Three)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
@@ -0,0 +1,54 @@
1
+ require "test/unit"
2
+ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
3
+ require "camping"
4
+
5
+ class Test_Camping_class_methods < Test::Unit::TestCase
6
+ def test_escape_from_the_doc
7
+ assert_equal "I%27d+go+to+the+museum+straightway%21", Camping.escape("I'd go to the museum straightway!")
8
+ end
9
+
10
+ def test_utf8_escape
11
+ assert_equal "%E6%97%A5%E6%9C%AC%E5%9B%BD", Camping.escape("\346\227\245\346\234\254\345\233\275")
12
+ end
13
+
14
+ def test_unescape_from_the_doc
15
+ assert_equal "I'd go to the museum straightway!", Camping.un("I%27d+go+to+the+museum+straightway%21")
16
+ end
17
+
18
+ def test_utf8_unescape
19
+ assert_equal "\346\227\245\346\234\254\345\233\275", Camping.un("%E6%97%A5%E6%9C%AC%E5%9B%BD")
20
+ end
21
+
22
+ def test_qsp_from_the_doc
23
+ input = Camping.qsp("name=Philarp+Tremain&hair=sandy+blonde")
24
+ assert_equal "Philarp Tremain", input.name
25
+ assert_equal "sandy blonde", input.hair
26
+ end
27
+
28
+ def test_qsp_with_array
29
+ input = Camping.qsp("users[]=why&users[]=rambo")
30
+ assert_equal ["why", "rambo"], input.users
31
+ end
32
+
33
+ def test_qsp_with_hash
34
+ input = Camping.qsp("post[id]=1&post[user]=_why")
35
+ hash = {'post' => {'id' => '1', 'user' => '_why'}}
36
+ assert_equal hash, input
37
+ end
38
+
39
+ def test_qsp_malformed1
40
+ input = Camping.qsp("name=Philarp+Tremain&&hair=sandy+blonde")
41
+ assert_equal "Philarp Tremain", input.name
42
+ assert_equal "sandy blonde", input.hair
43
+ end
44
+
45
+ def test_qsp_malformed2
46
+ input = Camping.qsp("morta==del")
47
+ assert_equal "=del", input.morta
48
+ end
49
+
50
+ # TODO : Test Camping.kp
51
+ # TODO : Test Camping.run
52
+
53
+ end
54
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picnic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Zukowski
@@ -9,9 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-11 00:00:00 -05:00
12
+ date: 2009-03-18 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
15
25
  - !ruby/object:Gem::Dependency
16
26
  name: markaby
17
27
  type: :runtime
@@ -40,7 +50,7 @@ dependencies:
40
50
  requirements:
41
51
  - - ">="
42
52
  - !ruby/object:Gem::Version
43
- version: 1.7.0
53
+ version: 1.8.2
44
54
  version:
45
55
  description: Camping for sissies
46
56
  email: matt@roughest.net
@@ -66,24 +76,41 @@ files:
66
76
  - lib/picnic/cli.rb
67
77
  - lib/picnic/conf.rb
68
78
  - lib/picnic/controllers.rb
69
- - lib/picnic/postambles.rb
79
+ - lib/picnic/logger.rb
80
+ - lib/picnic/server.rb
70
81
  - lib/picnic/service_control.rb
71
- - lib/picnic/utils.rb
72
82
  - lib/picnic/version.rb
83
+ - picnic.gemspec
73
84
  - setup.rb
74
85
  - test/picnic_test.rb
75
86
  - test/test_helper.rb
76
- - vendor/camping-1.5.180/CHANGELOG
77
- - vendor/camping-1.5.180/COPYING
78
- - vendor/camping-1.5.180/README
79
- - vendor/camping-1.5.180/Rakefile
80
- - vendor/camping-1.5.180/lib/camping-unabridged.rb
81
- - vendor/camping-1.5.180/lib/camping.rb
82
- - vendor/camping-1.5.180/lib/camping/db.rb
83
- - vendor/camping-1.5.180/lib/camping/fastcgi.rb
84
- - vendor/camping-1.5.180/lib/camping/reloader.rb
85
- - vendor/camping-1.5.180/lib/camping/session.rb
86
- - vendor/camping-1.5.180/lib/camping/webrick.rb
87
+ - vendor/camping-2.0.20090212/CHANGELOG
88
+ - vendor/camping-2.0.20090212/COPYING
89
+ - vendor/camping-2.0.20090212/README
90
+ - vendor/camping-2.0.20090212/Rakefile
91
+ - vendor/camping-2.0.20090212/bin/camping
92
+ - vendor/camping-2.0.20090212/doc/camping.1.gz
93
+ - vendor/camping-2.0.20090212/examples/README
94
+ - vendor/camping-2.0.20090212/examples/blog.rb
95
+ - vendor/camping-2.0.20090212/examples/campsh.rb
96
+ - vendor/camping-2.0.20090212/examples/tepee.rb
97
+ - vendor/camping-2.0.20090212/extras/Camping.gif
98
+ - vendor/camping-2.0.20090212/extras/flipbook_rdoc.rb
99
+ - vendor/camping-2.0.20090212/extras/permalink.gif
100
+ - vendor/camping-2.0.20090212/lib/camping-unabridged.rb
101
+ - vendor/camping-2.0.20090212/lib/camping.rb
102
+ - vendor/camping-2.0.20090212/lib/camping/ar.rb
103
+ - vendor/camping-2.0.20090212/lib/camping/ar/session.rb
104
+ - vendor/camping-2.0.20090212/lib/camping/mab.rb
105
+ - vendor/camping-2.0.20090212/lib/camping/reloader.rb
106
+ - vendor/camping-2.0.20090212/lib/camping/server.rb
107
+ - vendor/camping-2.0.20090212/lib/camping/session.rb
108
+ - vendor/camping-2.0.20090212/setup.rb
109
+ - vendor/camping-2.0.20090212/test/apps/env_debug.rb
110
+ - vendor/camping-2.0.20090212/test/apps/forms.rb
111
+ - vendor/camping-2.0.20090212/test/apps/misc.rb
112
+ - vendor/camping-2.0.20090212/test/apps/sessions.rb
113
+ - vendor/camping-2.0.20090212/test/test_camping.rb
87
114
  has_rdoc: true
88
115
  homepage: http://picnic.rubyforge.org
89
116
  post_install_message:
@@ -1,292 +0,0 @@
1
- module Picnic
2
- # In Camping, "postambles" are wrappers used to make your application run under
3
- # some given web server. They are called "postambles" because traditionally this
4
- # was a piece of code included at the bottom of your Camping script. Picnic
5
- # provides postambles for two popular Ruby-based web servers: webrick and mongrel.
6
- # These postambles take care of all the complications of launching and managing
7
- # the web server for you.
8
- module Postambles
9
-
10
- # Launches your Camping application using a webrick server.
11
- #
12
- # It is possible to run using SSL by providing an <tt>:ssl_cert</tt> path in your
13
- # app's configuration file, pointing to a valid SSL certificate file.
14
- #
15
- # If $DAEMONIZE is true, the webrick server will be forked to a background process.
16
- # Note that this may have some strange effects, since any open IOs or threads will not
17
- # be carried over to the forked daemon process.
18
- #
19
- # Module#start_picnic automatically calls this method to launch your Picnic app if
20
- # your app's <tt>:server</tt> configuration option is set to <tt>'webrick'</tt>.
21
- #
22
- # Usage example:
23
- #
24
- # require 'picnic/postambles'
25
- # self.extend self::Postambles
26
- # webrick
27
- #
28
- def webrick
29
- require 'webrick/httpserver'
30
- require 'webrick/https'
31
- require 'camping/webrick'
32
-
33
- # TODO: verify the certificate's validity
34
- # example of how to do this is here: http://pablotron.org/download/ruri-20050331.rb
35
-
36
- cert_path = Picnic::Conf.ssl_cert
37
- key_path = Picnic::Conf.ssl_key || Picnic::Conf.ssl_cert
38
- # look for the key in the ssl_cert if no ssl_key is specified
39
-
40
- webrick_options = {
41
- :BindAddress => Picnic::Conf.bind_address || "0.0.0.0",
42
- :Port => Picnic::Conf.port
43
- }
44
-
45
- unless cert_path.nil? && key_path.nil?
46
- raise "The specified certificate file #{cert_path.inspect} does not exist. " +
47
- " Your 'ssl_cert' configuration setting must be a path to a valid " +
48
- " ssl certificate." unless
49
- File.exists? cert_path
50
-
51
- raise "The specified key file #{key_path.inspect} does not exist. " +
52
- " Your 'ssl_key' configuration setting must be a path to a valid " +
53
- " ssl private key." unless
54
- File.exists? key_path
55
-
56
- require 'openssl'
57
-
58
- cert = OpenSSL::X509::Certificate.new(File.read(cert_path))
59
- key = OpenSSL::PKey::RSA.new(File.read(key_path))
60
-
61
- webrick_options[:SSLEnable] = true
62
- webrick_options[:SSLVerifyClient] = ::OpenSSL::SSL::VERIFY_NONE
63
- webrick_options[:SSLCertificate] = cert
64
- webrick_options[:SSLPrivateKey] = key
65
- end
66
-
67
- begin
68
- s = WEBrick::HTTPServer.new(webrick_options)
69
- rescue Errno::EACCES
70
- puts "\nThe server could not launch. Are you running on a privileged port? (e.g. port 443) If so, you must run the server as root."
71
- exit 2
72
- end
73
-
74
- self.create
75
- s.mount "#{Picnic::Conf.uri_path}", WEBrick::CampingHandler, self
76
-
77
- public_dirs = Picnic::Conf.public_dirs || Picnic::Conf.public_dir
78
- if public_dirs
79
- public_dirs = [public_dirs] unless public_dirs.kind_of? Array
80
-
81
- public_dirs.each do |d|
82
- dir = d[:dir]
83
- path = "#{Picnic::Conf.uri_path}/#{d[:path]}".gsub(/\/\/+/,'/')
84
- $LOG.debug("Mounting public directory #{dir.inspect} to path #{path.inspect}.")
85
- s.mount(path, WEBrick::HTTPServlet::FileHandler, dir)
86
- end
87
- end
88
-
89
- # This lets Ctrl+C shut down your server
90
- trap(:INT) do
91
- s.shutdown
92
- end
93
- trap(:TERM) do
94
- s.shutdown
95
- end
96
-
97
- server_url = "http#{webrick_options[:SSLEnable] ? 's' : ''}://#{ENV['HOSTNAME'] || 'localhost'}:#{Picnic::Conf.port}#{Picnic::Conf.uri_path}"
98
-
99
- if $DAEMONIZE
100
- puts "\n** #{self} will run at #{server_url} and log to #{Picnic::Conf.log[:file].inspect}. "
101
- puts "** Check the log file for further messages!\n\n"
102
-
103
- logdev = $LOG.instance_variable_get(:@logdev).instance_variable_get(:@filename)
104
- if logdev == 'STDOUT' || logdev == nil
105
- puts "\n!!! Warning !!!\nLogging to the console (STDOUT) is not possible once the server daemonizes. "+
106
- "You should change the logger configuration to point to a real file."
107
- end
108
-
109
- WEBrick::Daemon.start do
110
- begin
111
- #self.init_db_logger
112
- write_pid_file if $PID_FILE
113
- $LOG.info "Starting #{self} as a WEBrick daemon with process id #{Process.pid}."
114
- self.prestart if self.respond_to? :prestart
115
- s.start
116
- $LOG.info "Stopping #{self} WEBrick daemon with process id #{Process.pid}."
117
- clear_pid_file
118
- rescue => e
119
- $LOG.error e
120
- raise e
121
- end
122
- end
123
- else
124
- puts "\n** #{self} is running at #{server_url} and logging to #{Picnic::Conf.log[:file].inspect}\n\n"
125
- self.prestart if self.respond_to? :prestart
126
- s.start
127
- end
128
- end
129
-
130
- # Launches your Camping application using a mongrel server.
131
- #
132
- # Mongrel is much faster than webrick, but has two limitations:
133
- # 1. You must install the mongrel gem before you can run your server using mongrel.
134
- # This is not necessary with webrick, since webrick is included by default with Ruby.
135
- # 2. Unlike webrick, mongrel can't be run using SSL. You will have to use a reverse
136
- # proxy like Pound or Apache if you want to use mongrel with SSL.
137
- #
138
- # If $DAEMONIZE is true, the mongrel server will be forked to a background process.
139
- # Note that this may have some strange effects, since any open IOs or threads will not
140
- # be carried over to the forked daemon process.
141
- #
142
- # Module#start_picnic automatically calls this method to launch your Picnic app if
143
- # your app's <tt>:server</tt> configuration option is set to <tt>'mongrel'</tt>.
144
- #
145
- # Usage example:
146
- #
147
- # require 'picnic/postambles'
148
- # self.extend self::Postambles
149
- # mongrel
150
- #
151
- def mongrel
152
- require 'rubygems'
153
- require 'mongrel/camping'
154
-
155
- if $DAEMONIZE
156
- # check if log and pid are writable before daemonizing, otherwise we won't be able to notify
157
- # the user if we run into trouble later (since once daemonized, we can't write to stdout/stderr)
158
- check_pid_writable if $PID_FILE
159
- check_log_writable
160
- end
161
-
162
- self.create
163
-
164
- puts "\n** #{self} is starting. Look in #{Picnic::Conf.log[:file].inspect} for further notices."
165
-
166
- settings = {
167
- :host => Picnic::Conf.bind_address || "0.0.0.0",
168
- :log_file => Picnic::Conf.log[:file],
169
- :cwd => $APP_PATH
170
- }
171
-
172
- begin
173
- # need to close all IOs before daemonizing
174
- $LOG.close if $DAEMONIZE
175
-
176
- public_dirs = Picnic::Conf.public_dirs || Picnic::Conf.public_dir
177
- public_dirs = [public_dirs] unless
178
- public_dirs.kind_of? Array || public_dirs.nil?
179
-
180
- begin
181
- app_mod = self
182
- mongrel = Mongrel::Configurator.new settings do
183
- daemonize :log_file => Picnic::Conf.log[:file], :cwd => $APP_PATH if $DAEMONIZE
184
- app_mod.init_logger
185
- #app_mod.init_db_logger
186
- listener :port => Picnic::Conf.port do
187
- uri Picnic::Conf.uri_path, :handler => Mongrel::Camping::CampingHandler.new(app_mod)
188
-
189
- if public_dirs
190
- public_dirs.each do |d|
191
- dir = d[:dir]
192
- path = "#{Picnic::Conf.uri_path}/#{d[:path]}".gsub(/\/\/+/,'/')
193
- $LOG.debug("Mounting public directory #{dir.inspect} to path #{path.inspect}.")
194
- uri(path, :handler => Mongrel::DirHandler.new(dir))
195
- end
196
- end
197
-
198
- setup_signals
199
- end
200
- end
201
- rescue Errno::EADDRINUSE
202
- exit 1
203
- end
204
-
205
-
206
- mongrel.run
207
-
208
- if $DAEMONIZE && $PID_FILE
209
- write_pid_file
210
- unless File.exists? $PID_FILE
211
- $LOG.error "#{self} could not start because pid file #{$PID_FILE.inspect} could not be created."
212
- exit 1
213
- end
214
- end
215
-
216
- puts "\n** #{self} is running at http://#{ENV['HOSTNAME'] || 'localhost'}:#{Picnic::Conf.port}#{Picnic::Conf.uri_path} and logging to '#{Picnic::Conf.log[:file]}'"
217
-
218
-
219
- self.prestart if self.respond_to? :prestart
220
- mongrel.join
221
-
222
- clear_pid_file
223
-
224
- if mongrel.needs_restart
225
- $LOG.info "#{self} is restarting..."
226
- puts "\n** #{self} is restarting..."
227
- else
228
- $LOG.info "#{self} is stopped."
229
- puts "\n** #{self} is stopped (#{Time.now})"
230
- end
231
-
232
- end while mongrel.needs_restart
233
- end
234
-
235
- # Theoretically this should launch your Picnic app as a FastCGI process.
236
- # I say "theoretically" because this has never actually been tested.
237
- def fastcgi
238
- require 'camping/fastcgi'
239
- Dir.chdir('.')
240
-
241
- self.create
242
- Camping::FastCGI.start(self)
243
- end
244
-
245
- # Theoretically this should launch your Picnic app as a CGI process.
246
- # I say "theoretically" because this has never actually been tested.
247
- def cgi
248
- self.create
249
- puts self.run
250
- end
251
-
252
- private
253
- # Prints an error message to stderr if the log file is not writable.
254
- def check_log_writable
255
- log_file = Picnic::Conf.log['file']
256
- begin
257
- f = open(log_file, 'a')
258
- rescue
259
- $stderr.puts "Couldn't write to log file at '#{log_file}' (#{$!})."
260
- exit 1
261
- end
262
- f.close
263
- end
264
-
265
- # Prints an error message to stderr if the pid file is not writable.
266
- def check_pid_writable
267
- $LOG.debug "Checking if pid file #{$PID_FILE.inspect} is writable"
268
- begin
269
- f = open($PID_FILE, 'w')
270
- rescue
271
- $stderr.puts "Couldn't write to log at #{$PID_FILE.inspect} (#{$!})."
272
- exit 1
273
- end
274
- f.close
275
- end
276
-
277
- # Writes the pid file.
278
- def write_pid_file
279
- $LOG.debug "Writing pid #{Process.pid.inspect} to pid file #{$PID_FILE.inspect}"
280
- open($PID_FILE, "w") { |file| file.write(Process.pid) }
281
- end
282
-
283
- # Removes the pid file.
284
- def clear_pid_file
285
- if $PID_FILE && File.exists?($PID_FILE)
286
- $LOG.debug "Clearing pid file #{$PID_FILE.inspect}"
287
- File.unlink $PID_FILE
288
- end
289
- end
290
-
291
- end
292
- end