-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
149 lines (123 loc) · 3.21 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require 'rubygems'
require 'sinatra'
require 'mongo_mapper'
require 'builder'
require 'rack/mobile-detect'
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib')
require 'photo'
configure do
if ENV['MONGOHQ_URL']
MongoMapper.config = {ENV => {'uri' => ENV['MONGOHQ_URL']}}
else
MongoMapper.config = {ENV => {'uri' => 'mongodb://localhost/snapped'}}
end
MongoMapper.connect(ENV)
require 'ostruct'
Blog = OpenStruct.new(
:title => 'Snapped',
:author => 'Basil Fawlty',
:url_base => 'http://example.com/',
:admin_password => 'CHANGEME',
:admin_cookie_key => 'snapped_admin',
:admin_cookie_value => 'CHANGEME',
:flickr_api_key => 'get_this_from_your_flickr_account',
:flickr_shared_secret => 'get_this_from_your_flickr_account'
)
end
# Disable template caching until a new version of Tilt comes out. This is
# a performance hit:
# https://github.com/sinatra/sinatra/issuesearch?state=open&q=builder#issue/166/comment/681668
enable :reload_templates
error do
e = request.env['sinatra.error']
puts e.to_s
puts e.backtrace.join("\n")
"Application error"
end
use Rack::MobileDetect
helpers do
def admin?
request.cookies[Blog.admin_cookie_key] == Blog.admin_cookie_value
end
def auth
halt(401, 'Not authorized') unless admin?
end
def link_nav_if_url(text, url, klass="")
url ?
"<a href='#{url}' class='enabled #{klass}'>#{text}</a>" :
"<a href='#' class='disabled #{klass}' onclick='return false;'>#{text}</a>"
end
def mobile?
!request.env['X_MOBILE_DEVICE'].blank?
end
end
layout 'layout'
### Auth
get '/auth' do
erb :auth
end
post '/auth' do
response.set_cookie(Blog.admin_cookie_key, Blog.admin_cookie_value) if params[:password] == Blog.admin_password
redirect '/'
end
### Feed
get '/feed' do
@photos = Photo.sort(:created_at.desc).limit(20)
content_type 'application/atom+xml', :charset => 'utf-8'
builder :feed
end
get '/rss' do
redirect '/feed', 301
end
### Public
get '/' do
photo = Photo.sort(:created_at.desc).limit(1).first
if photo
erb :photo, :locals => { :photo => photo }
else
erb :no_photo
end
end
get '/:slug' do
photo = Photo.where(:slug => params[:slug]).first
halt(404, "Page not found") unless photo
@title = photo.title
erb :photo, :locals => { :photo => photo }
end
### Admin
get '/photos/new' do
auth
erb :edit, :locals => { :photo => Photo.new, :url => '/photos' }
end
post '/photos' do
auth
photo = Photo.new :title => params[:title], :original_url => params[:original_url], :body => params[:body], :created_at => Time.now
photo.slurp_flickr_information
photo.generate_slug
photo.save
redirect photo.url
end
get '/:slug/edit' do
auth
photo = Photo.where(:slug => params[:slug]).first
halt(404, "Page not found") unless photo
erb :edit, :locals => { :photo => photo, :url => photo.url }
end
post '/:slug' do
auth
photo = Photo.where(:slug => params[:slug]).first
halt(404, "Page not found") unless photo
photo.title = params[:title]
photo.body = params[:body]
photo.original_url = params[:original_url]
photo.slurp_flickr_information
photo.save
redirect photo.url
end
delete '/:slug' do
auth
photo = Photo.where(:slug => params[:slug]).first
halt(404, "Page not found") unless photo
photo.destroy
redirect '/'
end