#!/usr/bin/env bash
set -euo pipefail
node_id=$(jq -r '.node_id' /etc/guanghu/node.json)
domain_id=$(jq -r '.domain_id' /etc/guanghu/node.json)
forgejo_state=$(systemctl is-active forgejo.service)
timer_state=$(systemctl is-active guanghu-forgejo-backup.timer)
version=$(forgejo --version | head -1)
http_code=$(curl -sS -o /dev/null -w '%{http_code}' http://127.0.0.1:3000/api/healthz)
listeners=$(ss -lntH '( sport = :3000 )' | awk '{print $4}')
[[ $listeners == 127.0.0.1:3000 ]]
jq -n --arg node_id "$node_id" --arg domain_id "$domain_id" --arg forgejo "$forgejo_state" \
  --arg backup_timer "$timer_state" --arg version "$version" --arg http_code "$http_code" --arg bind "$listeners" \
  '{ok:true,node_id:$node_id,domain_id:$domain_id,forgejo:$forgejo,backup_timer:$backup_timer,version:$version,health_http:$http_code,bind:$bind,public_exposure:false}'
