From f5ef25145828cc13179349ae595f057ce0396ad9 Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 16 Jun 2025 18:06:43 +0100 Subject: [PATCH] embed web source --- src/main.rs | 106 ++++++++++--------------- {public => src/public}/style/index.css | 0 {views => src/views}/index.html | 0 3 files changed, 44 insertions(+), 62 deletions(-) rename {public => src/public}/style/index.css (100%) rename {views => src/views}/index.html (100%) diff --git a/src/main.rs b/src/main.rs index c4ab06d..f3e5190 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::io::{Result}; use std::net::{ToSocketAddrs}; -use std::{env, fs}; +use std::{env}; use mcstatusface::http::{HttpServer, StatusCode}; use mcstatusface::{MinecraftStatus}; @@ -58,20 +58,11 @@ env!("CARGO_PKG_VERSION")); } if request.path() == "/style/index.css" { - match fs::read_to_string("./public/style/index.css") { - Ok(content) => { - response.set_header("Content-Type", "text/css".to_string()); - response.status(StatusCode::OK); - response.body(content.to_string()); - return response.send(); - } - Err(err) => { - eprint!("failed to load index.css: {}\n", err.to_string()); - response.status(StatusCode::InternalServerError); - response.body("Internal Server Error\n".to_string()); - return response.send(); - } - } + let content = include_str!("public/style/index.css"); + response.set_header("Content-Type", "text/css".to_string()); + response.status(StatusCode::OK); + response.body(content.to_string()); + return response.send(); } if request.path() == "/" { @@ -79,36 +70,35 @@ env!("CARGO_PKG_VERSION")); |accept| accept.contains("text/html") ) { // HTML response - match fs::read_to_string("./views/index.html") { - Ok(mut content) => { - response.set_header("Content-Type", "text/html".to_string()); - response.status(StatusCode::OK); - let query_response: String; - match request.query().get("s") { - Some(query_address) => { - let mut address = query_address.to_string(); - if !address.contains(":") { address.push_str(":25565"); } - match address.to_socket_addrs() { - Err(_) => { - response.set_header("Content-Type", "text/html".to_string()); - response.status(StatusCode::BadRequest); - response.body("Server address is invalid or unreachable.\n".to_string()); - return response.send(); - } - Ok(mut addrs_iter) => { - let address = addrs_iter.next().unwrap(); + let content = include_str!("views/index.html"); + response.set_header("Content-Type", "text/html".to_string()); + response.status(StatusCode::OK); + let query_response: String; + match request.query().get("s") { + Some(query_address) => { + let mut address = query_address.to_string(); + if !address.contains(":") { address.push_str(":25565"); } + match address.to_socket_addrs() { + Err(_) => { + response.set_header("Content-Type", "text/html".to_string()); + response.status(StatusCode::BadRequest); + response.body("Server address is invalid or unreachable.\n".to_string()); + return response.send(); + } + Ok(mut addrs_iter) => { + let address = addrs_iter.next().unwrap(); - let status = MinecraftStatus::fetch(address).unwrap(); + let status = MinecraftStatus::fetch(address).unwrap(); - let minecraft_status = MinecraftStatusResponse{ - version: &status.version.name, - players: status.players.online, - max_players: status.players.max, - motd: status.parse_description(), - }; + let minecraft_status = MinecraftStatusResponse{ + version: &status.version.name, + players: status.players.online, + max_players: status.players.max, + motd: status.parse_description(), + }; - query_response = format!( - "
+ query_response = format!( + "

Server Details

Version: {}
@@ -120,31 +110,23 @@ env!("CARGO_PKG_VERSION")); minecraft_status.players, minecraft_status.max_players, sanitize_html(&minecraft_status.motd).to_string(), - ); - } - } - } - None => { - query_response = String::from(""); + ); } } - - content = content - .replace("{{response}}", &query_response) - .replace("{{host}}", match request.headers().get("Host") { - Some(host) => { host } - None => { "mcq.bliss.town" } - }); - response.body(content.to_string()); - return response.send(); } - Err(err) => { - eprint!("failed to load index.html: {}\n", err.to_string()); - response.status(StatusCode::InternalServerError); - response.body("Internal Server Error\n".to_string()); - return response.send(); + None => { + query_response = String::from(""); } } + + let response_content = content + .replace("{{response}}", &query_response) + .replace("{{host}}", match request.headers().get("Host") { + Some(host) => { host } + None => { "mcq.bliss.town" } + }); + response.body(response_content.to_string()); + return response.send(); } // JSON response diff --git a/public/style/index.css b/src/public/style/index.css similarity index 100% rename from public/style/index.css rename to src/public/style/index.css diff --git a/views/index.html b/src/views/index.html similarity index 100% rename from views/index.html rename to src/views/index.html