puma 0.8.2-java
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of puma might be problematic. Click here for more details.
- data/.gemtest +0 -0
- data/COPYING +55 -0
- data/Gemfile +6 -0
- data/History.txt +69 -0
- data/LICENSE +26 -0
- data/Manifest.txt +60 -0
- data/README.md +60 -0
- data/Rakefile +12 -0
- data/TODO +5 -0
- data/bin/puma +15 -0
- data/examples/builder.rb +29 -0
- data/examples/camping/README +3 -0
- data/examples/camping/blog.rb +294 -0
- data/examples/camping/tepee.rb +149 -0
- data/examples/httpd.conf +474 -0
- data/examples/mime.yaml +3 -0
- data/examples/mongrel.conf +9 -0
- data/examples/monitrc +57 -0
- data/examples/random_thrash.rb +19 -0
- data/examples/simpletest.rb +52 -0
- data/examples/webrick_compare.rb +20 -0
- data/ext/puma_http11/PumaHttp11Service.java +13 -0
- data/ext/puma_http11/ext_help.h +15 -0
- data/ext/puma_http11/extconf.rb +5 -0
- data/ext/puma_http11/http11_parser.c +1225 -0
- data/ext/puma_http11/http11_parser.h +63 -0
- data/ext/puma_http11/http11_parser.java.rl +161 -0
- data/ext/puma_http11/http11_parser.rl +146 -0
- data/ext/puma_http11/http11_parser_common.rl +54 -0
- data/ext/puma_http11/org/jruby/puma/Http11.java +225 -0
- data/ext/puma_http11/org/jruby/puma/Http11Parser.java +488 -0
- data/ext/puma_http11/puma_http11.c +482 -0
- data/lib/puma.rb +18 -0
- data/lib/puma/cli.rb +164 -0
- data/lib/puma/const.rb +132 -0
- data/lib/puma/events.rb +36 -0
- data/lib/puma/gems.rb +20 -0
- data/lib/puma/mime_types.yml +616 -0
- data/lib/puma/rack_patch.rb +22 -0
- data/lib/puma/server.rb +429 -0
- data/lib/puma/thread_pool.rb +95 -0
- data/lib/puma/utils.rb +44 -0
- data/lib/puma_http11.jar +0 -0
- data/lib/rack/handler/puma.rb +48 -0
- data/puma.gemspec +40 -0
- data/tasks/gem.rake +24 -0
- data/tasks/java.rake +12 -0
- data/tasks/native.rake +36 -0
- data/tasks/ragel.rake +24 -0
- data/test/lobster.ru +4 -0
- data/test/mime.yaml +3 -0
- data/test/test_cli.rb +19 -0
- data/test/test_http10.rb +27 -0
- data/test/test_http11.rb +151 -0
- data/test/test_persistent.rb +205 -0
- data/test/test_rack_handler.rb +10 -0
- data/test/test_rack_server.rb +122 -0
- data/test/test_thread_pool.rb +102 -0
- data/test/test_unix_socket.rb +37 -0
- data/test/test_ws.rb +97 -0
- data/test/testhelp.rb +41 -0
- data/tools/trickletest.rb +45 -0
- metadata +163 -0
data/.gemtest
ADDED
File without changes
|
data/COPYING
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
Mongrel Web Server (Mongrel) is copyrighted free software by Zed A. Shaw
|
2
|
+
<zedshaw at zedshaw dot com> You can redistribute it and/or modify it under
|
3
|
+
either the terms of the GPL or the conditions below:
|
4
|
+
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
6
|
+
software without restriction, provided that you duplicate all of the
|
7
|
+
original copyright notices and associated disclaimers.
|
8
|
+
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
10
|
+
you do at least ONE of the following:
|
11
|
+
|
12
|
+
a) place your modifications in the Public Domain or otherwise make them
|
13
|
+
Freely Available, such as by posting said modifications to Usenet or an
|
14
|
+
equivalent medium, or by allowing the author to include your
|
15
|
+
modifications in the software.
|
16
|
+
|
17
|
+
b) use the modified software only within your corporation or
|
18
|
+
organization.
|
19
|
+
|
20
|
+
c) rename any non-standard executables so the names do not conflict with
|
21
|
+
standard executables, which must also be provided.
|
22
|
+
|
23
|
+
d) make other distribution arrangements with the author.
|
24
|
+
|
25
|
+
3. You may distribute the software in object code or executable
|
26
|
+
form, provided that you do at least ONE of the following:
|
27
|
+
|
28
|
+
a) distribute the executables and library files of the software,
|
29
|
+
together with instructions (in the manual page or equivalent) on where
|
30
|
+
to get the original distribution.
|
31
|
+
|
32
|
+
b) accompany the distribution with the machine-readable source of the
|
33
|
+
software.
|
34
|
+
|
35
|
+
c) give non-standard executables non-standard names, with
|
36
|
+
instructions on where to get the original software distribution.
|
37
|
+
|
38
|
+
d) make other distribution arrangements with the author.
|
39
|
+
|
40
|
+
4. You may modify and include the part of the software into any other
|
41
|
+
software (possibly commercial). But some files in the distribution
|
42
|
+
are not written by the author, so that they are not under this terms.
|
43
|
+
|
44
|
+
5. The scripts and library files supplied as input to or produced as
|
45
|
+
output from the software do not automatically fall under the
|
46
|
+
copyright of the software, but belong to whomever generated them,
|
47
|
+
and may be sold commercially, and may be aggregated with this
|
48
|
+
software.
|
49
|
+
|
50
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
51
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
52
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
53
|
+
PURPOSE.
|
54
|
+
|
55
|
+
|
data/Gemfile
ADDED
data/History.txt
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
=== 1.2.0.beta.1 / 2010-07-??
|
2
|
+
|
3
|
+
Improvements:
|
4
|
+
|
5
|
+
* Ruby 1.9 early compatbility: Merged commits form Eric Wong and Matt Aimonetti.
|
6
|
+
* Better RubyGems support thanks to added env she-bang to mongrel_rails executable.
|
7
|
+
* Smartly load http11 extension using fat-binary approach.
|
8
|
+
* Better detection of Windows platform (usage of RbConfig::CONFIG instead of RUBY_PLATFORM)
|
9
|
+
|
10
|
+
Bugfixes:
|
11
|
+
|
12
|
+
* Fixed proper version reporting under 1.9
|
13
|
+
* Consider MinGW as valid Windows platform.
|
14
|
+
* Properly drop PID when daemonizing (EngineYard patched 1.1.5.1).
|
15
|
+
|
16
|
+
Reorganization:
|
17
|
+
|
18
|
+
* Rake task project reorganization and reformat using Hoe.
|
19
|
+
* Compile extension using rake-compiler
|
20
|
+
* Upgraded http11_parser to work with Ragel 6.2
|
21
|
+
* Deprecated obsolete Windows service example scripts (from 2006!)
|
22
|
+
* Relocate Http11 JRuby extension to share http11 C extension folder and name.
|
23
|
+
|
24
|
+
=== 1.1.5 / 2008-05-22
|
25
|
+
|
26
|
+
* Fix bug where num_processors is not actually set from mongrel_rails.
|
27
|
+
|
28
|
+
|
29
|
+
=== 1.1.4 / 2008-02-29
|
30
|
+
|
31
|
+
* Fix camping handler. Correct treatment of @throttle parameter.
|
32
|
+
|
33
|
+
|
34
|
+
=== 1.1.3 / 2007-12-29
|
35
|
+
|
36
|
+
* Fix security flaw of DirHandler; reported on mailing list.
|
37
|
+
|
38
|
+
|
39
|
+
=== 1.1.2 / 2007-12-15
|
40
|
+
|
41
|
+
* Fix worker termination bug; fix JRuby 1.0.3 load order issue; fix require issue on systems without Rubygems.
|
42
|
+
|
43
|
+
|
44
|
+
=== 1.1.1 / 2007-11-12
|
45
|
+
|
46
|
+
* Fix mongrel_rails restart bug; fix bug with Rack status codes.
|
47
|
+
|
48
|
+
|
49
|
+
=== 1.1 / 2007-11-01
|
50
|
+
|
51
|
+
* Pure Ruby URIClassifier.
|
52
|
+
* More modular architecture.
|
53
|
+
* JRuby support.
|
54
|
+
* Move C URIClassifier into mongrel_experimental project.
|
55
|
+
|
56
|
+
|
57
|
+
=== 1.0.4 / 2007-10-29
|
58
|
+
|
59
|
+
* Backport fixes for versioning inconsistency, mongrel_rails bug, and DirHandler bug.
|
60
|
+
|
61
|
+
|
62
|
+
=== 1.0.3
|
63
|
+
|
64
|
+
* Fix user-switching bug; make people upgrade to the latest from the RC.
|
65
|
+
|
66
|
+
|
67
|
+
=== 1.0.2
|
68
|
+
|
69
|
+
* Signed gem; many minor bugfixes and patches.
|
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Some code copyright (c) 2005, Zed Shaw
|
2
|
+
Copyright (c) 2011, Evan Phoenix
|
3
|
+
All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
7
|
+
|
8
|
+
* Redistributions of source code must retain the above copyright notice, this
|
9
|
+
list of conditions and the following disclaimer.
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
* Neither the name of the Evan Phoenix nor the names of its contributors
|
14
|
+
may be used to endorse or promote products derived from this software
|
15
|
+
without specific prior written permission.
|
16
|
+
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
18
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
19
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
21
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
23
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
24
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
25
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
26
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
COPYING
|
2
|
+
Gemfile
|
3
|
+
History.txt
|
4
|
+
LICENSE
|
5
|
+
Manifest.txt
|
6
|
+
README.md
|
7
|
+
Rakefile
|
8
|
+
TODO
|
9
|
+
bin/puma
|
10
|
+
examples/builder.rb
|
11
|
+
examples/camping/README
|
12
|
+
examples/camping/blog.rb
|
13
|
+
examples/camping/tepee.rb
|
14
|
+
examples/httpd.conf
|
15
|
+
examples/mime.yaml
|
16
|
+
examples/mongrel.conf
|
17
|
+
examples/monitrc
|
18
|
+
examples/random_thrash.rb
|
19
|
+
examples/simpletest.rb
|
20
|
+
examples/webrick_compare.rb
|
21
|
+
ext/puma_http11/PumaHttp11Service.java
|
22
|
+
ext/puma_http11/ext_help.h
|
23
|
+
ext/puma_http11/extconf.rb
|
24
|
+
ext/puma_http11/http11_parser.c
|
25
|
+
ext/puma_http11/http11_parser.h
|
26
|
+
ext/puma_http11/http11_parser.java.rl
|
27
|
+
ext/puma_http11/http11_parser.rl
|
28
|
+
ext/puma_http11/http11_parser_common.rl
|
29
|
+
ext/puma_http11/org/jruby/puma/Http11.java
|
30
|
+
ext/puma_http11/org/jruby/puma/Http11Parser.java
|
31
|
+
ext/puma_http11/puma_http11.c
|
32
|
+
lib/puma.rb
|
33
|
+
lib/puma/cli.rb
|
34
|
+
lib/puma/const.rb
|
35
|
+
lib/puma/events.rb
|
36
|
+
lib/puma/gems.rb
|
37
|
+
lib/puma/mime_types.yml
|
38
|
+
lib/puma/rack_patch.rb
|
39
|
+
lib/puma/server.rb
|
40
|
+
lib/puma/thread_pool.rb
|
41
|
+
lib/puma/utils.rb
|
42
|
+
lib/rack/handler/puma.rb
|
43
|
+
puma.gemspec
|
44
|
+
tasks/gem.rake
|
45
|
+
tasks/java.rake
|
46
|
+
tasks/native.rake
|
47
|
+
tasks/ragel.rake
|
48
|
+
test/lobster.ru
|
49
|
+
test/mime.yaml
|
50
|
+
test/test_cli.rb
|
51
|
+
test/test_http10.rb
|
52
|
+
test/test_http11.rb
|
53
|
+
test/test_persistent.rb
|
54
|
+
test/test_rack_handler.rb
|
55
|
+
test/test_rack_server.rb
|
56
|
+
test/test_thread_pool.rb
|
57
|
+
test/test_unix_socket.rb
|
58
|
+
test/test_ws.rb
|
59
|
+
test/testhelp.rb
|
60
|
+
tools/trickletest.rb
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Puma: A Ruby Web Server Built For Concurrency
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
Puma is a small library that provides a very fast and concurrent HTTP 1.1 server for Ruby web applications. It is designed for running rack apps only.
|
6
|
+
|
7
|
+
What makes Puma so fast is the careful use of an Ragel extension to provide fast, accurate HTTP 1.1 protocol parsing. This makes the server scream without too many portability issues.
|
8
|
+
|
9
|
+
## License
|
10
|
+
|
11
|
+
Puma is copyright 2011 Evan Phoenix and contributors. It is licensed under the BSD license. See the include LICENSE file for details.
|
12
|
+
|
13
|
+
## Quick Start
|
14
|
+
|
15
|
+
The easiest way to get started with Puma is to install it via RubyGems and then run a Ruby on Rails application. You can do this easily:
|
16
|
+
|
17
|
+
$ gem install puma
|
18
|
+
|
19
|
+
Now you should have the puma command available in your PATH, so just do the following:
|
20
|
+
|
21
|
+
$ puma app.ru
|
22
|
+
|
23
|
+
## Install
|
24
|
+
|
25
|
+
$ gem install puma
|
26
|
+
|
27
|
+
## Advanced Setup
|
28
|
+
|
29
|
+
### Sinatra
|
30
|
+
|
31
|
+
You can run your Sinatra application with Puma from the command line like this:
|
32
|
+
|
33
|
+
$ ruby app.rb -s Puma
|
34
|
+
|
35
|
+
Or you can configure your application to always use Puma:
|
36
|
+
|
37
|
+
require 'sinatra'
|
38
|
+
configure { set :server, :puma }
|
39
|
+
|
40
|
+
If you use Bundler, make sure you add Puma to your Gemfile (see below).
|
41
|
+
|
42
|
+
### Rails
|
43
|
+
|
44
|
+
First, make sure Puma is in your Gemfile:
|
45
|
+
|
46
|
+
gem 'puma'
|
47
|
+
|
48
|
+
Then start your server with the `rails` command:
|
49
|
+
|
50
|
+
$ rails s puma
|
51
|
+
|
52
|
+
### Rackup
|
53
|
+
|
54
|
+
You can pass it as an option to `rackup`:
|
55
|
+
|
56
|
+
$ rackup -s puma
|
57
|
+
|
58
|
+
Alternatively, you can modify your `config.ru` to choose Puma by default, by adding the following as the first line:
|
59
|
+
|
60
|
+
#\ -s puma
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#
|
2
|
+
# NOTE: Keep this file clean.
|
3
|
+
# Add your customizations inside tasks directory.
|
4
|
+
# Thank You.
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
|
9
|
+
IS_JRUBY = defined?(RUBY_ENGINE) ? RUBY_ENGINE == "jruby" : false
|
10
|
+
|
11
|
+
# load rakefile extensions (tasks)
|
12
|
+
Dir['tasks/*.rake'].sort.each { |f| load f }
|
data/TODO
ADDED
data/bin/puma
ADDED
data/examples/builder.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'mongrel'
|
2
|
+
|
3
|
+
class TestPlugin < GemPlugin::Plugin "/handlers"
|
4
|
+
include Mongrel::HttpHandlerPlugin
|
5
|
+
|
6
|
+
def process(request, response)
|
7
|
+
STDERR.puts "My options are: #{options.inspect}"
|
8
|
+
STDERR.puts "Request Was:"
|
9
|
+
STDERR.puts request.params.to_yaml
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
config = Mongrel::Configurator.new :host => "127.0.0.1" do
|
14
|
+
load_plugins :includes => ["mongrel"], :excludes => ["rails"]
|
15
|
+
daemonize :cwd => Dir.pwd, :log_file => "mongrel.log", :pid_file => "mongrel.pid"
|
16
|
+
|
17
|
+
listener :port => 3000 do
|
18
|
+
uri "/app", :handler => plugin("/handlers/testplugin", :test => "that")
|
19
|
+
uri "/app", :handler => Mongrel::DirHandler.new(".")
|
20
|
+
load_plugins :includes => ["mongrel", "rails"]
|
21
|
+
end
|
22
|
+
|
23
|
+
trap("INT") { stop }
|
24
|
+
run
|
25
|
+
end
|
26
|
+
|
27
|
+
config.join
|
28
|
+
|
29
|
+
|
@@ -0,0 +1,294 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$:.unshift File.dirname(__FILE__) + "/../../lib"
|
4
|
+
require 'rubygems'
|
5
|
+
require_gem 'camping', '>=1.4'
|
6
|
+
require 'camping/session'
|
7
|
+
|
8
|
+
Camping.goes :Blog
|
9
|
+
|
10
|
+
module Blog
|
11
|
+
include Camping::Session
|
12
|
+
end
|
13
|
+
|
14
|
+
module Blog::Models
|
15
|
+
def self.schema(&block)
|
16
|
+
@@schema = block if block_given?
|
17
|
+
@@schema
|
18
|
+
end
|
19
|
+
|
20
|
+
class Post < Base; belongs_to :user; end
|
21
|
+
class Comment < Base; belongs_to :user; end
|
22
|
+
class User < Base; end
|
23
|
+
end
|
24
|
+
|
25
|
+
Blog::Models.schema do
|
26
|
+
create_table :blog_posts, :force => true do |t|
|
27
|
+
t.column :id, :integer, :null => false
|
28
|
+
t.column :user_id, :integer, :null => false
|
29
|
+
t.column :title, :string, :limit => 255
|
30
|
+
t.column :body, :text
|
31
|
+
end
|
32
|
+
create_table :blog_users, :force => true do |t|
|
33
|
+
t.column :id, :integer, :null => false
|
34
|
+
t.column :username, :string
|
35
|
+
t.column :password, :string
|
36
|
+
end
|
37
|
+
create_table :blog_comments, :force => true do |t|
|
38
|
+
t.column :id, :integer, :null => false
|
39
|
+
t.column :post_id, :integer, :null => false
|
40
|
+
t.column :username, :string
|
41
|
+
t.column :body, :text
|
42
|
+
end
|
43
|
+
execute "INSERT INTO blog_users (username, password) VALUES ('admin', 'camping')"
|
44
|
+
end
|
45
|
+
|
46
|
+
module Blog::Controllers
|
47
|
+
class Index < R '/'
|
48
|
+
def get
|
49
|
+
@posts = Post.find :all
|
50
|
+
render :index
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Add
|
55
|
+
def get
|
56
|
+
unless @state.user_id.blank?
|
57
|
+
@user = User.find @state.user_id
|
58
|
+
@post = Post.new
|
59
|
+
end
|
60
|
+
render :add
|
61
|
+
end
|
62
|
+
def post
|
63
|
+
post = Post.create :title => input.post_title, :body => input.post_body,
|
64
|
+
:user_id => @state.user_id
|
65
|
+
redirect View, post
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class Info < R '/info/(\d+)', '/info/(\w+)/(\d+)', '/info', '/info/(\d+)/(\d+)/(\d+)/([\w-]+)'
|
70
|
+
def get(*args)
|
71
|
+
div do
|
72
|
+
code args.inspect; br; br
|
73
|
+
code ENV.inspect; br
|
74
|
+
code "Link: #{R(Info, 1, 2)}"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class View < R '/view/(\d+)'
|
80
|
+
def get post_id
|
81
|
+
@post = Post.find post_id
|
82
|
+
@comments = Models::Comment.find :all, :conditions => ['post_id = ?', post_id]
|
83
|
+
render :view
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
class Edit < R '/edit/(\d+)', '/edit'
|
88
|
+
def get post_id
|
89
|
+
unless @state.user_id.blank?
|
90
|
+
@user = User.find @state.user_id
|
91
|
+
end
|
92
|
+
@post = Post.find post_id
|
93
|
+
render :edit
|
94
|
+
end
|
95
|
+
|
96
|
+
def post
|
97
|
+
@post = Post.find input.post_id
|
98
|
+
@post.update_attributes :title => input.post_title, :body => input.post_body
|
99
|
+
redirect View, @post
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class Comment
|
104
|
+
def post
|
105
|
+
Models::Comment.create(:username => input.post_username,
|
106
|
+
:body => input.post_body, :post_id => input.post_id)
|
107
|
+
redirect View, input.post_id
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
class Login
|
112
|
+
def post
|
113
|
+
@user = User.find :first, :conditions => ['username = ? AND password = ?', input.username, input.password]
|
114
|
+
|
115
|
+
if @user
|
116
|
+
@login = 'login success !'
|
117
|
+
@state.user_id = @user.id
|
118
|
+
else
|
119
|
+
@login = 'wrong user name or password'
|
120
|
+
end
|
121
|
+
render :login
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
class Logout
|
126
|
+
def get
|
127
|
+
@state.user_id = nil
|
128
|
+
render :logout
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
class Style < R '/styles.css'
|
133
|
+
def get
|
134
|
+
@headers["Content-Type"] = "text/css; charset=utf-8"
|
135
|
+
@body = %{
|
136
|
+
body {
|
137
|
+
font-family: Utopia, Georga, serif;
|
138
|
+
}
|
139
|
+
h1.header {
|
140
|
+
background-color: #fef;
|
141
|
+
margin: 0; padding: 10px;
|
142
|
+
}
|
143
|
+
div.content {
|
144
|
+
padding: 10px;
|
145
|
+
}
|
146
|
+
}
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
module Blog::Views
|
152
|
+
|
153
|
+
def layout
|
154
|
+
html do
|
155
|
+
head do
|
156
|
+
title 'blog'
|
157
|
+
link :rel => 'stylesheet', :type => 'text/css',
|
158
|
+
:href => '/styles.css', :media => 'screen'
|
159
|
+
end
|
160
|
+
body do
|
161
|
+
h1.header { a 'blog', :href => R(Index) }
|
162
|
+
div.content do
|
163
|
+
self << yield
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def index
|
170
|
+
if @posts.empty?
|
171
|
+
p 'No posts found.'
|
172
|
+
p { a 'Add', :href => R(Add) }
|
173
|
+
else
|
174
|
+
for post in @posts
|
175
|
+
_post(post)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def login
|
181
|
+
p { b @login }
|
182
|
+
p { a 'Continue', :href => R(Add) }
|
183
|
+
end
|
184
|
+
|
185
|
+
def logout
|
186
|
+
p "You have been logged out."
|
187
|
+
p { a 'Continue', :href => R(Index) }
|
188
|
+
end
|
189
|
+
|
190
|
+
def add
|
191
|
+
if @user
|
192
|
+
_form(post, :action => R(Add))
|
193
|
+
else
|
194
|
+
_login
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
def edit
|
199
|
+
if @user
|
200
|
+
_form(post, :action => R(Edit))
|
201
|
+
else
|
202
|
+
_login
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def view
|
207
|
+
_post(post)
|
208
|
+
|
209
|
+
p "Comment for this post:"
|
210
|
+
for c in @comments
|
211
|
+
h1 c.username
|
212
|
+
p c.body
|
213
|
+
end
|
214
|
+
|
215
|
+
form :action => R(Comment), :method => 'post' do
|
216
|
+
label 'Name', :for => 'post_username'; br
|
217
|
+
input :name => 'post_username', :type => 'text'; br
|
218
|
+
label 'Comment', :for => 'post_body'; br
|
219
|
+
textarea :name => 'post_body' do; end; br
|
220
|
+
input :type => 'hidden', :name => 'post_id', :value => post.id
|
221
|
+
input :type => 'submit'
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
# partials
|
226
|
+
def _login
|
227
|
+
form :action => R(Login), :method => 'post' do
|
228
|
+
label 'Username', :for => 'username'; br
|
229
|
+
input :name => 'username', :type => 'text'; br
|
230
|
+
|
231
|
+
label 'Password', :for => 'password'; br
|
232
|
+
input :name => 'password', :type => 'text'; br
|
233
|
+
|
234
|
+
input :type => 'submit', :name => 'login', :value => 'Login'
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def _post(post)
|
239
|
+
h1 post.title
|
240
|
+
p post.body
|
241
|
+
p do
|
242
|
+
a "Edit", :href => R(Edit, post)
|
243
|
+
a "View", :href => R(View, post)
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
def _form(post, opts)
|
248
|
+
p do
|
249
|
+
text "You are logged in as #{@user.username} | "
|
250
|
+
a 'Logout', :href => R(Logout)
|
251
|
+
end
|
252
|
+
form({:method => 'post'}.merge(opts)) do
|
253
|
+
label 'Title', :for => 'post_title'; br
|
254
|
+
input :name => 'post_title', :type => 'text',
|
255
|
+
:value => post.title; br
|
256
|
+
|
257
|
+
label 'Body', :for => 'post_body'; br
|
258
|
+
textarea post.body, :name => 'post_body'; br
|
259
|
+
|
260
|
+
input :type => 'hidden', :name => 'post_id', :value => post.id
|
261
|
+
input :type => 'submit'
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
def Blog.create
|
267
|
+
Camping::Models::Session.create_schema
|
268
|
+
unless Blog::Models::Post.table_exists?
|
269
|
+
ActiveRecord::Schema.define(&Blog::Models.schema)
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
if __FILE__ == $0
|
274
|
+
require 'mongrel/camping'
|
275
|
+
|
276
|
+
Blog::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog.db'
|
277
|
+
Blog::Models::Base.logger = Logger.new('camping.log')
|
278
|
+
Blog::Models::Base.threaded_connections=false
|
279
|
+
Blog.create
|
280
|
+
|
281
|
+
# Use the Configurator as an example rather than Mongrel::Camping.start
|
282
|
+
config = Mongrel::Configurator.new :host => "0.0.0.0" do
|
283
|
+
listener :port => 3002 do
|
284
|
+
uri "/blog", :handler => Mongrel::Camping::CampingHandler.new(Blog)
|
285
|
+
uri "/favicon", :handler => Mongrel::Error404Handler.new("")
|
286
|
+
trap("INT") { stop }
|
287
|
+
run
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
puts "** Blog example is running at http://localhost:3002/blog"
|
292
|
+
puts "** Default username is `admin', password is `camping'"
|
293
|
+
config.join
|
294
|
+
end
|