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/* {
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
version: 0.10.3
inotify:
git: https://github.com/petoem/inotify.cr.git
version: 1.0.3
kemal:
git: https://github.com/kemalcr/kemal.git
version: 1.5.0

View File

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

View File

@ -1,11 +1,17 @@
require "docr"
require "./funko.cr"
require "docr"
require "inotify"
require "kemal"
module Proxy
CADDY_CONFIG_PATH = "config/Caddyfile"
@@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 "/proxy/" do
@@current_config
@ -69,14 +75,13 @@ CONFIG
file << config
end
# Reload config
Process.run(command: "caddy", args: ["reload", "--config", "Caddyfile"])
@@current_config = config
end
config
end
end
# Update proxy config once a second
# Update proxy config every 1 second (if changed)
spawn do
loop do
Proxy.update_proxy_config