Class: Server::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/server/dispatcher.rb

Constant Summary collapse

WEB_ROOT =
'web_root'
DEV_ROOT =
'dev_root'

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

Returns a new instance of Dispatcher.



8
9
10
11
# File 'lib/server/dispatcher.rb', line 8

def initialize
  @generator = Generator::Generator.new
  Dir.mkdir(WEB_ROOT) unless File.exist?(WEB_ROOT)
end

Instance Method Details

#dispatch(request, response) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/server/dispatcher.rb', line 13

def dispatch(request, response)
  path = request.path

  regenerate_files_if(path)

  return response.send_404 unless File.exist?(path)

  if !File.directory?(path)
    if path.include? '.php'
      response.send(200, execute_php(path, request.params))
    else
      response.send_file path
    end
  else
    if File.exist?("#{path}/index.html")
      response.send_301 "#{path}/index.html"
    else
      response.send(200, list_dir(path))
    end
  end
end

#execute_php(path, params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/server/dispatcher.rb', line 71

def execute_php(path, params)
  params_s = ''
  params.each do |key, value|
    params_s += "#{key}=#{value[0]} "
  end
  executors = ['php', 'php-cgi', 'php-fpm']
  cmd = "#{executors[0]} #{path} #{params_s}"
  STDERR.puts(cmd)
  %x[ #{cmd} ]
end

#list_dir(path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/server/dispatcher.rb', line 35

def list_dir(path)
  content = ''

  Dir.glob("#{path.gsub(WEB_ROOT, DEV_ROOT)}/*/*.haml").each do |f|
    next if f.include? 'layout.haml'
    f = File.path(f)
    f_name = File.basename(f).gsub('.haml', '.html')
    f_path = File.dirname(f).sub("#{DEV_ROOT}", '')
    content += "<li><a href='#{f_path}/#{f_name}'><b>#{f_path}</b>/#{f_name}</a></li>"
  end

   Dir.glob("#{path.gsub(WEB_ROOT, DEV_ROOT)}/*/public/*.html").each do |f|
    f = File.path(f)
    f_name = File.basename(f)
    f_path = File.dirname(f).sub('/public', '').sub("#{DEV_ROOT}", '')
    puts f_path
    content += "<li><a href='#{f_path}/#{f_name}'><b>#{f_path}</b>/#{f_name}</a></li>"
  end

  "<!DOCTYPE html><html><head><style>*{ font-size: 20px; font-family: Verdana, 'Lucida Sans Unicode', sans-serif;} a,a:hover{text-decoration: none; color: #818181;}</style></head><body><h1>#{path}</h1><ul>#{content}</ul><a target='_blank' href='https://github.com/dennisvandehoef/easy-html-creator'>ehc on Github</a></body></html>"
end

#regenerate_files_if(path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/server/dispatcher.rb', line 57

def regenerate_files_if(path)
  #TODO we only generate sass and coffee if its a haml file
  return unless path.include? '.html'

  #no html? no reload -> no regenarate
  Server.log "#######################"
  Server.log "#                     #"
  Server.log "#   Renew all files   #"
  Server.log "#                     #"
  Server.log "#######################"

  @generator.generate path.gsub("#{WEB_ROOT}", "#{DEV_ROOT}").gsub('.html', '.haml')
end