Skip to content

Commit

Permalink
added caching and content type stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
danwrong committed Apr 15, 2009
1 parent e59c110 commit cb53791
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 13 deletions.
9 changes: 9 additions & 0 deletions assets/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,14 @@ require 'evil'

Evil.app_root = File.expand_path(File.dirname(__FILE__))

unless Evil.heroku?
require 'rack/cache'

use Rack::Cache,
:verbose => true,
:metastore => "file:#{Evil.app_root}/cache/meta",
:entitystore => "file:#{Evil.app_root}/cache/entity"
end

use Rack::Session::Cookie, :secret => '__SECRET__', :key => '_evil'
run Evil::Application
4 changes: 4 additions & 0 deletions lib/evil.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ module Setup

class << self
attr_accessor :gem_root, :app_root

def heroku?
Object.const_defined?(:Heroku)
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/evil/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ class Application < Sinatra::Base
require_whitelisted_openid(/^\/admin/)
load_template_routes

get '/openid/login' do
get '/admin/openid/login' do
attempt_openid_authentication do |identity_url|
session[:identity_url] = identity_url
redirect session[:destination_url]
end
end

post '/openid/logout' do
post '/admin/openid/logout' do
session[:identity_url] = nil
redirect '/admin'
end
Expand Down
11 changes: 8 additions & 3 deletions lib/evil/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ def reload!
end

def connect_to_database
ActiveRecord::Base.establish_connection :adapter => 'sqlite3',
:database => File.join(Evil.app_root, 'evil.db')
if Evil.heroku?
dbconfig = YAML.load(File.read(File.join(Evil.app_root, 'config/database.yml')))
ActiveRecord::Base.establish_connection dbconfig['production']
else
ActiveRecord::Base.establish_connection :adapter => 'sqlite3',
:database => File.join(Evil.app_root, 'evil.db')
end
end

def load_template_routes
Expand All @@ -47,7 +52,7 @@ def load_all_plugins

def require_whitelisted_openid(pattern)
before do
if request.path_info =~ pattern
if request.path_info =~ pattern && !params[:openid_url]
unless whitelisted_openid?
session[:destination_url] = request.url
not_authorized
Expand Down
11 changes: 8 additions & 3 deletions lib/evil/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ module Helpers
def serve(template)
initialize_plugins

template = Liquid::Template.parse(template.reload.source)
liquid_template = Liquid::Template.parse(template.reload.source)

content_type 'text/html', :charset => 'utf-8'
template.render 'params' => params
content_type(template.content_type || 'text/html', :charset => template.encoding || 'utf-8')

if template.ttl
headers 'Cache-Control' => "public, max-age=#{template.ttl}"
end

liquid_template.render 'params' => params
end

def partial(template, options={})
Expand Down
2 changes: 1 addition & 1 deletion lib/evil/setup/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Migration
def create_evil_tables
create_table :evil_templates do |t|
t.integer :ttl, :position
t.string :title, :route
t.string :title, :route, :content_type, :encoding
t.text :source
t.timestamps
end unless Evil::Models::Template.table_exists?
Expand Down
10 changes: 10 additions & 0 deletions test/example/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ require 'evil'
Evil.app_root = File.expand_path(File.dirname(__FILE__))

use Rack::Session::Cookie, :secret => '4424427c834b0c67f38f40a977315480b75ce94b', :key => '_evil'

unless Evil.heroku?
require 'rack/cache'

use Rack::Cache,
:verbose => true,
:metastore => "file:#{Evil.app_root}/cache/meta",
:entitystore => "file:#{Evil.app_root}/cache/entity"
end

run Evil::Application
Binary file modified test/example/evil.db
Binary file not shown.
6 changes: 5 additions & 1 deletion test/example/public/stylesheets/evil.css
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,16 @@ form p {
margin: 1em 0;
}

form input.text, form textarea {
form input.text, form textarea, form input.short {
width: 100%;
font-family: Verdana, Helvetica, Arial, sans-serif;
font-size: 1em;
}

form input.short {
width: 30%;
}

form div.errors {
border: 1px solid red;
font-size: 0.8em;
Expand Down
Empty file removed test/example/tmp/restart.txt
Empty file.
2 changes: 1 addition & 1 deletion views/_banner.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#banner
%a{ :href => '/admin' }
%img#logo{ :src => '/images/logo.png', :alt => 'Evil' }
%form{ :method => 'post', :action => '/openid/logout', :id => 'logout' }
%form{ :method => 'post', :action => '/admin/openid/logout', :id => 'logout' }
Logged in as
%em= session[:identity_url]
%input.submit{ :type => 'submit', :value => 'Log out' }
2 changes: 1 addition & 1 deletion views/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
%tr{ :class => ('alt' if i % 2 == 1) }
%td= template.title
%td= template.route
%td= template.ttl
%td= template.route.empty? ? 'N/A' : template.ttl
%td.tools
%a{ :href => "/admin/templates/#{template.id}"} Edit
%tfoot
Expand Down
2 changes: 1 addition & 1 deletion views/login.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%form{ :action => '/openid/login', :id => 'login' }
%form{ :action => '/admin/openid/login', :id => 'login' }
%p
%img{ :src => '/images/logo.png', :alt => 'Evil' }
%input#openid_url{ :name => 'openid_url' }
Expand Down
9 changes: 9 additions & 0 deletions views/templates/_fields.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@
%label{ :for => 'template_source' }
Source *
%textarea#template_source{ :name => 'template[source]' }= @template.source
%p
%label{ :for => 'template_ttl' }
Expires every (seconds)
%input#template_ttl.text{ :name => 'template[ttl]', :value => @template.ttl || 600 }
%p
%label{ :for => 'template_content_type' }
Content Type
%input#template_content_type.short{ :name => 'template[content_type]', :value => @template.content_type || 'text/html' }
%input#template_encoding.short{ :name => 'template[encoding]', :value => @template.encoding || 'utf-8' }
%p
%input.submit{ :value => 'Save', :type => 'submit' }

0 comments on commit cb53791

Please sign in to comment.