embed web source
This commit is contained in:
parent
6bd7379df3
commit
f5ef251458
3 changed files with 44 additions and 62 deletions
106
src/main.rs
106
src/main.rs
|
@ -1,6 +1,6 @@
|
||||||
use std::io::{Result};
|
use std::io::{Result};
|
||||||
use std::net::{ToSocketAddrs};
|
use std::net::{ToSocketAddrs};
|
||||||
use std::{env, fs};
|
use std::{env};
|
||||||
|
|
||||||
use mcstatusface::http::{HttpServer, StatusCode};
|
use mcstatusface::http::{HttpServer, StatusCode};
|
||||||
use mcstatusface::{MinecraftStatus};
|
use mcstatusface::{MinecraftStatus};
|
||||||
|
@ -58,20 +58,11 @@ env!("CARGO_PKG_VERSION"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.path() == "/style/index.css" {
|
if request.path() == "/style/index.css" {
|
||||||
match fs::read_to_string("./public/style/index.css") {
|
let content = include_str!("public/style/index.css");
|
||||||
Ok(content) => {
|
response.set_header("Content-Type", "text/css".to_string());
|
||||||
response.set_header("Content-Type", "text/css".to_string());
|
response.status(StatusCode::OK);
|
||||||
response.status(StatusCode::OK);
|
response.body(content.to_string());
|
||||||
response.body(content.to_string());
|
return response.send();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.path() == "/" {
|
if request.path() == "/" {
|
||||||
|
@ -79,36 +70,35 @@ env!("CARGO_PKG_VERSION"));
|
||||||
|accept| accept.contains("text/html")
|
|accept| accept.contains("text/html")
|
||||||
) {
|
) {
|
||||||
// HTML response
|
// HTML response
|
||||||
match fs::read_to_string("./views/index.html") {
|
let content = include_str!("views/index.html");
|
||||||
Ok(mut content) => {
|
response.set_header("Content-Type", "text/html".to_string());
|
||||||
response.set_header("Content-Type", "text/html".to_string());
|
response.status(StatusCode::OK);
|
||||||
response.status(StatusCode::OK);
|
let query_response: String;
|
||||||
let query_response: String;
|
match request.query().get("s") {
|
||||||
match request.query().get("s") {
|
Some(query_address) => {
|
||||||
Some(query_address) => {
|
let mut address = query_address.to_string();
|
||||||
let mut address = query_address.to_string();
|
if !address.contains(":") { address.push_str(":25565"); }
|
||||||
if !address.contains(":") { address.push_str(":25565"); }
|
match address.to_socket_addrs() {
|
||||||
match address.to_socket_addrs() {
|
Err(_) => {
|
||||||
Err(_) => {
|
response.set_header("Content-Type", "text/html".to_string());
|
||||||
response.set_header("Content-Type", "text/html".to_string());
|
response.status(StatusCode::BadRequest);
|
||||||
response.status(StatusCode::BadRequest);
|
response.body("Server address is invalid or unreachable.\n".to_string());
|
||||||
response.body("Server address is invalid or unreachable.\n".to_string());
|
return response.send();
|
||||||
return response.send();
|
}
|
||||||
}
|
Ok(mut addrs_iter) => {
|
||||||
Ok(mut addrs_iter) => {
|
let address = addrs_iter.next().unwrap();
|
||||||
let address = addrs_iter.next().unwrap();
|
|
||||||
|
|
||||||
let status = MinecraftStatus::fetch(address).unwrap();
|
let status = MinecraftStatus::fetch(address).unwrap();
|
||||||
|
|
||||||
let minecraft_status = MinecraftStatusResponse{
|
let minecraft_status = MinecraftStatusResponse{
|
||||||
version: &status.version.name,
|
version: &status.version.name,
|
||||||
players: status.players.online,
|
players: status.players.online,
|
||||||
max_players: status.players.max,
|
max_players: status.players.max,
|
||||||
motd: status.parse_description(),
|
motd: status.parse_description(),
|
||||||
};
|
};
|
||||||
|
|
||||||
query_response = format!(
|
query_response = format!(
|
||||||
"<hr/>
|
"<hr/>
|
||||||
<h2>Server Details</h2>
|
<h2>Server Details</h2>
|
||||||
<p>
|
<p>
|
||||||
<strong>Version:</strong> <code>{}</code><br/>
|
<strong>Version:</strong> <code>{}</code><br/>
|
||||||
|
@ -120,31 +110,23 @@ env!("CARGO_PKG_VERSION"));
|
||||||
minecraft_status.players,
|
minecraft_status.players,
|
||||||
minecraft_status.max_players,
|
minecraft_status.max_players,
|
||||||
sanitize_html(&minecraft_status.motd).to_string(),
|
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) => {
|
None => {
|
||||||
eprint!("failed to load index.html: {}\n", err.to_string());
|
query_response = String::from("");
|
||||||
response.status(StatusCode::InternalServerError);
|
|
||||||
response.body("Internal Server Error\n".to_string());
|
|
||||||
return response.send();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// JSON response
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue