Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 126 lines (101 sloc) 2.81 kb
a9d069e @adamwiggins init
authored
1 require 'rubygems'
2 require 'sinatra'
9efe3fb @adamwiggins vendor sequel
authored
3
4 $LOAD_PATH.unshift File.dirname(__FILE__) + '/vendor/sequel'
a9d069e @adamwiggins init
authored
5 require 'sequel'
6
cc77b7b @adamwiggins db setup inside configure block
authored
7 configure do
81ffae5 @adamwiggins specify id as primary key for sequel
authored
8 Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://blog.db')
173a506 @adamwiggins Blog object with configs used by feed
authored
9
10 require 'ostruct'
11 Blog = OpenStruct.new(
57f13f7 @adamwiggins genericify blog config
authored
12 :title => 'a scanty blog',
13 :author => 'John Doe',
14 :url_base => 'http://localhost:4567/',
01c1ddf @adamwiggins brief help when there are no posts
authored
15 :admin_password => 'changeme',
57f13f7 @adamwiggins genericify blog config
authored
16 :admin_cookie_key => 'scanty_admin',
d96f5e4 @adamwiggins disqus comments cofigurable
authored
17 :admin_cookie_value => '51d6d976913ace58',
18 :disqus_shortname => nil
173a506 @adamwiggins Blog object with configs used by feed
authored
19 )
cc77b7b @adamwiggins db setup inside configure block
authored
20 end
a9d069e @adamwiggins init
authored
21
ea1ac42 @adamwiggins log exceptions
authored
22 error do
23 e = request.env['sinatra.error']
24 puts e.to_s
25 puts e.backtrace.join("\n")
26 "Application error"
27 end
28
d9a9f69 @adamwiggins move post create table to top of post model file
authored
29 $LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib')
30 require 'post'
0547591 @adamwiggins create a Post model
authored
31
eb02178 @adamwiggins edit posts
authored
32 helpers do
33 def admin?
c9bb401 @adamwiggins make admin keys part of the Blog config struct
authored
34 request.cookies[Blog.admin_cookie_key] == Blog.admin_cookie_value
922140a @adamwiggins dirt simple admin protection
authored
35 end
36
37 def auth
38 stop [ 401, 'Not authorized' ] unless admin?
eb02178 @adamwiggins edit posts
authored
39 end
40 end
41
8b57157 @adamwiggins use a layout
authored
42 layout 'layout'
43
eb02178 @adamwiggins edit posts
authored
44 ### Public
45
a9d069e @adamwiggins init
authored
46 get '/' do
5c61841 @adamwiggins index uses post methods
authored
47 posts = Post.reverse_order(:created_at).limit(10)
8b57157 @adamwiggins use a layout
authored
48 erb :index, :locals => { :posts => posts }, :layout => false
a9d069e @adamwiggins init
authored
49 end
50
2160c39 @adamwiggins reorder actions
authored
51 get '/past/:year/:month/:day/:slug/' do
52 post = Post.filter(:slug => params[:slug]).first
53 stop [ 404, "Page not found" ] unless post
1f4607a @adamwiggins erb doesn't seem to pass locals to layout, so use an instance var
authored
54 @title = post.title
55 erb :post, :locals => { :post => post }
de667e6 @adamwiggins add an atom feed, code borrowed from Wink
authored
56 end
57
dae0f86 @adamwiggins redirect on article titles without trailing slahs
authored
58 get '/past/:year/:month/:day/:slug' do
59 redirect "/past/#{params[:year]}/#{params[:month]}/#{params[:day]}/#{params[:slug]}/", 301
60 end
61
a28fe63 @adamwiggins archive page shows all posts
authored
62 get '/past' do
63 posts = Post.reverse_order(:created_at)
1f4607a @adamwiggins erb doesn't seem to pass locals to layout, so use an instance var
authored
64 @title = "Archive"
65 erb :archive, :locals => { :posts => posts }
a28fe63 @adamwiggins archive page shows all posts
authored
66 end
67
dbf1aa8 @adamwiggins search by tag
authored
68 get '/past/tags/:tag' do
8b57157 @adamwiggins use a layout
authored
69 tag = params[:tag]
70 posts = Post.filter(:tags.like("%#{tag}%")).reverse_order(:created_at).limit(30)
1f4607a @adamwiggins erb doesn't seem to pass locals to layout, so use an instance var
authored
71 @title = "Posts tagged #{tag}"
72 erb :tagged, :locals => { :posts => posts, :tag => tag }
dbf1aa8 @adamwiggins search by tag
authored
73 end
74
2160c39 @adamwiggins reorder actions
authored
75 get '/feed' do
253857a @adamwiggins feed includes last 20 posts
authored
76 @posts = Post.reverse_order(:created_at).limit(20)
2160c39 @adamwiggins reorder actions
authored
77 content_type 'application/atom+xml', :charset => 'utf-8'
78 builder :feed
65b10b7 @adamwiggins post page
authored
79 end
dbf1aa8 @adamwiggins search by tag
authored
80
f40d65f @adamwiggins redirect for /rss (simplelog legacy link)
authored
81 get '/rss' do
82 redirect '/feed', 301
83 end
84
eb02178 @adamwiggins edit posts
authored
85 ### Admin
86
922140a @adamwiggins dirt simple admin protection
authored
87 get '/auth' do
88 erb :auth
89 end
90
91 post '/auth' do
c9bb401 @adamwiggins make admin keys part of the Blog config struct
authored
92 set_cookie(Blog.admin_cookie_key, Blog.admin_cookie_value) if params[:password] == Blog.admin_password
922140a @adamwiggins dirt simple admin protection
authored
93 redirect '/'
94 end
95
e534964 @adamwiggins new post
authored
96 get '/posts/new' do
922140a @adamwiggins dirt simple admin protection
authored
97 auth
e534964 @adamwiggins new post
authored
98 erb :edit, :locals => { :post => Post.new, :url => '/posts' }
99 end
100
101 post '/posts' do
922140a @adamwiggins dirt simple admin protection
authored
102 auth
e534964 @adamwiggins new post
authored
103 post = Post.new :title => params[:title], :tags => params[:tags], :body => params[:body], :created_at => Time.now, :slug => Post.make_slug(params[:title])
104 post.save
105 redirect post.url
106 end
107
eb02178 @adamwiggins edit posts
authored
108 get '/past/:year/:month/:day/:slug/edit' do
922140a @adamwiggins dirt simple admin protection
authored
109 auth
eb02178 @adamwiggins edit posts
authored
110 post = Post.filter(:slug => params[:slug]).first
111 stop [ 404, "Page not found" ] unless post
e534964 @adamwiggins new post
authored
112 erb :edit, :locals => { :post => post, :url => post.url }
eb02178 @adamwiggins edit posts
authored
113 end
114
115 post '/past/:year/:month/:day/:slug/' do
922140a @adamwiggins dirt simple admin protection
authored
116 auth
eb02178 @adamwiggins edit posts
authored
117 post = Post.filter(:slug => params[:slug]).first
118 stop [ 404, "Page not found" ] unless post
119 post.title = params[:title]
120 post.tags = params[:tags]
121 post.body = params[:body]
122 post.save
123 redirect post.url
124 end
125
Something went wrong with that request. Please try again.