Class: Moka::SimpleServer

Inherits:
Object show all
Includes:
WEBrick
Defined in:
lib/commands/server.rb

Instance Method Summary collapse

Instance Method Details

#run(config = {}) ⇒ Object



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
# File 'lib/commands/server.rb', line 11

def run(config = {})
  start_webrick(config) do |server|
    compiler = Moka::Compiler.new
    server.mount_proc('/') do |req, resp|
      puts req
      if req.path == "" or req.path[-1] == ?/
        req.path << "index.html"
      end
      manifest = YAML.load_file(File.expand_path("manifest.yml", MOKA_ROOT))
      group_name, page_name = search_page_by_path(req.path.sub("/", ""), manifest)
      if group_name.nil? or page_name.nil?
        if File.extname(req.path) == ".css" and (File.exists?(File.expand_path("project/styles/#{File.basename(req.path, ".css") + ".sass"}", MOKA_ROOT)) or File.exists?(File.expand_path("project/styles/#{File.basename(req.path, ".css") + ".scss"}", MOKA_ROOT)))
          resp.status = 200
          resp["Content-Type"] = get_content_type(req.path)
          resp.body = compiler.compile_style(File.basename(req.path, ".css"))
        elsif File.exist?(File.expand_path("compiled#{req.path}", MOKA_ROOT))
          f = File.new(File.expand_path("compiled#{req.path}", MOKA_ROOT), "r")
          resp.status = 200
          resp["Content-Type"] = get_content_type(req.path)
          resp.body = f.read
          f.close
        else
          resp.status = 404
          resp["Content-Type"] = "text/plain"
          resp.body = "Sorry... it looks like the page you are searching doesn't exist..."
        end
      else
        resp.status = 200
        resp["Content-Type"] = get_content_type(req.path)
        resp.body = compiler.compile(group_name, page_name, manifest)
      end
    end
  end
end