forked from twilson63/colonial
/
app.rb
81 lines (68 loc) · 1.69 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
require 'rubygems'
# Need for processing documents
require 'yaml'
require 'haml'
# Webserving, yay!
require 'stevenson'
# Making life easier
require 'ext'
pen :run => true do
collection :people do
page :tom
end
end
=begin
# Pre-compiling (in a sense) all of the people, pages, and projects ahead of time.
# Reducing the hit on the filesystem.
people = []
puts 'Loading People'
Dir['./people/*.{haml,html,erb}'].each do |file|
people << parse(file)
puts "\t+ #{file} -> /people/#{people.last[1][:path]}"
end
projects = []
puts 'Loading Projects'
Dir['./projects/*.{haml,html}'].each do |file|
projects << parse(file)
puts "\t+ #{file} -> /projects/#{projects.last[1][:path]}"
end
pages = []
puts 'Loading Pages'
Dir['./pages/*.{haml,html}'].each do |file|
pages << parse(file)
puts "\t+ #{file} -> /#{pages.last[1][:path]}"
end
print 'Compiling Page Routes... '
pages_match = '('+pages.collect {|p| Regexp.escape(p[1][:path]) }.join('|')+')'
print "Done\n"
puts "\n" # Our reporting is finished; now it's time to let Sinatra take the mic.
before do
@pages = pages
end
# Essentially serving up static assets out of memory and the filesystem. Only heaving lifting is routing and the views.
get '/' do
@people = people
@projects = projects
haml :index
end
get '/people/:path' do
@person = people.select {|p| p[1][:path] === params[:path]}.first
if @person
haml :person
else
not_found
end
end
get '/projects/:path' do
@project = projects.select {|p| p[1][:path] === params[:path]}.first
if @project
haml :project
else
not_found
end
end
get Regexp.new("^/#{pages_match}$", true) do |t|
@page = pages.select {|p| p[1][:path] === t }.first
haml :page
end
=end