-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
155 lines (136 loc) · 2.43 KB
/
app.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
149
150
151
152
153
154
155
require 'rubygems'
require 'sinatra'
require 'config'
before do
@widgets = Widget.filter.order(:order)
end
get '/' do
@posts = Post.ordered
@next_page = next_page
erb :index
end
get '/page/:page' do
@posts = Post.ordered params[:page]
@next_page = next_page
erb :index
end
get '/search' do
@posts = Post.search params[:q]
erb :index
end
get '/feed' do
@posts = Post.ordered
content_type 'xml'
feed @posts
end
get '/login' do
go_home if logged?
erb :login
end
post '/login' do
if auth? params[:code]
login
go_home
else
message 'Ops! Wrong code.'
erb :login
end
end
get '/logout' do
if_logged do
login false
go_home
end
end
get '/post/new' do
if_logged do
@post = Post.new
erb :post
end
end
post '/post' do
if_logged do
post = Post[params[:id]] || Post.new
post.title = params[:title]
post.text = params[:text]
unless post.id
post.date = Time.now
post.link = to_link params[:title]
end
if params[:action] == 'save'
post.save
go_home
elsif params[:action] == 'preview'
@posts = [] << post
erb :index
end
end
end
get '/post/:id' do
if_logged do
@post = Post[params[:id]]
erb :post
end
end
delete '/post/:id' do
if_logged do
Post[params[:id]].delete
go_home
end
end
get '/widget/new' do
if_logged do
@widget = Widget.new
erb :widget
end
end
post '/widget' do
if_logged do
widget = Widget[params[:id]] || Widget.new
widget.title = params[:title]
widget.content = params[:content]
widget.order = params[:order]
widget.save
go_home
end
end
get '/widget/:id' do
if_logged do
@widget = Widget[params[:id]]
erb :widget
end
end
delete '/widget/:id' do
if_logged do
Widget[params[:id]].delete
go_home
end
end
get '/settings' do
if_logged do
erb :settings
end
end
post '/settings' do
if_logged do
if auth? params[:code]
$settings.name = params[:name]
$settings.title = params[:title]
$settings.code = params[:new_code] unless params[:new_code].empty?
$settings.feed = params[:feed]
$settings.footer = params[:footer]
$settings.tracker = params[:tracker]
$settings.save
go_home
else
message 'Ops! Wrong code.'
erb :settings
end
end
end
get '/:link' do
@posts = Post.with_link params[:link]
@title = @posts.first.title
@footer = $settings.footer
erb :index
end