Module: Pekky::Server

Defined in:
lib/pekky/server.rb

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pekky/server.rb', line 12

def self.call(env)      
  Pekky.build
  path = env['PATH_INFO']
  if path.match(/\.(\w+)$/)
    file = File.join(Config[:output_dir], path)
    type = Rack::Mime.mime_type(File.extname(path))
  else
    file = File.join(Config[:output_dir], path, "index.html")
    type = "text/html"        
  end
  
  if File.exists?(file)
    output = File.open(file).read
    [200, {"Content-Type" => type}, [output]]
  else
    [200, {"Content-Type" => "text/html"}, "Oh, file is not found; Pekky cries for you."]
  end
end

.start(port) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/pekky/server.rb', line 3

def self.start(port)
  Pekky.load
  app = Rack::Builder.app {
    use Rack::Static, :urls => %w(/stylesheets /javascripts /images), :root => Pekky::Config[:output_dir]
    run Pekky::Server
  }
  Rack::Server.new(:app => app, :Port => port || 3000).start
end