/
init.rb
68 lines (56 loc) · 1.85 KB
/
init.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
# This file initializes our Sinatra-powered blog.
# Require the necessary libraries.
require 'rubygems'
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/vendor/sinatra/lib')
require 'sinatra'
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/vendor/sequel/sequel_core/lib',
File.dirname(__FILE__) + '/vendor/sequel/sequel/lib')
require 'sequel'
require 'json'
require 'yaml'
configure do
root = File.expand_path(File.dirname(__FILE__))
set_option :views, File.join(root, 'app', 'views')
# Load the configuration file.
if !File.exist?('config.yml')
puts "There's no configuration file at config.yml!"
exit!
end
CONFIG = YAML.load_file('config.yml')
# Connect to the database
if !File.exist?('blog.db')
puts "There's no database file at blog.db! Create an empty file called " +
"blog.db and run 'ruby create_db.rb' to create the database."
exit!
end
DB = Sequel.sqlite('blog.db')
end
helpers do
# Use this helper to make not found page a bit easier to display. It abstracts
# away some of the common code for all "Not found" responses, for example
# setting the status code to 404.
#
# If a block is given, this executes the block (yielding <tt>self</tt>, a
# Sinatra::EventContext), and renders it result (using #stop). Else, if the
# <tt>content</tt> parameter is not nil, this renders <tt>content</tt> (using
# #stop). Otherwise, it renders <tt>notfound.haml</tt>.
def not_found(content = nil)
status 404
if block_given?
stop yield(self)
elsif content
stop content
else
stop haml(:notfound)
end
end
end
# Set the not found page for URIs that don't match to any specified route.
not_found do
status 404
haml :notfound
end
# Create the models.
Dir["app/models/*.rb"].each { |x| require x }
# Load the controllers.
Dir["app/controllers/*.rb"].each { |x| load x }