use jsonify() helper
Some checks failed
Gitea Actions Demo / test (push) Blocked by required conditions
Gitea Actions Demo / release-image (push) Blocked by required conditions
Gitea Actions Demo / lint (push) Has been cancelled

This commit is contained in:
Ryan Cavicchioni 2025-03-09 15:34:57 -05:00
parent a4955d35fa
commit fde1dd14b5
Signed by: ryanc
SSH Key Fingerprint: SHA256:KbXiwUnZnHFwFtt3Bytd+F3FN9pPHn1Z1cxMIE1TPbg

25
app.rb
View File

@ -216,12 +216,13 @@ before do
end
helpers do
def json(obj, opts: nil, pretty: false)
if pretty
def jsonify(obj, opts: nil, pretty: false)
buf = if pretty
JSON.pretty_generate obj, opts:
else
JSON.generate(obj, opts:)
end
"#{buf}\n"
end
def protected! hidden = false
@ -251,14 +252,14 @@ end
get "/env", provides: "json" do
pretty = params.key? :pretty
json ENV.sort.to_h, pretty:
jsonify ENV.sort.to_h, pretty:
end
get "/headers", provides: "json" do
pretty = params.key? :pretty
h = req_headers
json h, pretty:
jsonify h, pretty:
end
get "/livez" do
@ -273,7 +274,7 @@ 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
jsonify x
end
post "/livez/toggle" do
@ -337,7 +338,7 @@ end
get "/pid" do
pretty = params.key? :pretty
json({puma: master_pid, pid: Process.pid}, pretty:)
jsonify({puma: master_pid, pid: Process.pid}, pretty:)
end
get "/token" do
@ -347,31 +348,31 @@ get "/token" do
token = JWT.encode payload, JWT_SECRET, "HS256"
x = {token: token, expires_at: expires_at}
json x
jsonify x
end
get "/token/validate" do
token = req_headers["authorization"].split[1]
payload = JWT.decode token, JWT_SECRET, true, algorithm: "HS256"
json payload
jsonify payload
end
post "/session" do
session.merge! params
json session.to_hash
jsonify session.to_hash
end
get "/session" do
j = session.to_hash
j[:hostname] = ENV["HOSTNAME"]
json j
jsonify j
end
get "/cookies" do
json response.headers
jsonify response.headers
end
get "/_cat/headers" do
@ -422,5 +423,5 @@ route :delete, :get, :patch, :post, :put, "/auth/basic", provides: "json" do
protected!
end
json({authenticated: true, user: @auth.username}, pretty:)
jsonify({authenticated: true, user: @auth.username}, pretty:)
end