start of internal html files
This commit is contained in:
@ -29,6 +29,7 @@ FROM scratch
|
||||
# copy the build artifact from the build stage
|
||||
COPY --from=build /http_server/target/x86_64-unknown-linux-musl/release/http_server /
|
||||
COPY ./www /www
|
||||
COPY ./internal /internal
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
|
13
internal/server-info.html
Normal file
13
internal/server-info.html
Normal file
@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>server-info</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Server written by AustrianToast</h1>
|
||||
<a href="https://gitea.hopeless-cloud.xyz/AustrianToast/http_server">Source code</a>
|
||||
<br>
|
||||
<a href="mailto:austriantoast@hopeless-cloud.xyz">AustrianToasts e-mail</a>
|
||||
</body>
|
||||
</html>
|
36
src/main.rs
36
src/main.rs
@ -418,7 +418,6 @@ fn response_builder(
|
||||
fn try_get_file(start_line: &StartLine, field_lines: &HashMap<String, Vec<String>>) -> Vec<u8> {
|
||||
let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
|
||||
let mut response_body: Vec<u8> = vec![];
|
||||
|
||||
let path: PathBuf = match start_line.target.as_str() {
|
||||
"/" => PathBuf::from("/www/index.html"),
|
||||
_ => PathBuf::from(format!("/www{}", start_line.target)),
|
||||
@ -475,6 +474,37 @@ fn try_get_file(start_line: &StartLine, field_lines: &HashMap<String, Vec<String
|
||||
}
|
||||
}
|
||||
|
||||
fn try_get_file_internal(start_line: &StartLine) -> Vec<u8> {
|
||||
let mut response_field_lines: HashMap<String, Vec<String>> = HashMap::new();
|
||||
let mut response_body: Vec<u8> = vec![];
|
||||
let path: PathBuf = PathBuf::from(format!("/internal{}.html", start_line.target));
|
||||
|
||||
match fs::read(&path) {
|
||||
Ok(val) => {
|
||||
val.iter().for_each(|byte| response_body.push(*byte));
|
||||
|
||||
response_field_lines.insert(
|
||||
String::from("content-length"),
|
||||
vec![response_body.len().to_string()],
|
||||
);
|
||||
|
||||
let mime_type = mime_guess::from_path(&path)
|
||||
.first()
|
||||
.expect("Could not guess mime-type from path");
|
||||
|
||||
response_field_lines.insert(String::from("content-type"), vec![mime_type.to_string()]);
|
||||
|
||||
response_builder(
|
||||
start_line.method,
|
||||
"HTTP/1.1 200 ",
|
||||
Some(response_field_lines),
|
||||
Some(response_body),
|
||||
)
|
||||
}
|
||||
Err(_) => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
let mut line = String::new();
|
||||
let mut reader = BufReader::new(&mut stream);
|
||||
@ -537,8 +567,8 @@ fn handle_request(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
|
||||
let response = match start_line.target.as_str() {
|
||||
// For docker healtcheck. If the server can properly respond, then it must be healthy.
|
||||
"/server-health" => response_builder(RequestMethods::Head, "HTTP/1.1 200 ", None, None),
|
||||
// "/server-stats" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
|
||||
// "/server-info" => response_builder(start_line.method, "HTTP/1.1 404 ", None, None),
|
||||
"/server-stats" => try_get_file_internal(&start_line),
|
||||
"/server-info" => try_get_file_internal(&start_line),
|
||||
_ => try_get_file(&start_line, &field_lines),
|
||||
};
|
||||
stream.write_all(&response)?;
|
||||
|
Reference in New Issue
Block a user