require "kemal" require "./types" require "./billing_manager" module ContainerMom VERSION = "0.1.0" # Initialize billing manager billing = BillingManager.instance # Set up Kemal routes post "/verify" do |env| begin request = JSON.parse(env.request.body.not_nil!) name = request["name"].as_s namespace = request["namespace"].as_s spec = ContainerDeploymentSpec.from_json(request["spec"].to_json) result = billing.verify_billing(name, namespace, spec) env.response.content_type = "application/json" { status: result ? "approved" : "denied", message: result ? "Billing verification passed" : "Resource limits exceeded" }.to_json rescue ex : JSON::ParseException env.response.status_code = 400 {status: "error", message: "Invalid JSON payload"}.to_json rescue ex : BillingManager::Error env.response.status_code = 400 {status: "error", message: ex.message}.to_json rescue ex env.response.status_code = 500 {status: "error", message: "Internal server error"}.to_json end end post "/track-usage" do |env| begin request = JSON.parse(env.request.body.not_nil!) name = request["name"].as_s namespace = request["namespace"].as_s spec = ContainerDeploymentSpec.from_json(request["spec"].to_json) billing.track_usage(name, namespace, spec) env.response.content_type = "application/json" {status: "success", message: "Usage tracked"}.to_json rescue ex : JSON::ParseException env.response.status_code = 400 {status: "error", message: "Invalid JSON payload"}.to_json rescue ex : BillingManager::Error env.response.status_code = 400 {status: "error", message: ex.message}.to_json rescue ex env.response.status_code = 500 {status: "error", message: "Internal server error"}.to_json end end get "/health" do |env| env.response.content_type = "application/json" { status: "healthy", version: VERSION }.to_json end # Start the server Kemal.run do |config| server = config.server.not_nil! server.bind_tcp "0.0.0.0", 8082 end end