Professional Documents
Culture Documents
Corey Donohoe(cdonohoe@engineyard.com)
helpers do
def greeting
haml :greeting
end
end
template :greeting do
"%h2 Hello World"
end
get('/') do
greeting
end
Hello World
def localhost(suffix)
port_part = case request.scheme
when "http"
request.port == 80 ? "" : ":#{request.port}"
when "https"
request.port == 443 ? "" : ":#{request.port}"
end
"#{request.scheme}://#{request.host}#{port_part}#{suffix}"
end
end
get '/baz' do
"baz is set to " + options.baz
end
register Sinatra::Hancock::Defaults
register Sinatra::Hancock::Sessions
register Sinatra::Hancock::Users
register Sinatra::Hancock::OpenIDServer
end
end
module Helpers
def session_user
session['user_id'].nil? ? nil : ::Hancock::User.get(session['user_id'])
end
end
def self.registered(app)
app.helpers(Sinatra::Hancock::Sessions::Helpers)
app.template(:unauthenticated) { sessions_template ('unauthenticated') }
app.get '/sso/login' do
ensure_authenticated
end
app.post '/sso/login' do
@user = ::Hancock::User.authenticate(params['email'], params['password'])
if @user
session['user_id'] = @user.id
end
ensure_authenticated
redirect session['return_to'] || '/'
end
app.get '/sso/logout' do
session.clear
redirect '/'
end
end
end
end
end
register Sinatra::Hancock::Defaults
register Sinatra::Hancock::Sessions
register Sinatra::Hancock::Users
register Sinatra::Hancock::OpenIDServer
end
end
# vim:filetype=ruby
use Rack::Static, :urls => ["/css", "/img", "/js"], :root => "public"
map "/gateway/" do
use EySso
run Gateway::App
end
map "/migration/" do
use Rack::ShowExceptions
if ENV["RACK_ENV"] == "production"
use EySso do |sso|
sso.only_staff!
end
end
run Migration::App
end
map "/" do
app = lambda do |env|
[404, {"Content-Type" => "text/plain", "Content-Length" => "9"}, ["Not found"]]
end
run app
end
use Rack::Static, :urls => ["/css", "/img", "/js"], :root => "public"
map "/gateway/" do
use EySso
run Gateway::App
end
map "/migration/" do
use Rack::ShowExceptions
if ENV["RACK_ENV"] == "production"
use EySso do |sso|
sso.only_staff!
end
end
run Migration::App
end
map "/" do
app = lambda do |env|
[404, {"Content-Type" => "text/plain", "Content-Length" => "9"}, ["Not found"]]
end
run app
end
DataMapper.setup(:default, "mysql://atmos:s3cr3t@localhost/twitrsvp_production")
ENV['TWIT_RSVP_READKEY'] = '4the'
ENV['TWIT_RSVP_READSECRET'] = 'lulz'
include ::TwitRSVP
if __FILE__ == $0
IRB.start(__FILE__)
else
# check -e option
if /^-e$/ =~ $0
IRB.start(__FILE__)
else
IRB.setup(__FILE__)
end
end
def app
MyApp.new
end
def test_redirect_logged_in_users_to_dashboard
authorize "bryan", "secret"
get "/"
follow_redirect!
✤ http://github.com/chneukirchen/rack
✤ http://github.com/atmos/hancock
✤ http://github.com/atmos/hancock-client
✤ http://github.com/atmos/hancock-client-rails
✤ http://github.com/brynary/rack-bug
✤ http://github.com/foca/integrity
DataMapper.setup(:default, "sqlite3:///#{Dir.pwd}/development.db")
Sinatra::Mailer.config = {
:host => 'smtp.example.com',
:port => '25',
:user => 'sso',
:pass => 'lolerskates',
:auth => :plain # :plain, :login, :cram_md5, the default is no auth
:domain => "example.com" # the HELO domain provided by the client to the server
}
run Hancock::App
config.ru
Rails::Initializer.run do |config|
config.gem 'hancock', :lib => 'hancock'
config.middleware.use 'Hancock::Client'
# all your other normal stuff
end