Module: Nginx::Puma

Defined in:
lib/nginx/puma.rb,
lib/nginx/puma/version.rb,
lib/nginx/puma/patch/puma/dsl.rb

Defined Under Namespace

Modules: Patch Classes: SocketFileName

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.base_dirObject



132
133
134
# File 'lib/nginx/puma.rb', line 132

def base_dir
  "/tmp"
end

.bind_puma_to(sock) ⇒ Object



118
119
120
121
122
# File 'lib/nginx/puma.rb', line 118

def bind_puma_to sock
  binds = puma_binds
  binds << sock
  puma_binds = binds.uniq
end

.conf_fileObject



138
139
140
# File 'lib/nginx/puma.rb', line 138

def conf_file
  "nginx_#{instance}.conf"
end

.create_configObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/nginx/puma.rb', line 56

def create_config
  @config = <<-CONFIG.strip_heredoc
  pid #{pid_file};
  events {}
  http {
    upstream app {
      server #{puma_sock.to_nginx} fail_timeout=0;
    }
    server {
      #{listeners}
      client_max_body_size 1G;
      keepalive_timeout 5;
      server_tokens off;
      access_log #{base_dir}/app_access.log;
      error_log #{base_dir}/app_error.log;
      root #{public_path};
      try_files $uri/index.html $uri.html $uri @app;
      location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
      }
      location ~ ^/(assets)/ {
        root #{public_path};
        gzip_static on;
        expires max;
        add_header Cache-Control public;
        access_log /dev/null;
      }
    }
  }
  CONFIG
  @config =  @config.split("\n").map{|x|x.gsub(/^[ ]+/,'')}.join(' ').gsub(" }","}").gsub("{ ","{").gsub("; ",";")
  File.write "#{base_dir}/#{conf_file}",@config
end

.ensure_shutdownObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nginx/puma.rb', line 37

def ensure_shutdown
  at_exit do
    if File.exists? "#{base_dir}/#{pid_file}"
      pid = File.read("#{base_dir}/#{pid_file}").strip.to_i
      Process.kill "TERM", pid
      File.unlink "#{base_dir}/#{pid_file}" rescue nil
    end
    if File.exists? "#{base_dir}/#{conf_file}"
      File.unlink "#{base_dir}/#{conf_file}" rescue nil
    end
  end
end

.hijack_puma_bindingObject



49
50
51
52
53
54
55
# File 'lib/nginx/puma.rb', line 49

def hijack_puma_binding
  @listens = puma_listeners_of_type(:tcp).map do |bind|
    bind.sub(/:\/\//,':')
  end
  unbind_puma_from :tcp
  bind_puma_to puma_sock.to_puma
end

.instanceObject



23
24
25
# File 'lib/nginx/puma.rb', line 23

def instance
  @instance ||= SecureRandom.hex(6)
end

.listenersObject



101
102
103
104
105
# File 'lib/nginx/puma.rb', line 101

def listeners
  listens.collect do |listen|
    "listen #{listen} default;"
  end.join("\n")
end

.listensObject



98
99
100
# File 'lib/nginx/puma.rb', line 98

def listens
  @listens.empty? ? ['0.0.0.0:9292'] : @listens
end

.pid_fileObject



135
136
137
# File 'lib/nginx/puma.rb', line 135

def pid_file
  "nginx_#{instance}.pid"
end

.public_pathObject



95
96
97
# File 'lib/nginx/puma.rb', line 95

def public_path
  Rails.root.join('public').to_s rescue File.expand_path("./public")
end

.puma_bindsObject



123
124
125
# File 'lib/nginx/puma.rb', line 123

def puma_binds
  ::Puma.cli_config.options[:binds]
end

.puma_listeners_of_type(type) ⇒ Object



106
107
108
109
110
# File 'lib/nginx/puma.rb', line 106

def puma_listeners_of_type type
  puma_binds.select do |bind|
    bind =~ /^#{type}/
  end
end

.puma_sockObject



129
130
131
# File 'lib/nginx/puma.rb', line 129

def puma_sock
  @socket_file_name ||= SocketFileName.new "/tmp/puma_#{instance}.sock"
end

.runObject



26
27
28
29
30
31
32
33
# File 'lib/nginx/puma.rb', line 26

def run
  use :Puma
  ensure_shutdown
  hijack_puma_binding
  create_config
  run_nginx
  puts "Running NGinx"
end

.run_nginxObject



92
93
94
# File 'lib/nginx/puma.rb', line 92

def run_nginx
  `#{start_command}`
end

.start_commandObject



126
127
128
# File 'lib/nginx/puma.rb', line 126

def start_command
  "nginx -p #{base_dir} -c #{conf_file}&"
end

.unbind_puma_from(type) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/nginx/puma.rb', line 111

def unbind_puma_from type
  binds = puma_binds
  binds = binds.reject do |bind|
    bind =~ /^#{type}/
  end.uniq
  puma_binds = binds
end

.use(const) ⇒ Object

Raises:

  • (RuntimeError)


34
35
36
# File 'lib/nginx/puma.rb', line 34

def use const
  raise RuntimeError.new unless Object.const_defined? const
end