Created
August 22, 2009 07:26
-
-
Save okkez/172696 to your computer and use it in GitHub Desktop.
一行掲示板
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'rack' | |
require 'activerecord' | |
require 'erb' | |
class LineBbs | |
include ERB::Util | |
def call(env) | |
request = Rack::Request.new(env) | |
case | |
when request.get? | |
response = get(request) | |
when request.post? | |
response = post(request) | |
else | |
response = Rack::Response.new{|res| | |
res['Content-Type']= 'text/html;charset=utf-8' | |
res.status = '405' | |
res.write "405 not allowed\n" | |
} | |
end | |
response.finish | |
end | |
def get(request) | |
comments = Comment.all(:order => "id DESC") | |
Rack::Response.new{|res| | |
res['Content-Type']= 'text/html;charset=utf-8' | |
res.status = '200' | |
res.write ERB.new(::TEMPLATE, nil, '-').result(binding) | |
} | |
end | |
def post(request) | |
Comment.new{|c| | |
c.name = request.params['name'] | |
c.comment = request.params['comment'] | |
}.save! | |
Rack::Response.new{|res| | |
res['Content-Type']= 'text/html;charset=utf-8' | |
res.status = '302' | |
res['Location'] = "#{request.scheme}://#{request.host}:#{request.port}/" | |
} | |
end | |
end | |
ActiveRecord::Base.establish_connection( | |
:adapter => 'sqlite3', | |
:database => 'linebbs.sqlite3' | |
) | |
ActiveRecord::Base.logger = Logger.new(STDERR) | |
ActiveRecord::Base.connection.execute <<SQL | |
CREATE TABLE IF NOT EXISTS "comments" ( | |
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | |
"name" varchar(255) DEFAULT NULL NULL, | |
"comment" varchar(255) DEFAULT NULL NULL, | |
"created_at" datetime DEFAULT NULL NULL, | |
"updated_at" datetime DEFAULT NULL NULL | |
); | |
SQL | |
class Comment < ActiveRecord::Base | |
end | |
TEMPLATE=<<EOD | |
<html> | |
<head> | |
<title>LineBBS</title> | |
</head> | |
<body> | |
<h3>LineBBS</h3> | |
<div class="form"> | |
<form action="/" method="post"> | |
<input type="text" name="name" size="20" /> : | |
<input type="text" name="comment" size="60" /> | |
<input type="submit" name="submit" /> | |
</form> | |
</div> | |
<hr /> | |
<div class="comments"> | |
<%- comments.each do |c| -%> | |
<p> | |
<%=h c.created_at.strftime('%Y-%m-%d %H:%M:%S') %> : | |
<%=h c.name %> : <%=h c.comment %> | |
</p> | |
<%- end -%> | |
</div> | |
</body> | |
</html> | |
EOD | |
if __FILE__ == $0 | |
app = Rack::Builder.new{ | |
use Rack::Lint | |
use Rack::ShowExceptions | |
use Rack::Reloader | |
use Rack::CommonLogger | |
use Rack::Static | |
run LineBbs.new | |
}.to_app | |
options = { :Port => 9292, :Host => '0.0.0.0', :AccessLog => [] } | |
Rack::Handler::Mongrel.run(app, options) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'linebbs' | |
use Rack::Lint | |
use Rack::ShowExceptions | |
use Rack::Reloader | |
use Rack::CommonLogger | |
use Rack::Static | |
run LineBbs.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment