yea that oughta do it for now i think
This commit is contained in:
parent
eda7f79fb0
commit
cf28e189cd
2 changed files with 142 additions and 61 deletions
62
src/main.rs
62
src/main.rs
|
@ -2,13 +2,13 @@ use std::io::{Result};
|
|||
use std::net::{ToSocketAddrs};
|
||||
use std::env;
|
||||
|
||||
use mcstatusface::http::HttpServer;
|
||||
use mcstatusface::http::{HttpServer, StatusCode};
|
||||
use mcstatusface::{MinecraftStatus};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() < 2 {
|
||||
eprintln!("Usage: {} <serve | address[:port]>", args[0]);
|
||||
eprintln!("Usage: {} [serve] <address[:port]>", args[0]);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,63 @@ fn main() -> Result<()> {
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
HttpServer::new("0.0.0.0").start().unwrap();
|
||||
let mut address = "0.0.0.0:8080".to_string();
|
||||
if args.len() > 2 { address = args[2].to_string() }
|
||||
|
||||
HttpServer::new(address, 64).start(|request, mut response| {
|
||||
response.status(StatusCode::OK);
|
||||
response.set_header("x-powered-by", "GIRL FUEL".to_string());
|
||||
|
||||
if request.method() != "GET" {
|
||||
response.status(StatusCode::NotFound);
|
||||
return response.send()
|
||||
}
|
||||
|
||||
if !request.query().contains_key("s") {
|
||||
response.status(StatusCode::BadRequest);
|
||||
// TODO: nice index landing page for browsers
|
||||
response.body("?s=<server address>\n".to_string());
|
||||
return response.send()
|
||||
}
|
||||
|
||||
let mut address = request.query().get("s").unwrap().to_string();
|
||||
if !address.contains(":") { address.push_str(":25565"); }
|
||||
let mut addrs_iter = address.to_socket_addrs().unwrap();
|
||||
let address = addrs_iter.next().unwrap();
|
||||
|
||||
let status = MinecraftStatus::fetch(address).unwrap();
|
||||
|
||||
#[derive(serde::Serialize)]
|
||||
struct MinecraftStatusResponse<'a> {
|
||||
version: &'a String,
|
||||
players: u32,
|
||||
max_players: u32,
|
||||
motd: String,
|
||||
}
|
||||
|
||||
let minecraft_status = MinecraftStatusResponse{
|
||||
version: &status.version.name,
|
||||
players: status.players.online,
|
||||
max_players: status.players.max,
|
||||
motd: status.parse_description(),
|
||||
};
|
||||
|
||||
match serde_json::to_string(&minecraft_status) {
|
||||
Ok(json) => {
|
||||
response.status(StatusCode::OK);
|
||||
response.set_header("Content-Type", "application/json".to_string());
|
||||
response.body(json);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Request to {address} failed: {e}");
|
||||
response.status(StatusCode::InternalServerError);
|
||||
response.set_header("Content-Type", "text/plain".to_string());
|
||||
response.body("Unable to reach the requested server.\n".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
response.send()
|
||||
}).unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue