-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
83 lines (62 loc) · 1.37 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
require "sinatra"
require "bcrypt"
require_relative "model.rb"
use Rack::Session::Pool
helpers do
def admin? ; session["isLogdIn"] == true; end
#def admin? ; true; end
def protected! ; halt 401 unless admin? ; end
end
configure :development do
MongoMapper.database = 'newz'
end
configure :production do
MongoMapper.setup({'production' => {'uri' => ENV['MONGOLAB_URI']}}, 'production')
end
get "/" do
newz = Newz.sort(:created_at.desc).limit(30)
erb :index, {:locals => {news: newz}}
end
get "/about" do
erb :about
end
get "/submit" do
"nope"
end
get "/archiv/?" do
redirect "/archiv/1"
end
get "/archiv/:page/?" do |page|
elementPerPage = 50
pageId = page.to_i
newz = Newz.paginate({
:order => :created_at.desc,
:per_page => elementPerPage,
:page => pageId,
})
total = (Newz.all.count / elementPerPage)
p total
erb :archiv, {:locals => {page: pageId, news: newz, totalPage: total}}
end
post "/create" do
protected!
Newz.create(:title => params["title"], :url => params["url"])
redirect "/"
end
get "/admin" do
erb :admin
end
post "/admin" do
logedIn = false
name = params["inputUsername"]
pass = params["inputPassword"]
if name == ENV['USER'] && BCrypt::Engine.hash_secret(pass, ENV['SALT']) == ENV['PASS']
session["isLogdIn"] = true
redirect "/"
end
redirect "/admin"
end
get "/logout/?" do
session["isLogdIn"] = false
redirect "/"
end