Commit 7b295ee2 authored by Terence Lee's avatar Terence Lee

separate NginxConfig + regex into a separate file

parent 3ebad8d8
require 'json'
require_relative 'nginx_config_util'
class NginxConfig
def initialize(json_file)
json = {}
json = JSON.parse(File.read(json_file)) if File.exist?(json_file)
json["worker_connections"] ||= ENV["WORKER_CONNECTIONS"] || 512
json["port"] ||= ENV["PORT"] || 5000
json["root"] ||= "public_html/"
json["proxies"] ||= {}
json["proxies"].each do |loc, hash|
if hash["origin"][-1] != "/"
json["proxies"][loc].merge!("origin" => hash["origin"] + "/")
end
end
json["clean_urls"] ||= false
json["routes"] ||= {}
json["routes"] = Hash[json["routes"].map {|route, target| [NginxConfigUtil.to_regex(route), target] }]
json["redirects"] ||= {}
json["error_page"] ||= nil
json.each do |key, value|
self.class.send(:define_method, key) { value }
end
end
def context
binding
end
end
module NginxConfigUtil
def self.to_regex(path)
segments = []
while !path.empty?
if path[0...2] == '**'
segments << '.*'
path = path[2..-1]
elsif path[0...1] == '*'
segments << '[^/]*'
path = path[1..-1]
else
next_star = path.index("*") || path.length
segments << Regexp.escape(path[0...next_star])
path = path[next_star..-1]
end
end
segments.join
end
end
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'fileutils' require 'fileutils'
require 'erb'
require_relative 'lib/nginx_config'
TEMPLATE = File.join(File.dirname(__FILE__), 'templates/nginx.conf.erb') TEMPLATE = File.join(File.dirname(__FILE__), 'templates/nginx.conf.erb')
USER_CONFIG = 'static.json' USER_CONFIG = 'static.json'
NGINX_CONFIG = 'config/nginx.conf' NGINX_CONFIG = 'config/nginx.conf'
require 'erb' erb = ERB.new(File.read(TEMPLATE)).result(NginxConfig.new(USER_CONFIG).context)
require 'json' File.write(NGINX_CONFIG, erb)
class NginxConfig
def initialize(json_file)
json = {}
json = JSON.parse(File.read(json_file)) if File.exist?(json_file)
json["worker_connections"] ||= ENV["WORKER_CONNECTIONS"] || 512
json["port"] ||= ENV["PORT"] || 5000
json["root"] ||= "public_html/"
json["proxies"] ||= {}
json["proxies"].each do |loc, hash|
if hash["origin"][-1] != "/"
json["proxies"][loc].merge!("origin" => hash["origin"] + "/")
end
end
json["clean_urls"] ||= false
json["routes"] ||= {}
json["routes"] = Hash[json["routes"].map { |route, target| [self.class.to_regex(route), target] }]
json["redirects"] ||= {}
json["error_page"] ||= nil
json.each do |key, value|
self.class.send(:define_method, key) { value }
end
end
def context
binding
end
def self.to_regex(path)
segments = []
while !path.empty?
if path[0...2] == '**'
segments << '.*'
path = path[2..-1]
elsif path[0...1] == '*'
segments << '[^/]*'
path = path[1..-1]
else
next_star = path.index("*") || path.length
segments << Regexp.escape(path[0...next_star])
path = path[next_star..-1]
end
end
segments.join
end
end
if __FILE__ == $0
erb = ERB.new(File.read(TEMPLATE)).result(NginxConfig.new(USER_CONFIG).context)
File.write(NGINX_CONFIG, erb)
end
require_relative "spec_helper"
load File.join(File.dirname(__FILE__), '../scripts/config/make-config')
RSpec.describe "NginxConfig#to_regex" do
samples = [
['/foo/', '/foo/'],
['/foo/*', '/foo/[^/]*'],
['/foo/**', '/foo/.*'],
['/cache/*', '/cache/[^/]*'],
]
samples.each do |(input, output)|
it "converts #{input} to #{output}" do
result = NginxConfig.to_regex(input)
expect(result).to eq output
end
end
end
require_relative "../../../spec_helper"
require_relative "../../../../scripts/config/lib/nginx_config_util"
RSpec.describe NginxConfigUtil do
describe ".to_regex" do
samples = [
['/foo/', '/foo/'],
['/foo/*', '/foo/[^/]*'],
['/foo/**', '/foo/.*']
]
samples.each do |(input, output)|
it "converts #{input} to #{output}" do
result = NginxConfigUtil.to_regex(input)
expect(result).to eq output
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment