Class: Usmu::Ui::RackServer

Inherits:
Object
  • Object
show all
Defined in:
lib/usmu/ui/rack_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ RackServer

Returns a new instance of RackServer.



5
6
7
8
9
10
# File 'lib/usmu/ui/rack_server.rb', line 5

def initialize(configuration)
  @log = Logging.logger[self]
  @configuration = configuration
  @generator = @configuration.generator
  @index = configuration['serve', 'index', default: 'index.html']
end

Instance Method Details

#call(env) ⇒ void



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
# File 'lib/usmu/ui/rack_server.rb', line 12

def call(env)
  path = env['PATH_INFO'][1...env['PATH_INFO'].length]
  @log.info "Received a request for: #{path}"
  path = File.join(path, @index) if File.directory? File.join(@configuration.source_path, path)
  path = path[1...path.length] if path[0] == '/'
  @log.info "Serving: #{path}"

  valid = @generator.renderables.select {|r| r.output_filename == path }

  if valid.length > 0
    @generator.refresh
    page = valid[0]
    type = case page.output_filename[page.output_filename.rindex('.')...page.output_filename.length]
             when '.html', '.php'
               'text/html'
             when '.css'
               'text/css'
             when '.js'
               'text/javascript'
             else
               'text/plain'
           end

    ['200', {'Content-Type' => type}, [page.render]]
  else
    ['404', {'Content-Type' => 'text/html'}, ['<!DOCTYPE html><h1>File not found.</h1>']]
  end
end