Files
nix/modules/homelab/services/glance/test.nix

15 lines
4.1 KiB
Nix

[
{
type = "custom-api";
title = "Crafty Controller Minecraft Server";
cache = "5s";
allow-insecure = true;
url = "\${CRAFTY_URL}/api/v2/servers/\${CRAFTY_SERVER_ID}/stats";
headers = {
Authorization = "Bearer \${CRAFTY_API_TOKEN}";
Accept = "application/json";
};
template = "<!-- USER SETTINGS SECTION -->\n\n<!-- Change true to false if you wish to not display the MOTD -->\n{{ $displayMOTD := true }}\n\n<!-- END OF USER SETTINGS SECTION -->\n\n{{ $is_running := .JSON.Bool \"data.running\" }}\n{{ $online_players := .JSON.Int \"data.online\" | formatNumber }}\n{{ $max_players := .JSON.Int \"data.max\" | formatNumber }}\n{{ $name := .JSON.String \"data.world_name\" }}\n{{ $size := .JSON.String \"data.world_size\" }}\n{{ $version := .JSON.String \"data.version\" }}\n{{ $icon := .JSON.String \"data.icon\" }}\n{{ $server_ip := .JSON.String \"data.server_id.server_ip\" }}\n{{ $server_port := .JSON.String \"data.server_id.server_port\" }}\n{{ $motd := .JSON.String \"data.desc\" }}\n\n{{ $server_addr := \"\" }}\n{{ if and ($is_running) (eq $server_ip \"127.0.0.1\") }}\n {{ $server_addr = printf \"%s:%s\" (replaceMatches \"https?://\" \"\" \"\${CRAFTY_URL}\") $server_port }}\n{{ else if $is_running }}\n {{ $server_addr = printf \"%s:%s\" $server_ip $server_port }}\n{{ end }}\n\n{{ $starting := false }}\n{{ if and ($is_running) (eq $max_players \"0\") (eq $version \"False\") }}\n {{ $starting = true }}\n{{ end }}\n\n<!-- I couldn't find documentation describing the \"waiting_start\" state or the other booleans below. Implementation might not be correct. -->\n{{ $updating := .JSON.Bool \"data.updating\" }}\n{{ $importing := .JSON.Bool \"data.importing\" }}\n{{ $crashed := .JSON.Bool \"data.crashed\" }}\n\n<div style=\"display:flex; align-items:center; gap:12px;\">\n <!-- Server Icon -->\n <div style=\"width:40px; height:40px; flex-shrink:0; border-radius:4px; display:flex; justify-content:center; align-items:center; overflow:hidden;\">\n {{ if eq $icon \"\" }}\n <img src=\"https://cdn.jsdelivr.net/gh/selfhst/icons/png/minecraft.png\" style=\"width:100%; height:100%; object-fit:contain;\" alt=\"Server icon\">\n {{ else }}\n <img src=\"data:image/png;base64, {{ $icon }}\" style=\"width:100%; height:100%; object-fit:contain;\" alt=\"Server icon\">\n {{ end }}\n </div>\n\n <!-- Right side: Info -->\n <div style=\"display:flex; flex-direction:column;\">\n <!-- First row: Server Name + IP -->\n <div style=\"display:flex; align-items:center; gap:6px;\">\n <span class=\"size-h4 block text-truncate color-primary\">\n {{ $name }}\n </span>\n\n {{ if and ($is_running) (not $starting) (not (eq $server_addr \"\")) }}\n <div style=\"font-size:0.9em; color:var(--color-secondary);\">\n <span class=\"size-h6 color-secondary\">\n {{ $server_addr }}\n </span>\n </div>\n {{ end }}\n </div>\n\n <!-- Second row: MOTD & Stats if server is running, otherwise show status msg ... -->\n {{ if and ($is_running) (not $starting) }}\n {{ if and (not (eq $motd \"\")) ($displayMOTD) }}\n <div style=\"font-size:0.9em; color:var(--color-secondary);\">\n {{ replaceMatches \"§.\" \"\" $motd }}\n </div>\n {{ end }}\n\n <div style=\"font-size:0.9em; color:var(--color-secondary);\">\n {{ $version }} - {{ $online_players }}/{{ $max_players }} players - {{ $size }}\n </div>\n\n <!-- lots of assumptions about the boolean states meanings from crafty api.. -->\n {{ else if $starting }}\n <div style=\"font-size:0.9em; color:var(--color-secondary);\">Server is starting up..</div>\n {{ else if $importing }}\n <div style=\"font-size:0.9em; color:var(--color-secondary);\">Server is being imported..</div>\n {{ else if $updating }}\n <div style=\"font-size:0.9em; color:var(--color-secondary);\">Server is being updated..</div>\n {{ else if $crashed }}\n <div style=\"font-size:0.9em; color:var(--color-secondary);\">Server has crashed!</div>\n {{ else }}\n <div style=\"font-size:0.9em; color:var(--color-secondary);\">Server is offline</div>\n {{ end }}\n </div>\n</div>";
}
]