-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rb
83 lines (66 loc) · 1.41 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
require 'rubygems'
require 'sinatra'
require 'json'
$LOAD_PATH.unshift File.dirname(__FILE__) + '/vendor/sequel'
require 'sequel'
$LOAD_PATH.unshift File.dirname(__FILE__)
require 'setting'
configure do
Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://note.db')
end
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib')
require 'note'
error do
e = request.env['sinatra.error']
puts e.to_s
puts e.backtrace.join("\n")
"Application error"
end
error 403 do
'Access forbidden'
end
helpers do
def partial(template, locals = {})
erb(template, :layout => false, :locals => locals)
end
def admin?
request.cookies[Setting.admin_cookie_key] == Setting.admin_cookie_value
end
def auth
unless admin?
status 401
body 'Not authorized'
end
end
end
layout 'layout'
get '/' do
erb :index
end
get '/auth' do
erb :auth
end
post '/auth' do
if params[:password] == Setting.admin_password
response.set_cookie(Setting.admin_cookie_key, Setting.admin_cookie_value)
redirect '/'
end
redirect '/auth'
end
get '/notes/new' do
auth
erb :edit, :locals => { :note => Note.new, :url => '/notes' }
end
post '/notes' do
auth
note = Note.new params
note.created_at = Time.now
note.save
redirect "/notes"
end
get '/notes' do
notes = Note.filter(:body.like("%#{params[:keyword]}%")).order(:created_at.desc, :id.desc).limit(20)
content_type :json
puts notes.naked.all.to_json
notes.naked.all.to_json
end