feat(homelab)!: create interface for homelab management, use templating for route generation & support more options and route types

This commit is contained in:
2026-01-11 14:57:19 -08:00
parent 29cff3bf84
commit 73f8cb91c4
45 changed files with 3907 additions and 109 deletions

1
nix/homelab/api/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
target

2683
nix/homelab/api/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,16 @@
[package]
name = "homelab-api"
version = "0.1.0"
edition = "2024"
[dependencies]
actix-web = "4.12.1"
dotenvy = "0.15.7"
nom = "8.0.0"
rcon = { version = "0.6.0", features = ["rt-tokio"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "2.0.17"
tokio = "1.49.0"
kube = { version = "2.0.1", features = ["client", "runtime"] }
k8s-openapi = { version = "0.26", features = ["v1_32"] }

27
nix/homelab/api/flake.lock generated Normal file
View File

@@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1767799921,
"narHash": "sha256-r4GVX+FToWVE2My8VVZH4V0pTIpnu2ZE8/Z4uxGEMBE=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "d351d0653aeb7877273920cd3e823994e7579b0b",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-25.11",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

38
nix/homelab/api/flake.nix Normal file
View File

@@ -0,0 +1,38 @@
{
description = "Homelab api";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-25.11";
};
outputs =
{ nixpkgs, ... }@inputs:
let
systems = [
"x86_64-linux"
"aarch64-linux"
];
forAllSystems =
f:
nixpkgs.lib.genAttrs systems (
system:
f {
inherit system;
pkgs = nixpkgs.legacyPackages.${system};
}
);
in
{
devShells = forAllSystems (
{ system, pkgs }:
{
default = pkgs.mkShell {
buildInputs = with pkgs; [
openssl
pkgconf
];
};
}
);
};
}

View File

@@ -0,0 +1 @@
pub mod server_stats;

View File

@@ -0,0 +1,30 @@
use actix_web::{HttpResponse, web};
use serde::Serialize;
use crate::{AppState, error::Result, rcon::parse_online_list};
#[derive(Serialize)]
pub struct ServerStats {
pub status: String,
pub players_online: u16,
pub max_players: u16,
pub uptime: Option<String>,
pub world_size: Option<String>,
}
pub async fn get_server_stats(app_state: web::Data<AppState>) -> Result<HttpResponse> {
let list_response = app_state.rcon.cmd("list").await?;
let (_, (players_online, max_players)) =
parse_online_list(&list_response).map_err(|e| crate::error::Error::Parse(e.to_string()))?;
let stats = ServerStats {
status: "Online".to_string(),
players_online,
max_players,
uptime: None,
world_size: None,
};
Ok(HttpResponse::Ok().json(stats))
}

View File

@@ -0,0 +1,25 @@
use actix_web::{HttpResponse, ResponseError, body::BoxBody, http::StatusCode};
use thiserror::Error;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[error("rcon error: {0}")]
Rcon(#[from] rcon::Error),
#[error("parse error: {0}")]
Parse(String),
}
impl ResponseError for Error {
fn status_code(&self) -> StatusCode {
match self {
Self::Rcon(_) => StatusCode::SERVICE_UNAVAILABLE,
_ => StatusCode::INTERNAL_SERVER_ERROR,
}
}
fn error_response(&self) -> HttpResponse<BoxBody> {
HttpResponse::build(self.status_code()).body(self.to_string())
}
}

View File

@@ -0,0 +1,59 @@
mod endpoints;
mod error;
mod rcon;
use std::env;
use actix_web::{App, HttpServer, web};
use crate::rcon::RconClient;
struct AppState {
rcon: RconClient,
}
struct Env {
rcon_password: String,
}
#[cfg(debug_assertions)]
fn load_env() -> Env {
dotenvy::dotenv().ok();
Env {
rcon_password: env::var("RCON_PASSWORD")
.expect("environment variable RCON_PASSWORD must be set"),
}
}
#[cfg(not(debug_assertions))]
fn load_env() -> Env {
Env {
rcon_password: env::var("RCON_PASSWORD")
.expect("environment variable RCON_PASSWORD must be set"),
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let env = load_env();
let app_state = web::Data::new(AppState {
rcon: RconClient::new(env.rcon_password),
});
HttpServer::new(move || {
App::new()
.app_data(app_state.clone())
.route(
"/",
web::get()
.to(async || concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))),
)
.service(web::scope("/api").route(
"/minecraft-server-stats",
web::get().to(endpoints::server_stats::get_server_stats),
))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}

View File

@@ -0,0 +1,53 @@
use nom::{
IResult, Parser, bytes::complete::tag, character::complete::digit1, combinator::map_res,
sequence::preceded,
};
use rcon::Connection;
use tokio::{net::TcpStream, sync::Mutex};
use crate::error::Result;
fn parse_u16(input: &str) -> IResult<&str, u16> {
map_res(digit1, |s: &str| s.parse::<u16>()).parse(input)
}
pub fn parse_online_list(input: &str) -> IResult<&str, (u16, u16)> {
let (remaining, (online, max)) = (
preceded(tag("There are "), parse_u16),
preceded(tag(" of a max of "), parse_u16),
)
.parse(input)?;
Ok((remaining, (online, max)))
}
pub struct RconClient {
connection: Mutex<Option<Connection<TcpStream>>>,
rcon_password: String,
}
impl RconClient {
pub fn new(rcon_password: String) -> Self {
Self {
connection: None.into(),
rcon_password,
}
}
pub async fn cmd(&self, command: &str) -> Result<String> {
let mut connection = self.connection.lock().await;
if connection.is_none() {
let conn = create_connection(&self.rcon_password).await?;
*connection = Some(conn)
}
Ok(connection.as_mut().unwrap().cmd(command).await?)
}
}
async fn create_connection(rcon_password: &str) -> Result<Connection<TcpStream>> {
Ok(Connection::<TcpStream>::builder()
.enable_minecraft_quirks(true)
.connect("192.168.27.12:25575", rcon_password)
.await?)
}