-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.rb
104 lines (85 loc) · 2.71 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
# encoding: utf-8
require 'sinatra/base'
require 'yaml'
require 'digest/md5'
require 'will_paginate/data_mapper'
class App < Sinatra::Base
# Register Sinatra Flash
register Sinatra::Flash
# Include Session Cookie Module
use Rack::Session::Cookie, :secret => "<secret>"
# Development Specific configs
configure :development do
DataMapper::Logger.new($stdout, :debug)
end
# Production specific configs
configure :production do
YAML.load_file(File.dirname(__FILE__)+'/config/production.yaml').each do |k, v|
set k, v
end
end
# General ENV configuration
configure do
dbconf = YAML.load_file(File.dirname(__FILE__)+'/config/database.yaml')
case dbconf["adapter"]
when "mysql"
dbconf = dbconf[ENV['RACK_ENV']]
DataMapper.setup(:default, "mysql://#{dbconf["username"]}:#{dbconf["password"]}@#{dbconf["host"]}/#{dbconf["database"]}")
when "postgres"
dbconf = dbconf[ENV['RACK_ENV']]
DataMapper.setup(:postgres, "postgres://#{dbconf["username"]}:#{dbconf["password"]}@#{dbconf["host"]}/#{dbconf["database"]}")
when "sqlite3"
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/db/application.db")
end
# Enable sessions for all ENV's
enable :sessions
# Set up our general configs
set :root , File.dirname(__FILE__)
set :public_folder , File.dirname(__FILE__) + '/public'
set :app_file , __FILE__
set :views , File.dirname(__FILE__) + '/views'
set :tests , File.dirname(__FILE__) + '/tests'
set :haml , :format => :html5
set :dump_errors , true
set :logging , true
set :raise_errors , true
# Load general configs from the file
YAML.load_file(File.dirname(__FILE__)+'/config/development.yaml').each do |k, v|
set k, v
end
end
helpers do
# Set an error in the flash and redirect
def set_error(message, path)
flash[:error] = message
redirect path
end
# Set a notice in the flash and redirect
def set_notice(message, path)
flash[:notice] = message
redirect path
end
end
# Log error and redirect
error do
logger.error env['sinatra.error'].message # log this to the output
redirect to('500.html')
end
# Redirect to static 404 page
not_found do
redirect to('404.html')
end
end
# Load up all helpers first (NB)
Dir[File.dirname(__FILE__) + "/helpers/*.rb"].each do |file|
require file
end
# Load up all models next
Dir[File.dirname(__FILE__) + "/models/*.rb"].each do |file|
require file
end
DataMapper.finalize
# Load up all controllers last
Dir[File.dirname(__FILE__) + "/controllers/*.rb"].each do |file|
require file
end