feat(homelab)!: create interface for homelab management, use templating for route generation & support more options and route types
This commit is contained in:
1
nix/homelab/api/src/endpoints/mod.rs
Normal file
1
nix/homelab/api/src/endpoints/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod server_stats;
|
||||
30
nix/homelab/api/src/endpoints/server_stats.rs
Normal file
30
nix/homelab/api/src/endpoints/server_stats.rs
Normal 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))
|
||||
}
|
||||
25
nix/homelab/api/src/error.rs
Normal file
25
nix/homelab/api/src/error.rs
Normal 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())
|
||||
}
|
||||
}
|
||||
59
nix/homelab/api/src/main.rs
Normal file
59
nix/homelab/api/src/main.rs
Normal 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
|
||||
}
|
||||
53
nix/homelab/api/src/rcon.rs
Normal file
53
nix/homelab/api/src/rcon.rs
Normal 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?)
|
||||
}
|
||||
Reference in New Issue
Block a user