Was fuer Twitter zu lang ist

Jan Riethmayer

Dec 05

Alles nochma von 'scratch'

blog

Sat Dec 05 18:17:17 +0100 2009

Hab mir gestern Nacht ne neue Blogging App zurechtgeschustert um Sinatra auszuprobiern.

Find Sinatra mal echt geil. Hier mal die Logik, ohne Views: (hammer oder?)

require 'rubygems'
require 'sinatra'
require 'chronic'

$LOAD_PATH.unshift File.dirname(__FILE__) + '/vendor/sequel'
require 'sequel'

configure do
  Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://blog.db')

  require 'ostruct'
  Blog = OpenStruct.new(
    :title => 'Was fuer Twitter zu lang ist',
    :author => 'Jan Riethmayer',
    :url_base => 'http://riethmayer.de/',
    :admin_password => 'filtered',
    :admin_cookie_key => 'riethmayer_admin',
    :admin_cookie_value => 'filtered',
    :disqus_shortname => 'riethmayer'
  )
end

error do
  e = request.env['sinatra.error']
  puts e.to_s
  puts e.backtrace.join("\n")
  "Anwendungsfehler"
end

$LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib')
require 'post'

helpers do
  def admin?
    request.cookies[Blog.admin_cookie_key] == Blog.admin_cookie_value
  end

  def auth
    stop [ 401, 'Nicht erlaubt.' ] unless admin?
  end
end

layout 'layout'

### Public

get '/' do
  posts = Post.filter(:published_at <= Time.now).reverse_order(:created_at).limit(20)
  erb :index, :locals => { :posts => posts }, :layout => false
end

get '/past/:year/:month/:day/:slug/' do
  post = Post.filter(:slug => params[:slug]).first
  stop [ 404, "Page not found" ] unless post
  @title = post.title
  erb :post, :locals => { :post => post }, :layout => false
end

get '/past/:year/:month/:day/:slug' do
  redirect "/past/#{params[:year]}/#{params[:month]}/#{params[:day]}/#{params[:slug]}/", 301
end

get '/past' do
  posts = Post.reverse_order(:created_at)
  @title = "Archiv"
  erb :archive, :locals => { :posts => posts }
end

get '/past/tags/:tag' do
  tag = params[:tag]
  posts = Post.filter(:tags.like("%#{tag}%")).reverse_order(:created_at).limit(30)
  @title = "Posts tagged #{tag}"
  erb :tagged, :locals => { :posts => posts, :tag => tag }
end

get '/feed' do
  @posts = Post.reverse_order(:created_at).limit(20)
  content_type 'application/atom+xml', :charset => 'utf-8'
  builder :feed
end

get '/rss' do
  redirect '/feed', 301
end

### Admin

get '/auth' do
  erb :auth
end

post '/auth' do
  set_cookie(Blog.admin_cookie_key, Blog.admin_cookie_value) if params[:password] == Blog.admin_password
  redirect '/'
end

get '/posts/new' do
  auth
  erb :edit, :locals => { :post => Post.new, :url => '/posts' }
end

post '/posts' do
  auth
  published_at = Chronic.parse(params[:published_at]) || Time.now
  post = Post.new :title => params[:title], 
  :tags => params[:tags], 
  :body => params[:body], 
  :created_at => Time.now, 
  :published_at => published_at, 
  :slug => Post.make_slug(params[:title])
  post.save
  redirect post.url
end

get '/past/:year/:month/:day/:slug/edit' do
  auth
  post = Post.filter(:slug => params[:slug]).first
  stop [ 404, "Page not found" ] unless post
  erb :edit, :locals => { :post => post, :url => post.url }
end

post '/past/:year/:month/:day/:slug/' do
  auth
  post = Post.filter(:slug => params[:slug]).first
  stop [ 404, "Seite nicht gefunden." ] unless post
  post.title = params[:title]
  post.tags = params[:tags]
  post.body = params[:body]
  post.save
  redirect post.url
end

# this code is based on Adam Wiggins scanty blog.
blog comments powered by Disqus
(cc) 2009 Jan Riethmayer