forked from chrisvel/tududi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
130 lines (105 loc) · 3.4 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'sinatra'
require 'sinatra/activerecord'
require 'securerandom'
require 'byebug'
require './app/models/user'
require './app/models/area'
require './app/models/project'
require './app/models/task'
require './app/models/tag'
require './app/models/note'
require './app/helpers/authentication_helper'
require './app/routes/authentication_routes'
require './app/routes/tasks_routes'
require './app/routes/projects_routes'
require './app/routes/areas_routes'
require './app/routes/notes_routes'
require './app/routes/tags_routes'
require './app/routes/users_routes'
require 'sinatra/cross_origin'
helpers AuthenticationHelper
use Rack::MethodOverride
set :database_file, './app/config/database.yml'
set :views, proc { File.join(root, 'app/views') }
set :public_folder, 'public'
configure do
enable :sessions
secure_flag = production? && ENV['TUDUDI_INTERNAL_SSL_ENABLED'] == 'true'
set :sessions, httponly: true,
secure: secure_flag,
expire_after: 2_592_000,
same_site: secure_flag ? :none : :lax
set :session_secret, ENV.fetch('TUDUDI_SESSION_SECRET') { SecureRandom.hex(64) }
# Auto-create user if not exists
if ENV['TUDUDI_USER_EMAIL'] && ENV['TUDUDI_USER_PASSWORD'] && ActiveRecord::Base.connection.table_exists?('users')
user = User.find_or_initialize_by(email: ENV['TUDUDI_USER_EMAIL'])
if user.new_record?
user.password = ENV['TUDUDI_USER_PASSWORD']
user.save
end
end
end
use Rack::Protection
before do
require_login
end
configure do
enable :cross_origin
end
before do
response.headers['Access-Control-Allow-Origin'] = 'http://localhost:8080'
response.headers['Access-Control-Allow-Credentials'] = 'true'
end
options '*' do
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = 'Authorization, Content-Type, Accept'
200
end
helpers do
def current_path
request.path_info
end
def partial(page, options = {})
erb page, options.merge!(layout: false)
end
def nav_link_active?(path, query_params = {}, project_id = nil)
current_uri = request.path_info
current_query = request.query_string
current_params = Rack::Utils.parse_nested_query(current_query)
is_project_page = current_uri.include?('/project/') && path.include?('/project/')
if is_project_page
current_uri == path && (!project_id || current_uri.end_with?("/#{project_id}"))
elsif !query_params.empty?
current_uri == path && query_params.all? { |k, v| current_params[k] == v }
else
current_uri == path && current_params.empty?
end
end
def nav_link(path, query_params = {}, project_id = nil)
is_active = nav_link_active?(path, query_params, project_id)
classes = 'nav-link py-1 px-3'
classes += ' active-link' if is_active
classes
end
def update_query_params(key, value)
uri = URI(request.url)
params = Rack::Utils.parse_nested_query(uri.query)
params[key] = value
Rack::Utils.build_query(params)
end
def url_without_tag
uri = URI(request.url)
params = Rack::Utils.parse_nested_query(uri.query)
params.delete('tag') # Remove the 'tag' parameter
uri.query = Rack::Utils.build_query(params)
uri.to_s
end
end
get '/*' do
erb :index
end
not_found do
content_type :json
status 404
{ error: 'Not Found', message: 'The requested resource could not be found.' }.to_json
end