-
Notifications
You must be signed in to change notification settings - Fork 9
/
application.rb
241 lines (189 loc) · 6.83 KB
/
application.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env ruby
require 'mysql2'
require 'sequel'
require 'sinatra'
require 'haml'
require 'pp'
require 'logger'
require 'yaml'
require 'uri'
require 'json'
require 'net/http'
require 'rack'
use Rack::Logger
module Dirt
class Application < Sinatra::Application
CONFIG_FILE = File.join(File.dirname(__FILE__), 'config/config.yml')
DB_CONFIG_FILE = File.join(File.dirname(__FILE__), 'config/database.yml')
def array_match(value, array)
found = false
array.each do |v|
found = (value =~ v)
break if found
end
return found
end
def self.load_config(file_name)
env = ENV["RACK_ENV"]
data = YAML::load(File.open(file_name))[env]
raise "No configration found in #{file_name} for RACK_ENV environment '#{env}'. Check #{file_name} for valid values of RACK_ENV" if data.nil?
data.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo }
end
configure do
set :logging, true
# enable :sessions
Dirt::CONFIG = load_config(CONFIG_FILE)
db_config = load_config(DB_CONFIG_FILE)
Dirt::RT_DB = Sequel.connect(db_config[:rt])
Dirt::RT_DB.loggers << Logger.new($stdout)
Dirt::DIRT_DB = Sequel.connect(db_config[:dirt])
Dirt::DIRT_DB.loggers << Logger.new($stdout)
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:secret => 'qwedsa123',
:expire_after => 60*60*12
if Dirt::CONFIG[:log_sql]
sql_log_file = Dirt::CONFIG[:sql_log_file]
sql_logger = Logger.new(sql_log_file)
Dirt::RT_DB.loggers << sql_logger
Dirt::DIRT_DB.loggers << sql_logger
end
Dir['lib/*.rb'].sort.each { |lib| require File.join(File.dirname(__FILE__), lib) }
Dir['models/*.rb'].sort.each { |model| require File.join(File.dirname(__FILE__), model) }
Dir['controllers/*.rb'].sort.each { |controller| require File.join(File.dirname(__FILE__), controller) }
end
before do
@user = Dirt::User.get(session[:user_id])
path = request.path_info
if @user.nil? and not array_match(path, [/login/,/favicon/,/kanbanboard/,/v2.0/])
redirect to("/login?redirect_to=#{path}")
elsif session[:user].nil? && !@user.nil?
session[:user] = Dirt::User.persist(session[:user_id])
end
end
# -----------------------------------------------------------------
# App Related Routes
# -----------------------------------------------------------------
get '/login' do
Dirt::LoginController.show(params)
end
post '/login' do
begin
Dirt::LoginController.authenticate(params, session)
rescue => error
redirect to("/login?redirect_to=#{params[:redirect_to]}&failure_message=#{error.message}")
end
redirect to(params[:redirect_to])
end
get '/logout' do
@success_message = Dirt::LoginController.logout(params, session)
haml :login
end
# -----------------------------------------------------------------
# Project Related Routes
# -----------------------------------------------------------------
get %r{(^/$|^/projects[/]*$)} do
Dirt::ProjectController.show(params, session)
end
get '/projects/new' do
params[:new] = true
Dirt::ProjectController.edit(params, session)
end
get '/projects/:project/edit' do
params[:new] = false
Dirt::ProjectController.edit(params, session)
end
post '/projects/add' do
Dirt::ProjectController.save(params, session)
redirect "/projects/"
end
post '/projects/:project/save' do
Dirt::ProjectController.save(params, session)
redirect "/projects/"
end
# -----------------------------------------------------------------
# Project Page related routes
# -----------------------------------------------------------------
get '/projects/:project' do
redirect "/projects/#{params[:project]}/taskboard"
end
get '/projects/:project/pages' do
redirect "/projects/#{params[:project]}/pages/index"
end
get '/projects/:project/pages/:page' do
Dirt::PageController.show(params, session)
end
get '/projects/:project/pages/:page/edit' do
Dirt::PageController.edit(params, session)
end
post '/projects/:project/pages/:page/save' do
Dirt::PageController.save(params, session)
redirect "/projects/#{params[:project]}/pages/#{params[:page]}"
end
get '/projects/:project/notepad' do
Dirt::NotepadController.show(params,session)
end
post '/projects/:project/save_notepad' do
msg = Dirt::NotepadController.processNotepad(params, session)
msg = Rack::Utils.build_nested_query(msg)
redirect "/projects/#{params[:project]}/notepad?"+msg
end
get '/projects/:project/taskboard' do
Dirt::TaskboardController.show(params,session)
end
get '/projects/:project/kanbanboard' do
Dirt::KanbanBoardController.show(params,session)
end
# run! if app_file == $0
# -----------------------------------------------------------------
# Restapi related paths
# -----------------------------------------------------------------
get '/api/v1.0/?' do
#respond only to ajax request
if request.xhr?
value = Dirt::RestapiController.show(params, session)
return value.to_json
else
'Api responds only to ajax request'
end
end
get '/api/v2.0/projects/:project/:method' do
#respond only to ajax request
if request.xhr?
value = Dirt::ProjectApiController.show(params, session)
return value.to_json
else
'Api responds only to ajax request'
end
end
# -----------------------------------------------------------------
# Profile related routes
# -----------------------------------------------------------------
get '/profile/me' do
Dirt::ProfileController.show(params,session)
end
get '/profile/edit' do
Dirt::ProfileController.edit(params,session)
end
post '/profile/edit' do
begin
Dirt::ProfileController.save(params,session)
rescue => error
redirect to "profile/edit?error_msg=#{error.message}"
end
redirect to "profile/me"
end
# -----------------------------------------------------------------
# Static files related routes
# -----------------------------------------------------------------
get '/static/:page/?:subpage?' do
Dirt::StaticController.show(params, session)
end
# -----------------------------------------------------------------
# Page to handle errors
# -----------------------------------------------------------------
not_found do
Dirt::StaticController.show({:page => "notfound"}, session)
end
end
end