diff --git a/src/daemon-proxyconf.cr b/src/daemon-proxyconf.cr new file mode 100644 index 0000000..03efe85 --- /dev/null +++ b/src/daemon-proxyconf.cr @@ -0,0 +1,59 @@ +require "kemal" + +module Proxy + @@current_config = File.read("tinyproxy.conf") + + # Get current proxy config + get "/proxy/" do + @@current_config + end + + # Bump proxy config to current docker state, returns + # new proxy config + patch "/proxy/" do + Log.info { "Updating routing" } + # Get all the funkos, create routes for them all + update_proxy_config + end + + def self.update_proxy_config + docker_api = Docr::API.new(Docr::Client.new) + containers = docker_api.containers.list(all: true) + + funkos = [] of String + containers.each { |container| + names = container.names.select &.starts_with? "/faaso-" + next if names.empty? + funkos << names[0][7..] + } + funkos.sort! + + config = %( + Port 8888 + Listen 0.0.0.0 + Timeout 600 + Allow 0.0.0.0/0 + ReverseOnly Yes + ReverseMagic Yes + ReversePath "/admin/" "http://127.0.0.1:3000/" + ) + funkos.map { |funko| %(ReversePath "/faaso/#{funko}/" "http://#{funko}:3000/") }.join("\n") + + if @@current_config != config + File.open("tinyproxy.conf", "w") do |file| + file << config + end + # Reload config + Process.run(command: "/usr/bin/killall", args: ["-USR1", "tinyproxy"]) + @@current_config = config + end + config + end +end + +# Update proxy config once a second +spawn do + loop do + Proxy.update_proxy_config + sleep 1.second + end +end diff --git a/src/daemon.cr b/src/daemon.cr index e4ca06e..67b0c67 100644 --- a/src/daemon.cr +++ b/src/daemon.cr @@ -1,4 +1,5 @@ require "./daemon-secrets.cr" +require "./daemon-proxyconf.cr" require "compress/gzip" require "crystar" require "docr" @@ -9,51 +10,6 @@ require "uuid" # FIXME: make configurable basic_auth "admin", "admin" -current_config = File.read("tinyproxy.conf") - -# Get current proxy config -get "/proxy/" do - current_config -end - -# Bump proxy config to current docker state, returns -# new proxy config -patch "/proxy/" do - Log.info { "Updating routing" } - # Get all the funkos, create routes for them all - docker_api = Docr::API.new(Docr::Client.new) - containers = docker_api.containers.list(all: true) - - funkos = [] of String - containers.each { |container| - names = container.names.select &.starts_with? "/faaso-" - next if names.empty? - funkos << names[0][7..] - } - - funkos.sort! - - proxy_config = %( -Port 8888 -Listen 0.0.0.0 -Timeout 600 -Allow 0.0.0.0/0 -ReverseOnly Yes -ReverseMagic Yes -ReversePath "/admin/" "http://127.0.0.1:3000/" -) + funkos.map { |funko| %(ReversePath "/faaso/#{funko}/" "http://#{funko}:3000/") }.join("\n") - - if current_config != proxy_config - File.open("tinyproxy.conf", "w") do |file| - file << proxy_config - end - # Reload config - Process.run(command: "/usr/bin/killall", args: ["-USR1", "tinyproxy"]) - current_config = proxy_config - end - proxy_config -end - # Bring up the funko get "/funko/:name/up/" do |env| name = env.params.url["name"] diff --git a/tinyproxy.conf b/tinyproxy.conf index a77c81e..cf1e0e5 100644 --- a/tinyproxy.conf +++ b/tinyproxy.conf @@ -1,9 +1,9 @@ -Port 8888 -Listen 0.0.0.0 -Timeout 600 -Allow 0.0.0.0/0 -ReverseOnly Yes -ReverseMagic Yes -ReversePath "/admin/" "http://127.0.0.1:3000/" -ReversePath "/faaso/hello/" "http://hello:3000/" \ No newline at end of file + Port 8888 + Listen 0.0.0.0 + Timeout 600 + Allow 0.0.0.0/0 + ReverseOnly Yes + ReverseMagic Yes + ReversePath "/admin/" "http://127.0.0.1:3000/" + \ No newline at end of file