Skip to content

Instantly share code, notes, and snippets.

@mattr-
Created April 26, 2017 15:47
Show Gist options
  • Save mattr-/772ff14d25491dc44ec553a48c755776 to your computer and use it in GitHub Desktop.
Save mattr-/772ff14d25491dc44ec553a48c755776 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'optparse'
require 'erb'
class GeneratesConfig
attr_accessor :repo_dir, :output_file, :host, :port
def initialize
@host = "localhost"
end
def generate
raise "Unable to generate config" unless repo_dir && output_file
@name = File.basename(repo_dir)
@server_name = @name
@host_name = "#{@server_name}.teladoc.dev"
@hostname_with_port = "http://#{host}:#{port}"
output = ERB.new(File.read("./nginx.conf.erb")).result(binding)
File.write(output_file, output)
end
end
config_generator = GeneratesConfig.new
OptionParser.new do |options|
options.on("-h", "--host", "Hostname to proxy through nginx") do |host|
config_generator.host = host
end
options.on("-p", "--port PORT", "Hostname to proxy through nginx") do |port|
config_generator.port = port
end
options.on("-d", "--dir DIR", "Repository directory for the application you want to proxy through nginx") do |repo_dir|
config_generator.repo_dir = repo_dir
end
options.on("-o", "--outfile FILE", "Output location for the file that will contain the nginx config") do |output_file|
config_generator.output_file = output_file
end
end.parse!
config_generator.generate
upstream <%= @server_name %> {
server <%= @hostname_with_port %>;
}
server {
access_log /usr/local/var/log/nginx/<%= @name %>.access.log main;
listen 80;
root <%= @repo_dir %>/public;
server_name <%= @host_name %> *.<%= @host_name %>;
client_max_body_size 50M;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
try_files $uri/index.html $uri @<%= @server_name %>;
location @<%= @server_name %> {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://<%= @server_name %>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment