Use the new json() helper
All checks were successful
Gitea Actions Demo / lint (push) Successful in 1m26s
Gitea Actions Demo / test (push) Successful in 1m19s
Gitea Actions Demo / release-image (push) Successful in 1m6s

This commit is contained in:
Ryan Cavicchioni 2024-09-04 17:07:52 -05:00
parent 6f4b7335f8
commit 059ed64805
Signed by: ryanc
GPG Key ID: 877EEDAF9245103D

20
app.rb
View File

@ -1,6 +1,5 @@
require "bundler/setup" require "bundler/setup"
require "sinatra" require "sinatra"
require "sinatra/json"
require "sinatra/cookies" require "sinatra/cookies"
require "sinatra/multi_route" require "sinatra/multi_route"
require "time" require "time"
@ -243,18 +242,16 @@ get "/" do
end end
get "/env", provides: "json" do 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 ENV.sort.to_h, pretty:
JSON.generate ENV.sort.to_h
end end
get "/headers", provides: "json" do get "/headers", provides: "json" do
pretty = params.key? :pretty
h = req_headers h = req_headers
return JSON.pretty_generate h if params.key? "pretty"
JSON.generate h json h, pretty:
end end
get "/livez" do get "/livez" do
@ -268,6 +265,7 @@ end
get "/livez/uptime" do get "/livez/uptime" do
tt = TickTock.new tt = TickTock.new
x = {started_at: tt.started_at, seconds: tt.uptime.to_i, human: human_time(tt.uptime.to_i)} x = {started_at: tt.started_at, seconds: tt.uptime.to_i, human: human_time(tt.uptime.to_i)}
json x json x
end end
@ -330,7 +328,9 @@ post "/halt" do
end end
get "/pid" do get "/pid" do
JSON.generate({puma: master_pid, pid: Process.pid}) pretty = params.key? :pretty
json({puma: master_pid, pid: Process.pid}, pretty:)
end end
get "/token" do get "/token" do
@ -339,23 +339,27 @@ get "/token" do
expires_at = Time.at(exp).to_datetime expires_at = Time.at(exp).to_datetime
token = JWT.encode payload, JWT_SECRET, "HS256" token = JWT.encode payload, JWT_SECRET, "HS256"
x = {token: token, expires_at: expires_at} x = {token: token, expires_at: expires_at}
json x json x
end end
get "/token/validate" do get "/token/validate" do
token = req_headers["authorization"].split[1] token = req_headers["authorization"].split[1]
payload = JWT.decode token, JWT_SECRET, true, algorithm: "HS256" payload = JWT.decode token, JWT_SECRET, true, algorithm: "HS256"
json payload json payload
end end
post "/session" do post "/session" do
session.merge! params session.merge! params
json session.to_hash json session.to_hash
end end
get "/session" do get "/session" do
j = session.to_hash j = session.to_hash
j[:hostname] = ENV["HOSTNAME"] j[:hostname] = ENV["HOSTNAME"]
json j json j
end end