-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcms.rb
168 lines (134 loc) · 3.21 KB
/
cms.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
156
157
158
159
160
161
162
163
164
165
166
167
168
require "sinatra"
require "sinatra/reloader"
require "tilt/erubis"
require "redcarpet"
require "yaml"
require "bcrypt"
configure do
enable :sessions
set :session_secret, 'super secret'
end
def data_path
if ENV["RACK_ENV"] == "test"
File.expand_path("../test/data", __FILE__)
else
File.expand_path("../data", __FILE__)
end
end
def render_markdown(text)
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
markdown.render(text)
end
def load_file_content(path)
content = File.read(path)
case File.extname(path)
when ".txt"
headers["Content-Type"] = "text/plain"
content
when ".md"
erb render_markdown(content)
end
end
def user_signed_in?
session.key?(:username)
end
def require_signed_in_user
unless user_signed_in?
session[:message] = "You must be signed in to do that."
redirect "/"
end
end
def load_user_credentials
credentials_path = if ENV["RACK_ENV"] == "test"
File.expand_path("../test/users.yml", __FILE__)
else
File.expand_path("../users.yml", __FILE__)
end
YAML.load_file(credentials_path)
end
def valid_credentials?(username, password)
credentials = load_user_credentials
if credentials.key?(username)
bcrypt_password = BCrypt::Password.new(credentials[username])
bcrypt_password == password
else
false
end
end
get "/" do
pattern = File.join(data_path, "*")
@files = Dir.glob(pattern).map do |path|
File.basename(path)
end
erb :index
end
post "/create" do
require_signed_in_user
filename = params[:filename].to_s
if filename.size == 0
session[:message] = "A name is required."
status 422
erb :new
else
file_path = File.join(data_path, filename)
File.write(file_path, "")
session[:message] = "#{params[:filename]} has been created."
redirect "/"
end
end
get "/:filename" do
pass if params[:filename] == 'new'
file_path = File.join(data_path, params[:filename])
if File.exist?(file_path)
load_file_content(file_path)
else
session[:message] = "#{params[:filename]} does not exist."
redirect "/"
end
end
get "/:filename/edit" do
require_signed_in_user
file_path = File.join(data_path, params[:filename])
@filename = params[:filename]
@content = File.read(file_path)
erb :edit
end
post "/:filename" do
require_signed_in_user
file_path = File.join(data_path, params[:filename])
File.write(file_path, params[:content])
session[:message] = "#{params[:filename]} has been updated."
redirect "/"
end
post "/:filename/delete" do
require_signed_in_user
file_path = File.join(data_path, params[:filename])
File.delete(file_path)
session[:message] = "#{params[:filename]} has been deleted."
redirect "/"
end
get "/new" do
require_signed_in_user
erb :new
end
get "/users/signin" do
erb :signin
end
post "/users/signin" do
@username = params[:username]
credentials = load_user_credentials
if valid_credentials?(@username, params[:password])
session[:username] = @username
session[:message] = "Welcome!"
redirect "/"
else
session[:message] = "Invalid credentials"
status 422
erb :signin
end
end
post "/users/signout" do
session.delete(:username)
session[:message] = "You have been signed out."
redirect "/"
end