forked from schneems/ruby_view_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_advanced.rb
More file actions
31 lines (23 loc) · 796 Bytes
/
server_advanced.rb
File metadata and controls
31 lines (23 loc) · 796 Bytes
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
require 'erb'
require 'webrick'
root = File.expand_path 'public'
server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => root
## ===========
def process_erb(string, req = nil)
template = ERB.new(string)
return template.result(binding)
end
Dir['views/*.html.erb'].each do |file|
puts "Registering file for server: #{file}"
file_name = file.split('/').last.gsub('.html.erb', '')
server.mount_proc "/#{file_name}" do |req, res|
content_string = File.open(file, 'r').read
@request = req
layout_string = File.open('views/layouts/application.html.erb', 'r').read
main_contents = process_erb(content_string)
res.body = process_erb(layout_string, req) {main_contents}
end
end
## ===========
trap 'INT' do server.shutdown end
server.start