Created
June 8, 2015 18:34
-
-
Save ryanong/97974c5597c9f5dc7f92 to your computer and use it in GitHub Desktop.
Capybara Puma NGINX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/capybara.rb | |
require "capybara" | |
require "puma/server" | |
require "support/nginx" | |
Capybara.server do |app, port| | |
server = Puma::Server.new(app) | |
server.min_threads = 1 | |
server.max_threads = 1 | |
if ENV["PRECOMPILE"] == "true" | |
FileUtils.mkdir_p "tmp/sockets" | |
puma_sock = "tmp/sockets/puma.#{Process.pid}.sock" | |
server.add_unix_listener(puma_sock) | |
threads = server.run | |
nginx_server = Support::Nginx.new( | |
"#{Capybara.server_host}:#{port}", | |
"unix:#{puma_sock}" | |
) | |
nginx_server.start | |
threads.join | |
else | |
server.add_tcp_listener Capybara.server_host, port | |
server.run.join | |
end | |
end | |
RSpec.configure do |config| | |
if ENV["PRECOMPILE"] == "true" | |
config.after(:suite) do | |
nginx_pid = "tmp/nginx.#{Process.pid}.pid" | |
if File.exists? nginx_pid | |
pid = File.read(nginx_pid).strip.to_i | |
Process.kill "TERM", pid | |
File.unlink nginx_pid rescue nil | |
end | |
nginx_conf = "tmp/nginx.#{Process.pid}.conf" | |
if File.exists? nginx_conf | |
File.unlink nginx_conf rescue nil | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/nginx/mime.types | |
types { | |
text/html html htm shtml; | |
text/css css; | |
text/xml xml; | |
image/gif gif; | |
image/jpeg jpeg jpg; | |
application/javascript js; | |
application/atom+xml atom; | |
application/rss+xml rss; | |
text/mathml mml; | |
text/plain txt; | |
text/vnd.sun.j2me.app-descriptor jad; | |
text/vnd.wap.wml wml; | |
text/x-component htc; | |
image/png png; | |
image/tiff tif tiff; | |
image/vnd.wap.wbmp wbmp; | |
image/x-icon ico; | |
image/x-jng jng; | |
image/x-ms-bmp bmp; | |
image/svg+xml svg svgz; | |
image/webp webp; | |
application/font-woff woff; | |
application/java-archive jar war ear; | |
application/json json; | |
application/mac-binhex40 hqx; | |
application/msword doc; | |
application/pdf pdf; | |
application/postscript ps eps ai; | |
application/rtf rtf; | |
application/vnd.apple.mpegurl m3u8; | |
application/vnd.ms-excel xls; | |
application/vnd.ms-fontobject eot; | |
application/vnd.ms-powerpoint ppt; | |
application/vnd.wap.wmlc wmlc; | |
application/vnd.google-earth.kml+xml kml; | |
application/vnd.google-earth.kmz kmz; | |
application/x-7z-compressed 7z; | |
application/x-cocoa cco; | |
application/x-java-archive-diff jardiff; | |
application/x-java-jnlp-file jnlp; | |
application/x-makeself run; | |
application/x-perl pl pm; | |
application/x-pilot prc pdb; | |
application/x-rar-compressed rar; | |
application/x-redhat-package-manager rpm; | |
application/x-sea sea; | |
application/x-shockwave-flash swf; | |
application/x-stuffit sit; | |
application/x-tcl tcl tk; | |
application/x-x509-ca-cert der pem crt; | |
application/x-xpinstall xpi; | |
application/xhtml+xml xhtml; | |
application/xspf+xml xspf; | |
application/zip zip; | |
application/octet-stream bin exe dll; | |
application/octet-stream deb; | |
application/octet-stream dmg; | |
application/octet-stream iso img; | |
application/octet-stream msi msp msm; | |
application/vnd.openxmlformats-officedocument.wordprocessingml.document docx; | |
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx; | |
application/vnd.openxmlformats-officedocument.presentationml.presentation pptx; | |
audio/midi mid midi kar; | |
audio/mpeg mp3; | |
audio/ogg ogg; | |
audio/x-m4a m4a; | |
audio/x-realaudio ra; | |
video/3gpp 3gpp 3gp; | |
video/mp2t ts; | |
video/mp4 mp4; | |
video/mpeg mpeg mpg; | |
video/quicktime mov; | |
video/webm webm; | |
video/x-flv flv; | |
video/x-m4v m4v; | |
video/x-mng mng; | |
video/x-ms-asf asx asf; | |
video/x-ms-wmv wmv; | |
video/x-msvideo avi; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/nginx/nginx.conf.erb | |
master_process off; # this allows ruby to kill the process after specs finish | |
pid tmp/nginx.<%= pid %>.pid; | |
error_log log/nginx_text_error.<%= pid %>.log; | |
events { | |
worker_connections 1024; | |
} | |
http { | |
include <%= mime_types_path %>; | |
default_type application/octet-stream; | |
access_log log/access.<%= pid %>.log; | |
sendfile on; | |
#keepalive_timeout 0; | |
keepalive_timeout 65; | |
upstream app { | |
server <%= upstream %> fail_timeout=0; | |
} | |
server { | |
listen <%= listen %> default; | |
client_max_body_size 1G; | |
keepalive_timeout 5; | |
server_tokens off; | |
root public; | |
try_files $uri/index.html $uri @app; | |
location ^~ /assets/ { | |
gzip_static on; | |
expires max; | |
add_header Cache-Control public; | |
access_log /dev/null; | |
} | |
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/support/nginx.rb | |
require "erb" | |
require "childprocess" | |
module Support | |
class Nginx | |
CONF_TEMPLATE_PATH = File.expand_path("../nginx/nginx.conf.erb", __FILE__) | |
MIME_TYPES_PATH = File.expand_path("../nginx/mime.types", __FILE__) | |
attr_reader :listen, :upstream | |
def initialize(listen, upstream) | |
@listen = listen | |
@upstream = upstream | |
end | |
def start | |
create_config | |
process.start | |
at_exit do | |
quit | |
end | |
true | |
end | |
def quit | |
if process.alive? | |
process.quit | |
end | |
if File.exists? conf_path | |
File.unlink conf_path | |
end | |
end | |
def pid | |
Process.pid | |
end | |
def conf_path | |
"tmp/nginx.#{pid}.conf" | |
end | |
def create_config | |
b = binding | |
config = ERB.new(File.read(CONF_TEMPLATE_PATH)).result(b) | |
File.write conf_path, config | |
end | |
def process | |
@process ||= ChildProcess.build("nginx", "-p", ".", "-c", conf_path).tap do |process| | |
process.io.inherit! | |
end | |
end | |
def mime_types_path | |
MIME_TYPES_PATH | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment