From 059ed648051ea1b0a90e7c253c500fb59223a04f Mon Sep 17 00:00:00 2001 From: Ryan Cavicchioni Date: Wed, 4 Sep 2024 17:07:52 -0500 Subject: [PATCH] Use the new json() helper --- app.rb | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app.rb b/app.rb index cfe5f33..8f06463 100644 --- a/app.rb +++ b/app.rb @@ -1,6 +1,5 @@ require "bundler/setup" require "sinatra" -require "sinatra/json" require "sinatra/cookies" require "sinatra/multi_route" require "time" @@ -243,18 +242,16 @@ get "/" do end get "/env", provides: "json" do - content_type :json + pretty = params.key? :pretty - return JSON.pretty_generate ENV.sort.to_h if params.key? "pretty" - - JSON.generate ENV.sort.to_h + json ENV.sort.to_h, pretty: end get "/headers", provides: "json" do + pretty = params.key? :pretty h = req_headers - return JSON.pretty_generate h if params.key? "pretty" - JSON.generate h + json h, pretty: end get "/livez" do @@ -268,6 +265,7 @@ end get "/livez/uptime" do tt = TickTock.new x = {started_at: tt.started_at, seconds: tt.uptime.to_i, human: human_time(tt.uptime.to_i)} + json x end @@ -330,7 +328,9 @@ post "/halt" do end get "/pid" do - JSON.generate({puma: master_pid, pid: Process.pid}) + pretty = params.key? :pretty + + json({puma: master_pid, pid: Process.pid}, pretty:) end get "/token" do @@ -339,23 +339,27 @@ get "/token" do expires_at = Time.at(exp).to_datetime token = JWT.encode payload, JWT_SECRET, "HS256" x = {token: token, expires_at: expires_at} + json x end get "/token/validate" do token = req_headers["authorization"].split[1] payload = JWT.decode token, JWT_SECRET, true, algorithm: "HS256" + json payload end post "/session" do session.merge! params + json session.to_hash end get "/session" do j = session.to_hash j[:hostname] = ENV["HOSTNAME"] + json j end