-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
107 lines (85 loc) · 1.89 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
require "sinatra"
require "redcarpet"
require "builder"
require "bcrypt"
require "mysql2"
require "sequel"
use Rack::Session::Pool
class String
def is_i?
/\A[-+]?\d+\z/ === self
end
end
helpers do
def admin? ; session["isLogdIn"] == true || DEBUG; end
def protected! ; halt 401 unless admin? ; end
end
configure do
set :erb, :layout => :'meta-layout/layout'
set :erb, :locals => {:title => "Primärquelle", :tagline => "Seriösliche Nachrichten!"}
Md = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true)
end
configure :development do
DB = Sequel.sqlite
set :show_exceptions, true
DEBUG = true
end
configure :production do
# mysql2://user:pass@host/dbname
DB = Sequel.connect(ENV['MYSQL_BLOG_URI'])
DEBUG = false
end
# require after config!
require_relative "model.rb"
error 401 do
"fuck no login"
end
get "/login/?" do
erb :login
end
post '/login/?' do
if params['username'] == ENV['USER'] && BCrypt::Password.new(ENV['PASS']) == params['pass']
session["isLogdIn"] = true
redirect '/'
else
halt 401
end
end
get('/logout/?'){ session["isLogdIn"] = false ; redirect '/' }
get "/" do
erb :index, :locals => {:posts => Post.all_sorted, :onePost => false}
end
get "/impressum/?" do
erb :impressum
end
get "/feed/?" do
@posts = Post.all_sorted
builder :rss
end
post "/add/:id" do |id|
if id == "nil"
post = Post.new(:text => params["text"])
else
post = Post.find(:id => id)
if post == nil
halt 404
end
post.text = params["text"]
end
if !post.save
halt 500
end
redirect "/"
end
get "/edit/:id" do |id|
erb :postAdd, :locals => {:post => Post.find(:id => id)}
end
get "/:id" do |id|
post = nil
if id.is_i?
post = Post.find(:id => id)
else
post = Post.find(:url => id)
end
erb :index, :locals => {:posts => Array(post), :onePost => true}
end