From 6d1651f3ec01d56f52e797f8b3a324e84b5c4a2e Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Wed, 3 Jul 2024 20:54:19 -0300 Subject: [PATCH] Beginnings of secret admin w/dialog --- examples/hello_crystal/funko.cr | 6 +-- public/bars.svg | 52 ++++++++++++++++++++++++++ public/index.html | 66 ++++++++++++++++++++++++++++++--- src/daemon/proxyconf.cr | 2 +- src/daemon/secrets.cr | 23 ++++++++++-- src/funko.cr | 4 +- src/views/funkos.ecr | 2 +- src/views/secrets.ecr | 11 ++++++ tinyproxy.conf | 2 +- 9 files changed, 152 insertions(+), 16 deletions(-) create mode 100644 public/bars.svg create mode 100644 src/views/secrets.ecr diff --git a/examples/hello_crystal/funko.cr b/examples/hello_crystal/funko.cr index a397946..75828c0 100644 --- a/examples/hello_crystal/funko.cr +++ b/examples/hello_crystal/funko.cr @@ -1,7 +1,7 @@ require "kemal" - + # This is a kemal app, you can add handlers, middleware, etc. - + # A basic hello world get endpoint get "/" do "Hello World Crystal!" @@ -10,7 +10,7 @@ end # The `/ping/` endpoint is configured in the container as a healthcheck # You can make it better by checking that your database is responding # or whatever checks you think are important -# +# get "/ping/" do "OK" end diff --git a/public/bars.svg b/public/bars.svg new file mode 100644 index 0000000..7cb07e6 --- /dev/null +++ b/public/bars.svg @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/public/index.html b/public/index.html index 0cf0243..c1a3ac3 100644 --- a/public/index.html +++ b/public/index.html @@ -1,15 +1,23 @@ + +
-

Your Funko Collection

+

FaaSO Admin Interface

+

Your Funko Collection + +

@@ -24,12 +32,60 @@
-
- +
+

+ Your Secrets + + +

+ + + + + + + + + + + + + New Secret +
+ + + + + + +
+ + + \ No newline at end of file diff --git a/src/daemon/proxyconf.cr b/src/daemon/proxyconf.cr index 114deb4..0afef86 100644 --- a/src/daemon/proxyconf.cr +++ b/src/daemon/proxyconf.cr @@ -39,7 +39,7 @@ module Proxy ReverseOnly Yes ReverseMagic Yes ReversePath "/admin/" "http://127.0.0.1:3000/" - ) + funkos.map { |funko| %(ReversePath "/faaso/#{funko}/" "http://#{funko}:3000/") }.join("\n") + ) + funkos.map { |funko| %(ReversePath "/faaso/#{funko.split("-")[0]}/" "http://#{funko}:3000/") }.join("\n") if @@current_config != config File.open("tinyproxy.conf", "w") do |file| diff --git a/src/daemon/secrets.cr b/src/daemon/secrets.cr index d73e667..2c95158 100644 --- a/src/daemon/secrets.cr +++ b/src/daemon/secrets.cr @@ -7,15 +7,30 @@ module Secrets # Gets a secret in form {"name": "funko_name-secret_name", "value": "secret_value"} post "/secrets/" do |env| - name = env.params.json["name"].as(String) - value = env.params.json["value"].as(String) - SECRETS[name] = value + funko = env.params.body["funko"].as(String) + name = env.params.body["name"].as(String) + value = env.params.body["value"].as(String) + if funko.empty? || name.empty? || value.empty? + halt env, status_code: 400, response: "Bad request" + end + SECRETS["#{funko}-#{name}"] = value Secrets.update_secrets halt env, status_code: 201, response: "Created" end get "/secrets/" do |env| - halt env, status_code: 200, response: SECRETS.keys.to_json + result = [] of Hash(String, String) + SECRETS.each { |k, _| + result << { + "funko" => k.split("-")[0], + "name" => k.split("-", 2)[1], + } + } + if env.params.query.fetch("format", "json") == "html" + render "src/views/secrets.ecr" + else + result.to_json + end end # Deletes a secret from the disk and memory diff --git a/src/funko.cr b/src/funko.cr index aaa28f0..5b37d32 100644 --- a/src/funko.cr +++ b/src/funko.cr @@ -96,10 +96,11 @@ module Funko Log.info { "Scaling #{name} from #{current_scale} to #{new_scale}" } if new_scale > current_scale - Log.info { "Adding instance" } (current_scale...new_scale).each { + Log.info { "Adding instance" } id = create_container start(id) + sleep 0.1.seconds } else containers.select { |container| container.@state == "running" }.sort! { |i, j| @@ -109,6 +110,7 @@ module Funko docker_api.containers.stop(container.@id) current_scale -= 1 break if current_scale == new_scale + sleep 0.1.seconds } end diff --git a/src/views/funkos.ecr b/src/views/funkos.ecr index d9050aa..2e96dff 100644 --- a/src/views/funkos.ecr +++ b/src/views/funkos.ecr @@ -17,7 +17,7 @@ <%- end -%> <%- end -%> - " src="https://htmx.org/img/bars.svg" class="htmx-indicator"> + " src="bars.svg" class="htmx-indicator"> <%- end -%> diff --git a/src/views/secrets.ecr b/src/views/secrets.ecr new file mode 100644 index 0000000..d9899d4 --- /dev/null +++ b/src/views/secrets.ecr @@ -0,0 +1,11 @@ +<%- result.each do |f| -%> +"> + + + + +<%- end -%> diff --git a/tinyproxy.conf b/tinyproxy.conf index dc36489..d4619fb 100644 --- a/tinyproxy.conf +++ b/tinyproxy.conf @@ -8,4 +8,4 @@ ReverseOnly Yes ReverseMagic Yes ReversePath "/admin/" "http://127.0.0.1:3000/" - ReversePath "/faaso/hello-fkvrm2/" "http://hello-fkvrm2:3000/" \ No newline at end of file + ReversePath "/faaso/hello/" "http://hello-c9ge0d:3000/" \ No newline at end of file
FunkoNameActions
<%= f["funko"] %><%= f["name"] %> + + + " src="bars.svg" class="htmx-indicator"> +