From de46e9864b954b8fdece73a0dcaa048dd7d527f7 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sat, 6 Jul 2024 21:37:30 -0300 Subject: [PATCH] Make caddy reload reactive on modified file --- config/Caddyfile | 8 ++++++++ shard.lock | 4 ++++ shard.yml | 4 +++- src/daemon/proxy.cr | 11 ++++++++--- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/config/Caddyfile b/config/Caddyfile index 29669d7..276e7fd 100644 --- a/config/Caddyfile +++ b/config/Caddyfile @@ -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 + } + } + } diff --git a/shard.lock b/shard.lock index dbfe162..cccfdd6 100644 --- a/shard.lock +++ b/shard.lock @@ -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 diff --git a/shard.yml b/shard.yml index 1c792aa..eb29fd1 100644 --- a/shard.yml +++ b/shard.yml @@ -30,6 +30,8 @@ dependencies: github: kemalcr/kemal rucksack: github: busyloop/rucksack - + inotify: + github: petoem/inotify.cr + scripts: postinstall: cat .rucksack >> bin/faaso \ No newline at end of file diff --git a/src/daemon/proxy.cr b/src/daemon/proxy.cr index bb2559c..281bcc1 100644 --- a/src/daemon/proxy.cr +++ b/src/daemon/proxy.cr @@ -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