Class: H2OConfigurator::Host

Inherits:
Object
  • Object
show all
Defined in:
lib/h2o-configurator/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Host

Returns a new instance of Host.



8
9
10
11
# File 'lib/h2o-configurator/host.rb', line 8

def initialize(dir)
  @dir = dir
  @name = dir.basename.to_s
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



5
6
7
# File 'lib/h2o-configurator/host.rb', line 5

def dir
  @dir
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/h2o-configurator/host.rb', line 6

def name
  @name
end

Instance Method Details

#access_log_fileObject



123
124
125
# File 'lib/h2o-configurator/host.rb', line 123

def access_log_file
  H2OConfigurator::H2OLogDir / "#{@name}.access.log"
end

#cert_dirObject



103
104
105
# File 'lib/h2o-configurator/host.rb', line 103

def cert_dir
  H2OConfigurator::CertBaseDir / @name
end

#htpasswd_fileObject



115
116
117
# File 'lib/h2o-configurator/host.rb', line 115

def htpasswd_file
  @dir / '.htpasswd'
end

#make_configObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/h2o-configurator/host.rb', line 13

def make_config
  if cert_dir.exist?
    config_http = make_https_redirect_host_config(80)
    config_https = make_host_config(443)
    {
      "#{@name}:80" => config_http,
      "*.#{@name}:80" => config_http,
      "#{@name}:443" => config_https,
      "*.#{@name}:443" => config_https,
    }
  else
    config_http = make_host_config(80)
    {
      "#{@name}:80" => config_http,
      "*.#{@name}:80" => config_http,
    }
  end
end

#make_file_dir_handler(dir) ⇒ Object



97
98
99
100
101
# File 'lib/h2o-configurator/host.rb', line 97

def make_file_dir_handler(dir)
  {
    'file.dir' => dir.to_s,
  }
end

#make_handlersObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/h2o-configurator/host.rb', line 67

def make_handlers
  handlers = []
  if htpasswd_file.exist?
    handlers << make_ruby_handler(
      %Q{
        require 'htpasswd'
        Htpasswd.new('#{htpasswd_file}', '#{@name}')
      }
    )
  end
  handlers << make_ruby_external_handler('RedirectHandler')
  handlers << make_ruby_external_handler('AutoExtensionHandler')
  handlers << make_file_dir_handler(@dir)
  handlers
end

#make_host_config(port) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/h2o-configurator/host.rb', line 32

def make_host_config(port)
  config = {
    'listen' => { 'port' => port },
    'access-log' => access_log_file.to_s,
    'setenv' => { 'HOST_DIR' => @dir.to_s },
  }
  if proxy_reverse_url_file.exist?
    config['paths'] = {
      '/' => { 'proxy.reverse.url' => proxy_reverse_url_file.read.chomp }
    }
  else
    config['paths'] = {
      '/' => make_handlers
    }
  end
  if server_certificate_file.exist? && private_key_file.exist?
    config['listen']['ssl'] = {
      'certificate-file' => server_certificate_file.to_s,
      'key-file' => private_key_file.to_s,
    }
  end
  config
end

#make_https_redirect_host_config(port) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/h2o-configurator/host.rb', line 56

def make_https_redirect_host_config(port)
  {
    'listen' => port,
    'paths' => {
      '/' => {
        'redirect' => "https://#{@name}",
      }
    }
  }
end

#make_ruby_external_handler(klass) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/h2o-configurator/host.rb', line 89

def make_ruby_external_handler(klass)
  file = InstalledHandlersDir / H2OConfigurator::Handlers[klass]
  make_ruby_handler %Q{
    require '#{file}'
    H2OConfigurator::#{klass}.new
  }
end

#make_ruby_handler(code) ⇒ Object



83
84
85
86
87
# File 'lib/h2o-configurator/host.rb', line 83

def make_ruby_handler(code)
  {
    'mruby.handler' => code.gsub(/\n\s+/, "\n").strip,
  }
end

#private_key_fileObject



111
112
113
# File 'lib/h2o-configurator/host.rb', line 111

def private_key_file
  cert_dir / H2OConfigurator::PrivateKeyFilename
end

#proxy_reverse_url_fileObject



119
120
121
# File 'lib/h2o-configurator/host.rb', line 119

def proxy_reverse_url_file
  @dir / '.proxy-reverse-url'
end

#server_certificate_fileObject



107
108
109
# File 'lib/h2o-configurator/host.rb', line 107

def server_certificate_file
  cert_dir / H2OConfigurator::ServerCertificateFilename
end