Make caddy reload reactive on modified file

This commit is contained in:
Roberto Alsina 2024-07-06 21:37:30 -03:00
parent 46ff8fc584
commit de46e9864b
4 changed files with 23 additions and 4 deletions

View File

@ -15,4 +15,12 @@ http://*:8888 {
handle_path /admin/* { handle_path /admin/* {
reverse_proxy /* http://127.0.0.1:3000 reverse_proxy /* http://127.0.0.1:3000
} }
handle_path /faaso/exp/* {
reverse_proxy /* http://faaso-exp-6ne49v:3000 {
health_uri /ping
fail_duration 30s
}
}
} }

View File

@ -36,6 +36,10 @@ shards:
git: https://github.com/mamantoha/http_proxy.git git: https://github.com/mamantoha/http_proxy.git
version: 0.10.3 version: 0.10.3
inotify:
git: https://github.com/petoem/inotify.cr.git
version: 1.0.3
kemal: kemal:
git: https://github.com/kemalcr/kemal.git git: https://github.com/kemalcr/kemal.git
version: 1.5.0 version: 1.5.0

View File

@ -30,6 +30,8 @@ dependencies:
github: kemalcr/kemal github: kemalcr/kemal
rucksack: rucksack:
github: busyloop/rucksack github: busyloop/rucksack
inotify:
github: petoem/inotify.cr
scripts: scripts:
postinstall: cat .rucksack >> bin/faaso postinstall: cat .rucksack >> bin/faaso

View File

@ -1,11 +1,17 @@
require "docr"
require "./funko.cr" require "./funko.cr"
require "docr"
require "inotify"
require "kemal" require "kemal"
module Proxy module Proxy
CADDY_CONFIG_PATH = "config/Caddyfile" CADDY_CONFIG_PATH = "config/Caddyfile"
@@current_config = File.read(CADDY_CONFIG_PATH) @@current_config = File.read(CADDY_CONFIG_PATH)
@@watcher = Inotify.watch(CADDY_CONFIG_PATH) do |_|
Log.info { "Reloading caddy config" }
Process.run(command: "caddy", args: ["reload", "--config", CADDY_CONFIG_PATH])
end
# Get current proxy config # Get current proxy config
get "/proxy/" do get "/proxy/" do
@@current_config @@current_config
@ -69,14 +75,13 @@ CONFIG
file << config file << config
end end
# Reload config # Reload config
Process.run(command: "caddy", args: ["reload", "--config", "Caddyfile"])
@@current_config = config @@current_config = config
end end
config config
end end
end end
# Update proxy config once a second # Update proxy config every 1 second (if changed)
spawn do spawn do
loop do loop do
Proxy.update_proxy_config Proxy.update_proxy_config