rewire frontend actions

This commit is contained in:
Roberto Alsina 2024-07-03 16:44:04 -03:00
parent a6d15516ff
commit ed621a75eb
3 changed files with 43 additions and 15 deletions

View File

@ -73,6 +73,9 @@ module Funko
end end
end end
# Endpoints for the web frontend
# General status for the front page
get "/funkos/" do |env| get "/funkos/" do |env|
funkos = Funko.from_docker funkos = Funko.from_docker
funkos.sort! { |a, b| a.name <=> b.name } funkos.sort! { |a, b| a.name <=> b.name }
@ -93,7 +96,35 @@ module Funko
result.to_json result.to_json
end end
end end
# Stop => scale to 0
get "/funkos/:name/stop" do |env|
name = env.params.url["name"]
funko = Funko.from_names([name])[0]
funko.scale(0)
funko.wait_for(0, 1)
end
# Start => scale to 1
get "/funkos/:name/start" do |env|
name = env.params.url["name"]
funko = Funko.from_names([name])[0]
if funko.scale == 0
funko.scale(1)
funko.wait_for(1, 1)
end
end
# Restart => scale to 0, then 1
get "/funkos/:name/restart" do |env|
name = env.params.url["name"]
funko = Funko.from_names([name])[0]
funko.scale(0)
funko.wait_for(0, 1)
funko.scale(1)
funko.wait_for(1, 1)
end
# Helper to run faaso locally and get a response back
def run_faaso(args : Array(String)) def run_faaso(args : Array(String))
Log.info { "Running faaso [#{args.join(", ")}, -l]" } Log.info { "Running faaso [#{args.join(", ")}, -l]" }
output = IO::Memory.new output = IO::Memory.new

View File

@ -234,25 +234,21 @@ module Funko
end end
# Wait up to `t` seconds for the funko to reach the requested `state` # Wait up to `t` seconds for the funko to reach the requested `state`
def wait_for(state : String, t) def wait_for(new_scale : Int, t)
channel = Channel(Nil).new channel = Channel(Nil).new
spawn do spawn do
sleep 0.1.seconds loop do
case state channel.send(nil) if scale == new_scale
when "exited" sleep 0.2.seconds
if self.exited?
channel.send(nil)
end
when "running"
if self.running?
channel.send(nil)
end
when "paused"
if self.paused?
channel.send(nil)
end
end end
end end
select
when channel.receive
Log.info { "Funko #{name} reached scale #{new_scale}" }
when timeout(t.seconds)
Log.error { "Funko #{name} did not reach scale #{new_scale} in #{t} seconds" }
end
end end
# Create a container for this funko # Create a container for this funko

View File

@ -8,3 +8,4 @@
ReverseOnly Yes ReverseOnly Yes
ReverseMagic Yes ReverseMagic Yes
ReversePath "/admin/" "http://127.0.0.1:3000/" ReversePath "/admin/" "http://127.0.0.1:3000/"
ReversePath "/faaso/hello-fkvrm2/" "http://hello-fkvrm2:3000/"